Verifying signatures
Check that a webhook came from the gateway and was not replayed.
Every webhook request is signed with your endpoint's secret. Verify the signature before you act
on an event: anyone can send a POST to your URL.
The headers#
| Header | Value |
|---|---|
hixel-signature | t=<unix seconds>,v1=<signature>, with a second v1 while a rotated secret still signs |
hixel-event-id | The event's id |
hixel-event-type | The event's type |
content-type | application/json |
The algorithm#
- Read the raw request body as bytes, before any JSON parsing. A parser may reorder keys or change whitespace, and the signature is over the exact bytes.
- Split
hixel-signatureon,, and each part on its first=. Taketand everyv1. - Compute HMAC-SHA256 of
<t>.<raw body>(the timestamp, a dot, the body) with the endpoint's whole secret,whsec_included, as the key. Write it as lowercase hex. - Accept the request if it equals any
v1, compared in constant time. While a rotated secret is in its grace period there are twov1values, one per secret, so your old and your new secret both verify. - Refuse it if
tis more than 5 minutes away from your clock, in either direction. A captured request cannot be replayed later, and a retry is signed afresh with a newt.
In code#
import { createHmac, timingSafeEqual } from "node:crypto";
/** Whether `rawBody` was signed with `secret` within `toleranceSeconds`. */
export function verify(
rawBody: string,
header: string,
secret: string,
toleranceSeconds = 300,
): boolean {
const parts = header.split(",").map((part) => {
const at = part.indexOf("=");
return [part.slice(0, at), part.slice(at + 1)] as const;
});
const t = Number(parts.find(([name]) => name === "t")?.[1]);
if (!Number.isInteger(t) || Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false;
const expected = createHmac("sha256", secret).update(`${t}.${rawBody}`).digest();
return parts
.filter(([name]) => name === "v1")
.some(([, hex]) => {
const given = Buffer.from(hex, "hex");
return given.length === expected.length && timingSafeEqual(given, expected);
});
}import { verifyWebhook, WebhookVerificationError } from "@hixel/sdk";
/** A Fetch-style handler: Workers, Bun, Deno, Hono, Next.js route handlers. */
export async function POST(request: Request): Promise<Response> {
try {
// Reads the raw body and `hixel-signature`, and checks both steps above.
const event = await verifyWebhook(request, { secret: process.env.HIXEL_WEBHOOK_SECRET });
console.log(event.id, event.type); // queue it, then answer
} catch (error) {
if (error instanceof WebhookVerificationError) return new Response(null, { status: 400 });
throw error;
}
return new Response(null, { status: 200 });
}With the Node SDK, hixel.webhooks.verify(request) (or
verifyWebhook(request, { secret })) does all five steps, accepts both secrets while you rotate
({ secret: [newSecret, oldSecret] }), and returns the event typed by its type.
In a framework that parses JSON for you, make sure the handler gets the raw body: in Express,
express.raw({ type: "application/json" }) on the webhook route; in a Fetch-style handler
(Next.js route handlers, Hono, Workers), await request.text().
A test vector#
Check your implementation against this request. With the secret
whsec_5Wq3n0xYdR8mJ2kLp7TzA4vHc9GbE1sUfK6iNoQ3rXythe timestamp 1795600800 (25 November 2026, 10:00:00 UTC) and this exact body, one line with no
trailing newline, as a delivery sends it:
{"id":"evt_q4f1x9k2m7c3v8b5n6z0a1s2","object":"event","type":"webhook.test","api_version":"2026-11-25","created_at":"2026-11-25T10:00:00.000Z","environment":"env_k3j9x2m8q1w7e4r5t6y0u2i3","data":{"endpoint_id":"we_h7g6f5d4s3a2p1o0i9u8y7t6","message":"A test event, sent because someone asked for one."}}the header is
hixel-signature: t=1795600800,v1=de26799405846a97d0f7ab4aea94940b70690dc6f22ef39effe6902106ebd9aa(Your clock check will refuse it, of course: test the HMAC without the tolerance, or with your clock set to that time.)
Rotating the secret#
POST /v1/webhook_endpoints/{id}/rotate_secret returns a new secret. For
grace_period_seconds (24 hours by default, 0 to 7 days) both secrets sign, so deploy the new
secret within that time. Accepting any matching v1, as above, makes the switch seamless.