--- title: Outbound Webhooks description: Send signed, retried webhooks to your customers' endpoints, and verify them on the receiving side. --- # Outbound Webhooks `@stacksjs/notifications/webhooks` sends events to endpoints your customers register: one queued delivery per subscribed endpoint, signed, retried with backoff, and logged attempt by attempt. Endpoints that keep failing are switched off. ## Register an endpoint ```typescript import { createWebhookEndpoint } from '@stacksjs/notifications/webhooks' const { endpoint, secret } = await createWebhookEndpoint({ type: 'team', id: team.id }, { url: 'https://example.com/hooks/stacks', events: ['batch.completed', 'usage.threshold'], // or ['*'] }) ``` Show `secret` to the customer once. It is stored encrypted with the app key, because the sender needs the plaintext to sign each delivery. The URL must be public HTTPS. Private, loopback and link-local addresses are refused when the endpoint is registered and again at every delivery, since a name can resolve to a different address later. ## Send an event ```typescript import { dispatchWebhook } from '@stacksjs/notifications/webhooks' await dispatchWebhook({ type: 'team', id: team.id }, 'batch.completed', { batchId: 42 }, { idempotencyKey: 'batch:42', }) ``` Each subscribed, active endpoint gets a `DeliverWebhookJob` on the `webhooks` queue. With an `idempotencyKey`, sending the same event for the same owner again does nothing. The body is a JSON envelope: ```json { "id": "msg_…", "type": "batch.completed", "created": 1800000000, "data": { "batchId": 42 } } ``` Every request carries these headers: | Header | Value | | --- | --- | | `X-Signature` | `t=,v1=.">` | | `X-Webhook-Id` | The message id. It stays the same across retries and redeliveries, so receivers can dedupe on it. | | `X-Webhook-Event` | The event name | | `X-Webhook-Attempt` | The attempt number, starting at 1 | ## Retries, limits and disabling - A delivery succeeds on any 2xx answer. Anything else, a timeout or a refused address is a failed attempt. A failed attempt is retried after 30 seconds, then 2 minutes, then 8 minutes, and so on (×4 each time, capped at 12 hours), for up to 8 attempts. - An endpoint is disabled after 30 failed attempts in a row. A success resets the count. `enableWebhookEndpoint(id)` switches it back on. - Requests time out after 10 seconds, and redirects are not followed. A redirect counts as a failed attempt. - Bodies over 256 KiB are refused when the event is sent. Send a reference the receiver can fetch instead. - The first 2 KiB of each answer is kept with its attempt. Change any of these with `configureWebhooks({ maxAttempts, backoff, disableAfterFailures, timeoutMs, maxPayloadBytes })`. ## Manage endpoints ```typescript import { redeliverWebhook, rotateWebhookSecret, sendTestWebhook, webhookDeliveries } from '@stacksjs/notifications/webhooks' await sendTestWebhook(endpoint.id) // a `webhook.test` event const log = await webhookDeliveries(endpoint.id) // every attempt, newest first await redeliverWebhook(log[0].id) // the same body, under the same message id const { secret } = await rotateWebhookSecret(endpoint.id, { overlapSeconds: 86_400 }) ``` During a rotation's overlap, each delivery carries a `v1` signature for both secrets, so a receiver holding either one accepts it. Schedule `pruneWebhookDeliveries(30)` to delete attempts older than 30 days. ## Verify on the receiving side ```typescript import { verifySignedWebhook } from '@stacksjs/security' const raw = await request.text() // the exact bytes, before any JSON parsing verifySignedWebhook(process.env.WEBHOOK_SECRET!, request.headers.get('x-signature')!, raw) const event = JSON.parse(raw) ``` `verifySignedWebhook` throws `InvalidWebhookSignature` when the signature does not match, including for a body that was changed, and when the timestamp is more than five minutes off. Verify the raw body: re-serialised JSON will not match. Answer 2xx quickly and do the work afterwards. A slow answer is a failed attempt and will be retried.