HCC Docs

Event webhooks

import { SIGNATURE_HEADER, webhooks } from '@hcc/sdk';

export default {
  async fetch(request: Request, env: { HCC_WEBHOOK_SECRET: string }) {
    const event = await webhooks
      .constructEvent({
        secret: env.HCC_WEBHOOK_SECRET,
        body: await request.text(),
        signature: request.headers.get(SIGNATURE_HEADER),
      })
      .catch(() => null);
    if (!event) return new Response('invalid', { status: 400 });

    switch (event.type) {
      case 'call.completed':
        console.log(event.data.callId, event.data.durationSeconds);
        break;
      case 'conversation.transcript.ready':
        console.log(event.data.summary);
        break;
    }
    return new Response(null, { status: 204 });
  },
};

constructEvent checks the Hcc-Signature header (t=<unix>,v1=<hmac-sha256>, 5 minute tolerance), then validates the payload and returns a typed event; event.type narrows event.data. It throws an HccError with code invalid_signature or bad_request. Use webhooks.verify for a boolean check only.