> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spott.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Verify webhook deliveries

> Check that each webhook delivery comes from Spott, and answer it the right way.

Your endpoint's URL is public, so anyone who finds it can post to it. Spott signs every
delivery with the endpoint's secret, and your receiver checks that signature before it
acts on the delivery. This works the same whether the endpoint was
[set up in Settings](https://docs.spott.io/docs/developers/webhooks.md#in-settings) or
[through the API](https://docs.spott.io/docs/developers/manage-webhooks.md).

## What a delivery contains

Each delivery is a `POST` with a JSON body. The body's `event.name` says which event it
is, and each event's page in the sidebar shows its payload. Three headers carry what you
need to check it:

| Header | What it holds |
| - | - |
| `svix-id` | The ID of the message. It stays the same when a failed delivery is retried, so use it to skip a message you already handled. |
| `svix-timestamp` | When the message was sent, in Unix seconds. |
| `svix-signature` | One or more signatures, separated by spaces, each written as `v1,<signature>`. |

## Find the secret

For an endpoint created through the API, the secret is the `secret` that
[creating the endpoint](https://docs.spott.io/docs/developers/manage-webhooks.md#create-an-endpoint) returned, or
that [your last rotation](https://docs.spott.io/docs/developers/manage-webhooks.md#rotate-the-secret) returned. The
API cannot return it again. **Settings → Webhooks** also shows the secret of each endpoint
to an admin.

## Check the signature

The open-source `svix` library checks all three headers for you, in most languages, and
also turns away old messages that someone replays:

```javascript theme={null}
import { Webhook } from 'svix';

const webhook = new Webhook(process.env.SPOTT_WEBHOOK_SECRET);

// rawBody is the request body exactly as it arrived, before any JSON parsing.
const payload = webhook.verify(rawBody, {
  'svix-id': headers['svix-id'],
  'svix-timestamp': headers['svix-timestamp'],
  'svix-signature': headers['svix-signature'],
});
```

To check a delivery without the library:

1. Take the part of the secret after `whsec_` and decode it from base64. That is the key.
2. Join `svix-id`, `svix-timestamp` and the raw body with dots: `<svix-id>.<svix-timestamp>.<body>`.
3. Compute the HMAC-SHA256 of that text with the key, and encode the result in base64.
4. Accept the delivery if the result equals any one of the `v1` signatures in
   `svix-signature`, compared in constant time.
5. Turn it away if `svix-timestamp` is more than a few minutes from your current time.

The header can carry several signatures because, for a while after a
[rotation](https://docs.spott.io/docs/developers/manage-webhooks.md#rotate-the-secret), Spott signs with both the
old and the new secret. Accepting any one match is what keeps your receiver working
through a rotation, whichever of the two secrets it has.

## Answer the delivery

Answer with a `2xx` status as soon as the signature checks out, and do the real work
afterwards, for example from a queue. Any other status, or no answer, counts as a failed
delivery, and Spott tries again later. A retry has the same `svix-id`, so check it before
you act twice on the same message.

## Troubleshooting

| Problem | What to do |
| - | - |
| **The signature never matches** | Check the raw body: parsing the JSON and writing it out again changes the bytes. Check that the secret belongs to this endpoint, and, after a rotation, that the old secret's overlap has not ended. |
| **Deliveries fail right after a rotation** | The overlap ended, or was 0, before your receivers had the new secret. Deploy it; the failed deliveries are tried again later. See [Rotate the secret](https://docs.spott.io/docs/developers/manage-webhooks.md#rotate-the-secret). |
| **You lost the secret** | The API cannot return it again. An admin can read it on the endpoint in **Settings → Webhooks**, or you can rotate it to get a new one. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.