Verifying webhook signatures
Every webhook we send is signed with your secret. Verify it before you trust the payload.
Every delivery carries an X-Signature header. It is the hex HMAC-SHA256 of the
exact request body, keyed with your webhook secret. Recompute it and compare.
Endpoints on Webhooks v3 also get X-Signatures, the same digest once for every
signing key the endpoint holds, which is what lets you change keys without
rejecting a single delivery.
Hash the bytes you received
Hash the raw body, not a re-serialised copy of it. JSON.stringify(req.body)
will not reproduce our bytes, because key order and number formatting do not
survive the round trip, and the signature will not match. Read the body as a
string or a buffer before any JSON middleware touches it.
The header
X-Signature- Hex HMAC-SHA256 over the raw request body, keyed with your newest signing key. No prefix, no timestamp, no scheme label. Just the hex digest.
X-Signatures- Webhooks v3 only. The same digest for every active signing key, newest first, separated by commas. With one key it equals X-Signature.
The payload version is set per endpoint and shown on its page in the console.
Webhooks v2 sends X-Signature alone, exactly as it always has.
That is the whole scheme, so there is nothing else to parse. A valid signature proves the payload came from us and was not altered on the way. It does not prove the request is recent, because nothing signed carries a time. See what it does not prove.
Verify a signature
import { createHmac, timingSafeEqual } from 'node:crypto';
// rawBody is the exact body bytes, NOT JSON.stringify(req.body).
export function verify(rawBody, header, secret) {
const expected = createHmac('sha256', secret).update(rawBody).digest('hex');
// Constant-time: a length-dependent or short-circuiting comparison leaks how
// much of the signature was correct.
const a = Buffer.from(expected);
const b = Buffer.from(header);
return a.length === b.length && timingSafeEqual(a, b);
}Rotating your secret
Add and expire your signing keys yourself, in the console. An endpoint can hold more than one at a time, and each can carry an expiry date. A new key's secret is shown once, when you add it.
On Webhooks v3
Every delivery is signed with every active key, in X-Signatures. Accept a
delivery when any of them matches a secret you hold, and a rotation never
rejects a valid delivery:
- Add a new key in the console and store its secret.
- Deploy your handler with the new secret alongside the old one.
- Expire or revoke the old key. Its signature drops out of
X-Signaturesand the new key is the only one left.
import { createHmac, timingSafeEqual } from 'node:crypto';
// secrets: every signing secret you currently hold, old and new.
export function verifyAny(rawBody, signaturesHeader, secrets) {
const received = signaturesHeader.split(',').map((s) => Buffer.from(s.trim()));
return secrets.some((secret) => {
const expected = Buffer.from(
createHmac('sha256', secret).update(rawBody).digest('hex'),
);
return received.some(
(sig) => sig.length === expected.length && timingSafeEqual(sig, expected),
);
});
}Once the old key is gone, so is its signature
Expiring a key removes it from X-Signatures straight away. Keep the old secret
in your handler until then and remove it afterwards; holding it a little longer
does no harm.
On Webhooks v2
Webhooks v2 sends only X-Signature, signed with your newest key, so what
you receive changes the moment you add one. Since the new secret only exists
once the key is added, a v2 endpoint cannot rotate without a short window in
which deliveries carry a signature your handler does not know yet.
Keep the window short, or move to v3
Add the key, deploy its secret straight away, then expire the old key. Deliveries that fail in between are retried on the published schedule, so a quick deploy loses nothing. To rotate with no window at all, switch the endpoint to Webhooks v3 on its page in the console.
What a signature does not prove
It proves the payload came from us and that nothing changed on the way. It does not prove the request is recent, so a captured delivery replayed later still verifies.
The timestamp in the envelope does not close that gap. It records when the
webhook was built, so every retry of one delivery carries the same value. It
tells you when the event happened, not when this request was sent.
Make your handling idempotent instead
Key on what the payload identifies and treat a repeat as doing nothing. You need
that anyway: a lost 2xx makes us retry something you already processed, so
duplicates are normal rather than an attack. See
delivery and retries.
Common mistakes
- Re-serialising the parsed body instead of hashing the raw bytes. Easily the most common cause of a signature that never matches.
- Comparing with
==,===orString.equals. Use the constant-time comparison your language provides. Every example above names it. - Expecting a prefix or a timestamp in the header. The value is bare hex.
- On Webhooks v3, checking only
X-Signatureduring a rotation. It carries the newest key alone;X-Signaturesis the header that covers both.