# Verifying webhook signatures (https://docs.opensequence.dev/docs/webhooks/verifying)

How to verify Standard Webhooks signatures with the SDK or by hand, store the signing secret, and rotate it safely.

Every delivery carries three headers following the [Standard Webhooks](https://www.standardwebhooks.com) specification. Verify them before you trust the body; otherwise anyone who finds your URL can enroll a fake "reply" into your CRM.

<CodeGroup>

```ts title="TypeScript"
import { verifyWebhook, WebhookVerificationError } from '@opensequence/sdk';
import express from 'express';

const app = express();
// the RAW body is required: a parsed and re-serialized body will not verify
app.post('/hooks/opensequence', express.raw({ type: 'application/json' }), (req, res) => {
  try {
    const event = verifyWebhook({ secret: process.env.OSEQ_WEBHOOK_SECRET!, headers: req.headers, body: req.body });
    // event.id, event.type, event.project, event.occurred_at, event.data
    handle(event);
    res.sendStatus(200);
  } catch (err) {
    if (err instanceof WebhookVerificationError) return res.status(401).send(err.code);
    res.sendStatus(500); // the service retries
  }
});
```

```python title="Python"
import base64, hashlib, hmac, time

def verify(secrets: list[str], headers: dict, raw_body: bytes, tolerance=300) -> dict:
    """`secrets` holds every secret currently accepted for the endpoint: one, or two during a rotation."""
    msg_id = headers["webhook-id"]; ts = headers["webhook-timestamp"]; sig = headers["webhook-signature"]
    if abs(time.time() - int(ts)) > tolerance:
        raise ValueError("timestamp outside tolerance")
    signed = f"{msg_id}.{ts}.".encode() + raw_body
    for secret in secrets:
        key = base64.b64decode(secret.removeprefix("whsec_"))
        expected = hmac.new(key, signed, hashlib.sha256).digest()
        for part in sig.split(" "):
            if part.startswith("v1,") and hmac.compare_digest(base64.b64decode(part[3:]), expected):
                import json; return json.loads(raw_body)
    raise ValueError("no signature matched")
```

```bash title="By hand"
# secret=whsec_<base64>; id and ts from the headers; body.json is the raw body
printf '%s.%s.' "$ID" "$TS" | cat - body.json \
  | openssl dgst -sha256 -mac HMAC -macopt hexkey:$(echo "${SECRET#whsec_}" | base64 -d | xxd -p -c 256) -binary \
  | base64
# compare with the part after "v1," in webhook-signature
```

</CodeGroup>

## The scheme [#the-scheme]

| Header | Value |
|---|---|
| `webhook-id` | the event id (`evt_…`), the same on every retry of the same delivery |
| `webhook-timestamp` | Unix seconds when this attempt was signed |
| `webhook-signature` | `v1,<base64>` where the MAC is `HMAC-SHA256(secret, "<id>.<timestamp>.<raw body>")` and the key is the base64-decoded part of the secret after `whsec_`. Several space-separated `v1,…` values may appear |

Reject timestamps more than five minutes from your clock (the SDK's default `toleranceSeconds` is 300) to blunt replays. Compare MACs in constant time. Use the **raw** request bytes: the service signs the exact JSON it sends, and a body that went through a JSON parser and back will not match.

## Storing the secret [#storing-the-secret]

The secret is in the response of `POST /v1/webhooks` (and of `rotate-secret`) once; no later call returns it, and the service keeps only an encrypted copy for signing. Store it where you store other credentials (a secrets manager, or an encrypted column), keyed by the endpoint id (`wep_…`) so a project with several endpoints can find the right one, and never log it. If it is lost, rotate: there is no way to read it back.

A receiver without the SDK should keep a **list** of accepted secrets per endpoint, not a single value, and try each one against the `webhook-signature` header. That is what makes rotation safe:

## Rotation [#rotation]

`POST /v1/webhooks/{id}/rotate-secret` returns a new secret; deliveries from that moment are signed with it and there is no overlap window on the service side. The SDK's `verifyWebhook` accepts `secret: [newSecret, oldSecret]`. Without the SDK, the procedure is the same four steps:

1. Call `rotate-secret` and store the new secret **alongside** the old one for that endpoint.
2. Verify incoming deliveries against every stored secret (the Python example above takes a list and tries each one before it raises).
3. Watch the deliveries log (`GET /v1/webhooks/{id}/deliveries`) until nothing signed with the old secret is still pending: a retry of a delivery first attempted before the rotation is re-signed with the **current** secret at each attempt, so this is quick, and it is only in-flight attempts between your call and your store update that can still carry the old signature.
4. Drop the old secret.

If you cannot change your receiver to accept two secrets, rotate at a quiet moment and accept that deliveries in that window fail once and are retried with the new signature five seconds later ([Retries and recovery](/docs/webhooks/retries-and-recovery)).

## Errors the SDK throws [#errors-the-sdk-throws]

`WebhookVerificationError` with `code` `MISSING_HEADERS`, `BAD_TIMESTAMP`, `TIMESTAMP_OUT_OF_TOLERANCE`, `NO_MATCHING_SIGNATURE` or `INVALID_BODY`. Answer a `4xx` to a bad signature: the delivery is still retried by the service (it does not know your reason), so a persistently rejected endpoint ends up disabled after three days, which is what you want for a stolen URL. `signWebhook(secret, id, timestamp, body)` is exported for your own tests.
