Skip to content

Verify GitHub webhooks

Verify that a webhook really came from GitHub before acting on it.

Identifier 'github'
Import import { verifyGitHub } from 'verihook/github'
Headers x-hub-signature-256
x-hub-signature (SHA-1, legacy)
x-github-event
x-github-delivery
Signature HMAC-SHA256 of the raw body, sent as sha256=<hex>.
Replay window None (the provider sends no timestamp)
result.eventType the x-github-event header, e.g. push or issues

The secret you typed when creating the webhook: Settings → Webhooks on the repository or organization, or the webhook secret of your GitHub App. GitHub never shows it again, so set a new one if you lost it.

Store it in an environment variable (GITHUB_WEBHOOK_SECRET below) and never commit it.

import { verifyGitHub } from 'verihook/github';
export async function POST(request: Request) {
const result = await verifyGitHub(request, process.env.GITHUB_WEBHOOK_SECRET!);
if (!result.valid) {
console.warn(result.code, result.reason, result.hint);
return new Response('Invalid signature', { status: 401 });
}
console.log(result.eventType, result.event);
return Response.json({ received: true });
}

request can be a Fetch Request or { headers, body, url? } with the raw body. verifyWebhook('github', request, secret) from verihook does the same. Read why the raw body matters if verification fails behind a body parser.

app/api/webhooks/github/route.ts
import { createWebhookHandler } from 'verihook/next';
export const POST = createWebhookHandler('github', process.env.GITHUB_WEBHOOK_SECRET!, async (payload, result) => {
// Runs only for a valid signature
console.log(result.eventType, result.event);
});

Each adapter responds 401 to an invalid signature, 413 to a body over maxBodySize (2 MB by default) and 200 to a duplicate when you pass a dedupeStore.

  • GitHub sends a ping event when you create the webhook. Return 2xx for it.
  • GitHub doesn’t sign the x-github-delivery header, so a dedupe store keys GitHub events on a SHA-256 of the signed body instead. A redelivered event has the same body and is detected as a duplicate.

signWebhook from verihook/testing builds a correctly signed GitHub request for your tests:

import { signWebhook } from 'verihook/testing';
import { verifyGitHub } from 'verihook/github';
const hook = await signWebhook('github', {
secret: 'github_test_secret',
payload: { id: 'evt_test' },
});
const result = await verifyGitHub(
{ headers: hook.headers, body: hook.body, url: hook.url },
hook.secret,
);
// result.valid === true

From the command line, npx verihook simulate github --url http://localhost:3000/webhooks/github sends one to your local server.

See Testing your handlers for supertest and Fetch Request examples.

Failed results carry a code and often a hint with the likely cause. See Troubleshooting for each error code.

GitHub’s webhook documentation