Verify webhooks
Verify the HMAC-SHA256 signature on a onefold webhook delivery before you trust it, with examples in TypeScript and Python.
Verify the signature of every webhook delivery before you act on it. Anyone who learns your endpoint's URL can send it a request; only onefold can produce a valid signature.
The signature
onefold signs each delivery with the endpoint's signing secret:
signature = HMAC-SHA256(secret, timestamp + "." + body)| Part | Value |
|---|---|
secret | The endpoint's signing secret: the whole string, including the eitozx_whsec_ prefix |
timestamp | The X-Webhook-Timestamp header, exactly as sent |
body | The raw request body, byte for byte |
| Result | Hex-encoded, in the X-Webhook-Signature header |
The checks
- Read the raw body. Compute the signature over the bytes you received. Do not parse the JSON and serialize it again; the result differs and the signature fails.
- Compute and compare. Use a constant-time comparison.
- Check the timestamp. Reject a delivery whose timestamp is more than five minutes from your server's clock. onefold does not enforce this for you; it is what stops a captured delivery from being sent again later.
import { createHmac, timingSafeEqual } from "node:crypto";
export function verifyWebhook(
rawBody: string,
headers: Headers,
secret: string,
): boolean {
const timestamp = headers.get("x-webhook-timestamp") ?? "";
const signature = headers.get("x-webhook-signature") ?? "";
if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
const expected = createHmac("sha256", secret)
.update(`${timestamp}.${rawBody}`)
.digest("hex");
return (
signature.length === expected.length &&
timingSafeEqual(Buffer.from(signature), Buffer.from(expected))
);
}Getting the raw body
Most frameworks parse JSON before your handler runs. Read the unparsed body instead:
| Framework | Raw body |
|---|---|
| Next.js route handler | await request.text() |
| Express | express.raw({ type: "application/json" }) on the webhook route |
| Django | request.body |
| Flask | request.get_data() |
Headers on every delivery
| Header | Value |
|---|---|
X-Webhook-Event | The event name, for example consent.granted |
X-Webhook-Timestamp | Unix time in seconds when the delivery was signed |
X-Webhook-Signature | The hex signature |
X-Webhook-Delivery | The ID of this delivery |
X-Webhook-Event-Id | The ID of the original event |
When verification fails
| Cause | Fix |
|---|---|
| The body was parsed and re-serialized | Sign the raw bytes |
| The wrong secret | Use this endpoint's signing secret, not the client secret or another endpoint's |
The eitozx_whsec_ prefix was removed | Use the whole secret |
| The secret was rotated | Use the new secret |
Webhooks
Receive an HTTP request from onefold when a user authorizes your application, signs in, changes, is blocked, or leaves, and handle deliveries, retries and replays.
Management API
Use an API key to read your application's users, metadata, sign-up answers, analytics and activity from your server with the onefold management API.