Webhooks

Check that an event is from Goomzo, and manage your endpoints.

verifyWebhook checks an event's signature and timestamp, and answers with the event, typed. See Signatures.

Requirements

  • An endpoint, and its signing secret, which starts with whsec_. See Webhooks.

Receive an event

Pass the body exactly as it arrived, as text:

import { verifyWebhook, WebhookVerificationError } from '@goomzo/sdk'

export async function POST(request: Request) {
  try {
    const event = await verifyWebhook(await request.text(), request.headers, process.env.GOOMZO_WEBHOOK_SECRET!)
    if (event.type === 'message.delivered') {
      await markDelivered(event.data.id)
    }
    return new Response(null, { status: 204 })
  } catch (error) {
    if (error instanceof WebhookVerificationError) return new Response(null, { status: 400 })
    throw error
  }
}

Important: Don't parse the body as JSON before checking it. The signature is of the body as it arrived.

Manage endpoints

const endpoint = await goomzo.webhookEndpoints.create({
  url: 'https://example.com/webhooks/goomzo',
  events: ['message.delivered', 'message.failed'],
})
MethodDoes
webhookEndpoints.list()Lists your endpoints.
webhookEndpoints.get(id)Gets one.
webhookEndpoints.update(id, body)Changes an endpoint, or turns it off.
webhookEndpoints.delete(id)Deletes one.

Next steps

Last updated