Verify Calendly webhooks
Verify that a webhook really came from Calendly before acting on it.
| Identifier | 'calendly' |
| Import | import { verifyCalendly } from 'verihook/calendly' |
| Headers | calendly-webhook-signature |
| Signature | HMAC-SHA256 of <t>.<raw body>, sent as t=<timestamp>,v1=<hex>. |
| Replay window | 180 seconds (options.tolerance) |
result.eventType |
the body’s event, e.g. invitee.created |
Get your secret
Section titled “Get your secret”The signing key you passed when creating the webhook subscription through the API (signing_key).
Store it in an environment variable (CALENDLY_WEBHOOK_SIGNING_KEY below) and never commit it.
Verify a request
Section titled “Verify a request”import { verifyCalendly } from 'verihook/calendly';
export async function POST(request: Request) { const result = await verifyCalendly(request, process.env.CALENDLY_WEBHOOK_SIGNING_KEY!);
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('calendly', 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('calendly', process.env.CALENDLY_WEBHOOK_SIGNING_KEY!, 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/calendly', verihookExpress('calendly', process.env.CALENDLY_WEBHOOK_SIGNING_KEY!), (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/calendly', { preHandler: verihookFastify('calendly', process.env.CALENDLY_WEBHOOK_SIGNING_KEY!) }, 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: { CALENDLY_WEBHOOK_SIGNING_KEY: string }; Variables: VerihookVariables }>();
app.post('/webhooks/calendly', verihookHono('calendly', (c) => c.env.CALENDLY_WEBHOOK_SIGNING_KEY), (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('calendly') @UseGuards(createVerihookGuard('calendly', process.env.CALENDLY_WEBHOOK_SIGNING_KEY!)) handle(@Req() req: VerihookNestRequest) { const { eventType, event } = req.verihook!; }}import { createWebhookHandler } from 'verihook/h3';
export default defineEventHandler( createWebhookHandler('calendly', process.env.CALENDLY_WEBHOOK_SIGNING_KEY!, async (payload, result) => { console.log(result.eventType); }),);import { env } from '$env/dynamic/private';import { createWebhookHandler } from 'verihook/sveltekit';
export const POST = createWebhookHandler('calendly', () => env.CALENDLY_WEBHOOK_SIGNING_KEY, async (payload, result) => { console.log(result.eventType);});import { createWebhookHandler } from 'verihook/remix';
export const action = createWebhookHandler('calendly', process.env.CALENDLY_WEBHOOK_SIGNING_KEY!, async (payload, result) => { console.log(result.eventType);});import { createWebhookHandler } from 'verihook/astro';
export const prerender = false;export const POST = createWebhookHandler('calendly', import.meta.env.CALENDLY_WEBHOOK_SIGNING_KEY, async (payload, result) => { console.log(result.eventType);});import { createWebhookHandler } from 'verihook/lambda';
export const handler = createWebhookHandler('calendly', process.env.CALENDLY_WEBHOOK_SIGNING_KEY!, 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”- Subscriptions created without a
signing_keyaren’t signed. Create the subscription with one.
Testing
Section titled “Testing”signWebhook from verihook/testing builds a correctly signed Calendly request for your tests:
import { signWebhook } from 'verihook/testing';import { verifyCalendly } from 'verihook/calendly';
const hook = await signWebhook('calendly', { secret: 'calendly_signing_key', payload: { id: 'evt_test' },});
const result = await verifyCalendly( { headers: hook.headers, body: hook.body, url: hook.url }, hook.secret,);// result.valid === trueFrom the command line, npx verihook simulate calendly --url http://localhost:3000/webhooks/calendly 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.