matter.work
Building Apps / The SDK

Webhooks

Take events services like Stripe and Plain post to your app, in its worker.

A webhook gives your app a URL that a service like Stripe, Plain, GitHub or Slack posts JSON to when something happens. Matter checks the request’s signature, keeps the event, and hands it to your app’s worker through matter.webhooks.on. Your code decides what to do with it: write a row, start a routine, call one of the member’s connections.

Declare each webhook in webhooks, with how its requests are signed:

matter.json
{
  "minMatterVersion": "0.5.15",
  "worker": "worker.mjs",
  "webhooks": {
    "plain": { "verify": "plain" }
  }
}
worker.mjs
matter.webhooks.on('plain', async (event) => {
  if (event.body.type !== 'thread.thread_created') return
  await matter.routines.run('reply', { threadId: event.body.payload.thread.id }, { requestKey: event.id })
})

Setting up#

  1. Deploy a version that declares the webhook.
  2. Run matter app webhooks to get its URL, and give that URL to the service.
  3. Copy the signing secret the service shows you, and save it with matter app webhook plain --secret. Until then, Matter turns the service’s requests away.

The URL stays the same across deploys. Anyone who can edit the app can see it and set the secret.

For Slack ("verify": "slack", for its Events API), set minMatterVersion to 0.5.16, and save the signing secret first, from the Slack app’s Basic Information page: Slack checks the URL as soon as you add it, and Matter answers that check itself.

Where events go#

Events go to one computer: the app’s runner, the one that runs its app routines (see Managing apps). Its worker gets them, and acts as that member, on their connections and models. Workers on other computers never see them, and say so once in their log (matter app logs worker), with the computer that gets them. While the runner is offline, events wait on the server for up to 7 days.

Handling events#

The handler gets one event at a time. When it returns, the event is done. When it throws, Matter gives it again later, waiting longer each time, up to 10 tries. A handler has 10 minutes before Matter gives the event to the worker again.

Services send an event again when they don’t hear back, and Matter keeps it once. But your handler can still see an event twice, so make that harmless: pass event.id as a routine run’s requestKey, or upsert by it.

Calls#

Call Description
on(key, handler) Takes the events of the webhook key (a list of keys, or '*' for all of them), in the worker. Returns a function that stops it.

Each event is { id, key, body, headers, receivedAt, attempts }: body is the JSON the service posted, headers its headers in lowercase, and attempts is 1 the first time.

Request bodies must be JSON, up to 1 MB.