Receive Lemon Squeezy Webhooks in Next.js
Learn how to receive Lemon Squeezy webhooks in Next.js.
In this example we will create a very simple API endpoint in Next.js to receive Lemon Squeezy webhook events. This endpoint will check the webhook signature to make sure it’s a legitimate request from Lemon Squeezy.
Create a new Next.js project
First, create a new Next.js project:
Read the Next.js installation docs for more information.
In this example we have opted-in to use src
folder.
Create a route handler
Create a new route handler in Next.js in the src/app/api/webhooks
folder. The file should be called route.ts
:
This endpoint will be available on the api/webhooks
route.
For more information, refer to the Next.js docs
Verify the webhook signature
The first thing the webhook handler should do is verify the webhook signature. This ensures that the webhook request is legitimate and not from a malicious source. We do this by veryfing the X-Signature
header.
Read more about verifying signed requests.
Parse the webhook data
After verifying the webhook signature, you can parse the webhook data. The webhook data is sent as JSON in the request body.
Create a webhook in Lemon Squeezy
Now we need to create a webhook in Lemon Squeezy. Go to Settings » Webhook and create a new webhook. Use the URL of your app. The webhook endpoint will be at /api/webhook
.
Then select the events you want to be sent to your webhook route. If you’re selling digital products, maybe all you need is order_created
. If you’re selling subscriptions, you can enable subscription_*
events.
Add environment variables
As a final step, add LEMONSQUEEZY_WEBHOOK_SECRET
to your .env
file, and set the Lemon Squeezy webhook secret, that you added when created the webhook, as the value.
That’s it! You now have a webhook handler in Next.js that can receive Lemon Squeezy webhook events.