# Verify webhook deliveries

> Check that a webhook delivery came from Koil before trusting it, and handle retries without duplicates.

This guide is for `webhook` destinations. A queue or stream is not public and its deliveries are not signed; you trust them through who can publish to it ([Trusting deliveries](https://docs.koil.co/guides/subscribe-to-events#trusting-deliveries)). Deduping on the event `id`, below, applies to every type.

Every webhook delivery is signed with the destination's secret in the [Standard Webhooks](https://www.standardwebhooks.com/) format. Verify the signature before you act on an event: anyone can send a request to your endpoint. The signature covers the event id and a timestamp as well as the body, so a check that also refuses old timestamps stops a captured delivery from being replayed.

The secret is the one generated when you [created the destination](https://docs.koil.co/guides/subscribe-to-events#create-a-destination), or the `credentials.secret` you supplied. Read it with `GET /v1/event-destinations/{eventDestinationId}/secret`:

```bash
curl "https://api.koil.co/v1/event-destinations/des_6tW2fQ9mLp/secret" \
  -H "Authorization: Bearer $KOIL_API_KEY" \
  -H "Koil-Version: 2026-12-08"
```

## The headers

| Header | Value |
| --- | --- |
| `webhook-signature` | `v1,` then the base64 HMAC-SHA256 of `{webhook-id}.{webhook-timestamp}.{raw body}`, keyed with the base64-decoded destination secret (after its `whsec_` prefix). While a secret is being rotated it carries one signature per valid secret, space-separated: `v1,<current> v1,<previous>`. |
| `webhook-id` | The event `id`, for dedupe. Signed. |
| `webhook-topic` | The event `topic`. |
| `webhook-timestamp` | When the delivery was signed, in Unix seconds. Signed, so a replayed delivery can be refused. |
| `webhook-mode` | The event's `context.mode`, as a string, when it carries one. A queue destination receives it in the message's metadata as `mode` instead. |
| `webhook-profile-id` | The event's `context.profileId`, as a string, when it carries one. A queue destination receives it in the message's metadata as `profile-id` instead. |
| `webhook-platform-id` | The event's `context.platformId`, as a string, when it carries one. A queue destination receives it in the message's metadata as `platform-id` instead. |
| `webhook-collection` | The event's `context.collection`, as a string, when it carries one. A queue destination receives it in the message's metadata as `collection` instead. |
| `webhook-publish-request-id` | The event's `context.publishRequestId`, as a string, when it carries one. A queue destination receives it in the message's metadata as `publish-request-id` instead. |
| `webhook-media-id` | The event's `context.mediaId`, as a string, when it carries one. A queue destination receives it in the message's metadata as `media-id` instead. |
| `webhook-connection-id` | The event's `context.connectionId`, as a string, when it carries one. A queue destination receives it in the message's metadata as `connection-id` instead. |
| `webhook-event-destination-id` | The event's `context.eventDestinationId`, as a string, when it carries one. A queue destination receives it in the message's metadata as `event-destination-id` instead. |
| `webhook-backfill-request-id` | The event's `context.backfillRequestId`, as a string, when it carries one. A queue destination receives it in the message's metadata as `backfill-request-id` instead. |

The signature covers `webhook-id`, `webhook-timestamp`, and the body, and nothing else. `webhook-topic` and the routing headers are unsigned: routing a request on them before you verify it is fine, but after verifying, read the topic and routing values from the body, since a forged header would still pass the check.

## Verify in Node.js

Use a Standard Webhooks library rather than writing the check yourself: there is one for most languages, listed at [standardwebhooks.com](https://www.standardwebhooks.com/). In Node.js, install `standardwebhooks`. It checks the signature in constant time, accepts any listed signature, and refuses a timestamp more than five minutes from your clock.

Pass the **raw** body, exactly as received: re-serializing parsed JSON changes the bytes. With Express, keep the body raw on the webhook route, and pass the handler your own `enqueue`:

```ts
import express from "express";
import { Webhook } from "standardwebhooks";

export function createWebhookApp(
	enqueue: (event: unknown) => Promise<void>,
): express.Express {
	const app = express();
	const webhook = new Webhook(process.env.KOIL_WEBHOOK_SECRET ?? "");

	app.post(
		"/koil/events",
		express.raw({ type: "application/json" }),
		async (req, res) => {
			let event: unknown;
			try {
				// Checks the signature over the raw body and refuses a timestamp
				// more than five minutes off, then parses the JSON.
				event = webhook.verify(req.body, req.headers as Record<string, string>);
			} catch {
				res.status(400).send("bad signature");
				return;
			}

			// Dedupe on the event id, store or enqueue the event, then acknowledge.
			await enqueue(event);
			res.sendStatus(204);
		},
	);

	return app;
}
```

## Retries and duplicates

A delivery that does not get a `2xx` response is retried ([how often, and for how long](https://docs.koil.co/guides/subscribe-to-events#receiving-deliveries)), and a delivery can be [retried on request](https://docs.koil.co/guides/subscribe-to-events#delivery-history-and-retries), so the same event can arrive more than once. Each attempt is signed afresh, so its timestamp is current. `webhook-id` is the event's `id`, so dedupe on either: store each one you have processed and skip repeats. Return `2xx` only once the event is durably stored or enqueued — process it afterwards, so a slow handler never causes a retry.

## Rotating the secret

`POST /v1/event-destinations/{eventDestinationId}/rotate-secret` starts a rotation. Koil generates the new secret, and until `previousSecretInvalidAt` (24 hours after the rotation unless you set it) deliveries carry a signature for both the new and the previous secret. Read both with `GET /v1/event-destinations/{eventDestinationId}/secret`, which returns `previousSecret` and `previousSecretInvalidAt` during that window, deploy the new one to your receiver, and the check above keeps passing throughout. Rotating is the graceful way to change a secret: a `PATCH` of `credentials.secret` replaces it at once, with no overlap.
