# How to verify GitHub webhook signatures (X-Hub-Signature-256) in Node.js

> Verify X-Hub-Signature-256 in Node.js and Next.js with the raw body, deduplicate on X-GitHub-Delivery, and recover deliveries GitHub never retries.

- Published 2026-11-29, updated 2026-11-29
- URL: https://softreuse.com/guides/verify-github-webhook-signature-nodejs

**Short answer:** compute `sha256=` + hex(HMAC-SHA256(secret, raw body)) and compare it with `X-Hub-Signature-256` in constant time, before parsing the JSON. Then deduplicate on `X-GitHub-Delivery` and answer within 10 seconds, because GitHub does not retry failed deliveries.

## What GitHub sends

| Header | Meaning |
|---|---|
| `X-Hub-Signature-256` | `sha256=` followed by the hex HMAC-SHA256 of the raw body, keyed with your webhook secret. Absent if the webhook has no secret. |
| `X-GitHub-Delivery` | A GUID for the delivery. Stays the same when you redeliver it. |
| `X-GitHub-Event` | The event name: `push`, `pull_request`, `ping` (sent when the webhook is created), … |
| `X-Hub-Signature` | Legacy SHA-1 signature. Ignore it. |

There is no timestamp in the signature, so the delivery id is your protection against replays.

## Verifying in a Next.js route handler

```ts
// app/api/webhooks/github/route.ts
import { createHmac, timingSafeEqual } from "node:crypto";

export const runtime = "nodejs";

export async function POST(request: Request) {
  const raw = await request.text(); // exact bytes; parse only after verifying
  const header = request.headers.get("x-hub-signature-256") ?? "";
  const expected = "sha256=" + createHmac("sha256", process.env.GITHUB_WEBHOOK_SECRET!).update(raw).digest("hex");
  const a = Buffer.from(header), b = Buffer.from(expected); // timingSafeEqual throws on different lengths
  if (a.length !== b.length || !timingSafeEqual(a, b)) return new Response("invalid signature", { status: 401 });
  const delivery = request.headers.get("x-github-delivery")!;
  const event = request.headers.get("x-github-event")!;
  const payload = JSON.parse(raw);
  // store (delivery, event, payload) with a unique key on delivery, answer, then process
  return new Response(null, { status: 202 });
}
```

In Express, use `express.raw({ type: "application/json" })` on the webhook route (not `express.json()`) and verify `req.body`, which is then a Buffer. If the webhook's content type is `application/x-www-form-urlencoded`, GitHub signs the form-encoded body, so verify those bytes before decoding the `payload` field.

## Why signatures fail

- **The body changed.** Anything that parses the JSON and serialises it again changes whitespace and key order. Verify the raw bytes.
- **The secret differs.** Repository, organization and GitHub App webhooks each have their own secret. A trailing newline in an environment variable is enough to break it.
- **You compared the wrong header.** `X-Hub-Signature` is SHA-1; compute SHA-256 and compare with `X-Hub-Signature-256`.

Paste the secret, header and body into the [webhook signature debugger](https://softreuse.com/tools/webhook-signature-debugger) to see which one it is. It runs in your browser.

## Deliveries GitHub won't retry

GitHub marks a delivery as failed when your server is down or takes longer than **10 seconds**, and [does not redeliver it automatically](https://docs.github.com/en/webhooks/using-webhooks/handling-failed-webhook-deliveries). Two consequences:

1. **Answer fast.** Verify, store the event durably, return `2xx`, and do the slow work afterwards.
2. **Recover missed ones.** Redeliver from the webhook's Recent Deliveries page, or run a scheduled job that lists failed deliveries through the REST API and redelivers them. A redelivery carries the same `X-GitHub-Delivery`, so a unique key on it keeps redeliveries of already-processed events harmless.

## With Reliable Webhooks

[Reliable Webhooks](https://softreuse.com/products/reliable-webhooks) has a GitHub provider that does the verification above and deduplicates on `X-GitHub-Delivery`, stores each event in Postgres before answering, and retries your handler with backoff and a dead-letter state:

```ts
import { Pool } from "pg";
import { createWebhookReceiver, githubProvider, PostgresStore } from "@softreuse/webhook-kit";

const github = createWebhookReceiver({
  provider: githubProvider({ secret: process.env.GITHUB_WEBHOOK_SECRET! }), // or [new, old] while rotating
  store: new PostgresStore(new Pool({ connectionString: process.env.DATABASE_URL })),
  handlers: {
    push: async ({ event, idempotencyKey }) => { /* runs at least once; use idempotencyKey for side effects */ },
    "*": () => {}, // ping and anything you don't handle
  },
});

// app/api/webhooks/github/route.ts
export const POST = (request: Request) => github.handle(request);
```

## Sources

- [GitHub: Validating webhook deliveries](https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries)
- [GitHub: Best practices for using webhooks](https://docs.github.com/en/webhooks/using-webhooks/best-practices-for-using-webhooks)
- [GitHub: Handling failed webhook deliveries](https://docs.github.com/en/webhooks/using-webhooks/handling-failed-webhook-deliveries)

## Frequently asked questions

### How does GitHub sign webhooks?

With HMAC-SHA256 of the raw request body, keyed with the webhook's secret, sent hex-encoded as X-Hub-Signature-256: sha256=<hex>. The header is only sent when the webhook has a secret configured. The older X-Hub-Signature header uses SHA-1 and exists for legacy integrations.

### Does GitHub retry failed webhook deliveries?

No. GitHub records a delivery as failed if your server is down or takes longer than 10 seconds to answer, and does not redeliver it automatically. You redeliver from the webhook's Recent Deliveries page or with the REST API, so store events quickly and answer within the 10 seconds.

### How do I stop processing the same GitHub delivery twice?

Keep the X-GitHub-Delivery GUID with a unique constraint and skip deliveries you have already stored. GitHub keeps the same X-GitHub-Delivery value when you redeliver, so a redelivery of an event you already processed is recognised as a duplicate.

### Why does X-Hub-Signature-256 not match?

Usually because the body was parsed and re-serialised before verification, or the secret in your environment differs from the one in the webhook settings (watch for trailing newlines). Verify the exact bytes received. The webhook signature debugger on softreuse.com shows which of these it is.
