Article · Updated November 8, 2026 · By the Hooks team

How to verify a GitHub webhook signature

Short answer: GitHub signs the raw request body with HMAC-SHA256 using the webhook secret and sends the result in X-Hub-Signature-256 as sha256=<hex>. Compute the same digest over the exact bytes you received, compare it in constant time, and reject the request when they differ. Never verify against a re-serialized JSON object.

Most failed verifications come from one mistake: parsing JSON first and signing the re-encoded result. Key order, whitespace, and unicode escaping change the bytes, so the digest no longer matches.

Verify in Node.js

Read the body as bytes, hash those bytes, then compare. The comparison must take the same time whether the first byte or the last byte differs.

Node.js

import { createHmac, timingSafeEqual } from "node:crypto";

const expected = "sha256=" + createHmac("sha256", process.env.GITHUB_SECRET)
  .update(rawBody)
  .digest("hex");

const received = request.headers.get("x-hub-signature-256") ?? "";
const ok = received.length === expected.length &&
  timingSafeEqual(Buffer.from(received), Buffer.from(expected));

Deduplicate with the delivery id

GitHub retries deliveries and can send the same event twice. X-GitHub-Delivery is stable across those retries, so store it and drop duplicates before running side effects.

The tricky case is a lost response: your handler commits, the 2xx never reaches Hooks, and the retry arrives with the same delivery id. Claim the id in the same transaction as the side effect, so a repeat loses the insert race instead of repeating the work.

A gateway replay also reuses the original delivery identity. Keep the dedupe record longer than your replay window, and return 2xx for a duplicate so the sender stops retrying. A dependency-free version of this check runs with `pnpm e2e:tutorial-snippets` in the Hooks repository.

Unique claim before side effects

-- processed_deliveries(delivery_id TEXT PRIMARY KEY)
INSERT INTO processed_deliveries (delivery_id) VALUES ($1)
ON CONFLICT (delivery_id) DO NOTHING
RETURNING delivery_id;

-- No row returned: this delivery was already handled. Return 200
-- and skip the side effect. A row returned: do the work in the
-- same transaction, or outbox it and commit the claim with it.

Let the gateway do it

Hooks verifies the signature on arrival using the endpoint secret, rejects stale timestamps where the provider supplies one, and records the delivery id for dedupe. Your handler receives only verified events, and the body it gets is the one that was verified.

Frequently asked questions

Why does my signature check pass in tests but fail in production?

Tests usually sign the same string they verify. In production, a framework may parse and re-encode the body before your handler runs. Capture the raw bytes at the edge and sign those.

Does GitHub include a timestamp for replay protection?

No. GitHub's signature covers the body only, so replay protection comes from deduplicating X-GitHub-Delivery rather than rejecting old timestamps.

Try the flow without an account

Send a test webhook to a temporary endpoint and watch signature verification and forwarding. No account, card, or paid model call is involved.

Keep reading