The header
The signature is computed like this:
How to verify
1
Read the raw body
Use the bytes exactly as they arrived. If you parse and serialize the JSON again, the bytes change and the signature does not match.
2
Check the timestamp
Reject the delivery if
t is more than 5 minutes away from the current time. This protects against a captured request being replayed.3
Compute the HMAC
Compute the HMAC-SHA256 of
"{t}.{raw_body}" with the endpoint secret.4
Compare in constant time
Compare with each
v1 value of the header using a constant-time comparison. One match is enough.With the SDK
constructEvent does all of this, makes no network calls and returns the typed event.
Without the SDK
Secret rotation
To change the secret without losing events, callPOST /v1/webhook_endpoints/{id}/rotate_secret. The response carries the new secret.
- For 24 hours, both secrets (the new one and the previous one) sign deliveries, and the header carries two
v1=values. - During that time, validate with both secrets. The SDK accepts a list:
constructEvent(rawBody, header, [newSecret, previousSecret]). - After the window, the old secret stops being valid.
Common problems
The signature never matches
The signature never matches
It is almost always the body: a middleware already parsed the JSON and you are using its serialization. Configure your framework to expose the raw body on the webhook route (
express.raw({ type: 'application/json' }), for example).It works in tests but fails in production
It works in tests but fails in production
Check that you use the secret of the right endpoint. Each one has its own. Also check that the server clock is synchronized, since the tolerance is 5 minutes.
It fails right after rotating the secret
It fails right after rotating the secret
Validate with both the new secret and the previous one until the 24-hour window ends.