Overview
Lemon Squeezy is a merchant of record service that allows you to accept payments from your customers by integrating with its API. It's a perfect service for subscription cases. Here's a full checkout flow:
Setup
You need to add more environment variables to your project by following these steps:
-
Create API Key
Please create an account and get the API key from Settings > API Keys.
-
Add Webhook
Then, add the webhook URL to your project from Settings > Webhooks. For subscription cases, you need to listen to the
subscription_created
andsubscription_updated
events.code
-
Create Product
Create a product from the Products menu. After filling in the form, you will have a shareable URL and the product ID.
-
Update Environment Variables
Add the following environment variables to
.env
file with the API key and product ID:.env
Done! You may need to restart the server to apply the changes. And, you can apply for store activation when your landing page is ready.
Client Side
Create a checkout method in the pricing page (app/(landing)/pricing/page.tsx
) to hit the order API and redirect the user to the payment page.
page.tsx
Make sure to append the checkout[custom][order_id]
parameter to the shareable URL to identify the order ID.
API Endpoint
Here's an example of the API route to create an order in the app/api/orders/route.ts
:
route.ts
Webhook Handler
Lastly, you need to create a webhook handler in the app/api/webhooks/lemonsqueezy/route.ts
to listen to the events. Here's an example of how to handle the subscription events:
route.ts
The other handlers from the LemonSqueezy
class are:
onOrderCreated
: handler for an order is createdonLicenseKeyCreated
: handler for a license key activation by webhookactivate
: activate a license key with manual user input