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-idsvix-timestampsvix-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 |
Get your secret
Section titled “Get your secret”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.
Verify a request
Section titled “Verify a request”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.
Framework examples
Section titled “Framework examples”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);});import express from 'express';import { verihookExpress, type VerihookRequestAdditions } from 'verihook/express';
const app = express();
// Register before express.json(), or keep express.json() off this routeapp.post('/webhooks/clerk', verihookExpress('clerk', process.env.CLERK_WEBHOOK_SIGNING_SECRET!), (req, res) => { const { eventType, event } = (req as typeof req & VerihookRequestAdditions).verihook!; res.json({ received: true });});import Fastify from 'fastify';import { verihookFastify, verihookRawBody, type VerihookFastifyRequest } from 'verihook/fastify';
const app = Fastify();await app.register(verihookRawBody);
app.post('/webhooks/clerk', { preHandler: verihookFastify('clerk', process.env.CLERK_WEBHOOK_SIGNING_SECRET!) }, async (request) => { const { eventType, event } = (request as typeof request & VerihookFastifyRequest).verihook!; return { received: true };});import { Hono } from 'hono';import { verihookHono, type VerihookVariables } from 'verihook/hono';
const app = new Hono<{ Bindings: { CLERK_WEBHOOK_SIGNING_SECRET: string }; Variables: VerihookVariables }>();
app.post('/webhooks/clerk', verihookHono('clerk', (c) => c.env.CLERK_WEBHOOK_SIGNING_SECRET), (c) => { const { eventType, event } = c.get('verihook'); return c.json({ received: true });});// main.ts: NestFactory.create(AppModule, { rawBody: true })import { Controller, Post, Req, UseGuards } from '@nestjs/common';import { createVerihookGuard, type VerihookNestRequest } from 'verihook/nestjs';
@Controller('webhooks')export class WebhooksController { @Post('clerk') @UseGuards(createVerihookGuard('clerk', process.env.CLERK_WEBHOOK_SIGNING_SECRET!)) handle(@Req() req: VerihookNestRequest) { const { eventType, event } = req.verihook!; }}import { createWebhookHandler } from 'verihook/h3';
export default defineEventHandler( createWebhookHandler('clerk', process.env.CLERK_WEBHOOK_SIGNING_SECRET!, async (payload, result) => { console.log(result.eventType); }),);import { env } from '$env/dynamic/private';import { createWebhookHandler } from 'verihook/sveltekit';
export const POST = createWebhookHandler('clerk', () => env.CLERK_WEBHOOK_SIGNING_SECRET, async (payload, result) => { console.log(result.eventType);});import { createWebhookHandler } from 'verihook/remix';
export const action = createWebhookHandler('clerk', process.env.CLERK_WEBHOOK_SIGNING_SECRET!, async (payload, result) => { console.log(result.eventType);});import { createWebhookHandler } from 'verihook/astro';
export const prerender = false;export const POST = createWebhookHandler('clerk', import.meta.env.CLERK_WEBHOOK_SIGNING_SECRET, async (payload, result) => { console.log(result.eventType);});import { createWebhookHandler } from 'verihook/lambda';
export const handler = createWebhookHandler('clerk', process.env.CLERK_WEBHOOK_SIGNING_SECRET!, async (payload, result) => { console.log(result.eventType);});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.
Things to know
Section titled “Things to know”verifyClerklives inverihook/svixtogether with Svix and Resend.- If you use Clerk middleware, make the webhook route public so it doesn’t require a session.
Testing
Section titled “Testing”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 === trueFrom 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.
Troubleshooting
Section titled “Troubleshooting”Failed results carry a code and often a hint with the likely cause. See Troubleshooting for each error code.