LemonSqueezy Subscriptions

Set up recurring subscriptions with LemonSqueezy. This tutorial shows you how to create and manage subscriptions.

Create a subscription

1import { createCheckout } from '@/libs/lemonsqueezy';
2
3export async function createSubscription(userId: string) {
4  const checkout = await createCheckout({
5    storeId: process.env.LEMONSQUEEZY_STORE_ID!,
6    variantId: 'your-variant-id',
7    customPrice: 1999, // $19.99 in cents
8    productOptions: {
9      name: 'Pro Plan',
10      description: 'Monthly subscription',
11    },
12    checkoutOptions: {
13      embed: false,
14      media: false,
15    },
16  });
17
18  return checkout.data.attributes.url;
19}

Handle webhooks

app/api/webhooks/lemonsqueezy/route.ts
1import { NextRequest, NextResponse } from 'next/server';
2
3export async function POST(req: NextRequest) {
4  const body = await req.text();
5  const signature = req.headers.get('x-signature');
6
7  // Verify webhook signature
8  const isValid = verifyWebhook(body, signature);
9  if (!isValid) {
10    return NextResponse.json({ error: 'Invalid signature' }, { status: 401 });
11  }
12
13  const event = JSON.parse(body);
14  
15  if (event.meta.event_name === 'subscription_created') {
16    // Handle new subscription
17    await handleSubscriptionCreated(event.data);
18  }
19
20  return NextResponse.json({ received: true });
21}

Learn more in the Payments documentation.