Skip to content

Verify Lemon Squeezy webhooks

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

Identifier 'lemonsqueezy'
Import import { verifyLemonSqueezy } from 'verihook/lemonsqueezy'
Headers x-signature
x-event-name
Signature HMAC-SHA256 of the raw body, hex-encoded.
Replay window None (the provider sends no timestamp)
result.eventType meta.event_name in the body, e.g. order_created

The signing secret you entered when creating the webhook under Settings → Webhooks.

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

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

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

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

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

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

Lemon Squeezy’s webhook documentation