Skip to content

Verify Clerk webhooks

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

Identifier 'clerk'
Import import { verifyClerk } from 'verihook/svix'
Headers svix-id
svix-timestamp
svix-signature
Signature Svix signatures: HMAC-SHA256 of <id>.<timestamp>.<raw body>.
Replay window 300 seconds (options.tolerance)
result.eventType the body’s type, e.g. user.created

The endpoint’s Signing Secret (whsec_...): in the Clerk Dashboard, open Webhooks and select the endpoint.

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

import { verifyClerk } from 'verihook/svix';
export async function POST(request: Request) {
const result = await verifyClerk(request, process.env.CLERK_WEBHOOK_SIGNING_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('clerk', request, secret) from verihook does the same. Read why the raw body matters if verification fails behind a body parser.

app/api/webhooks/clerk/route.ts
import { createWebhookHandler } from 'verihook/next';
export const POST = createWebhookHandler('clerk', process.env.CLERK_WEBHOOK_SIGNING_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.

  • verifyClerk lives in verihook/svix together with Svix and Resend.
  • If you use Clerk middleware, make the webhook route public so it doesn’t require a session.

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

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

From the command line, npx verihook simulate clerk --url http://localhost:3000/webhooks/clerk 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.

Clerk’s webhook documentation