Next.js Starter Kit
LemonSqueezy

LemonSqueezy

Introduction

Follow this guide to get set up with LemonSqueezy.

Create LemonSqueezy Account

  1. First, create a LemonSqueezy account (opens in a new tab) if you don't have one already. Follow the onboarding instructions to set up your LemonSqueezy account.

Getting the API Key

  1. Create a new API key in the API keys page (opens in a new tab).
  2. In the .env.local file of your project, add the key to the LEMON_SQUEEZY_API_KEY variable.

Getting the Store ID

  1. Get the Store ID in the Stores Settings page (opens in a new tab). Copy the number without the # sign, and paste it into LEMON_SQUEEZY_STORE_ID in .env.local.

For example, if your store ID is #11111, it should look like:

.env.local
LEMON_SQUEEZY_STORE_ID=11111

Creating Products and Variants

  1. For your user to choose between different subscription plans, you will need to create a Product first on the Products page (opens in a new tab). Click "+ New Product" and go down to the Variants section. Here, you will add a variant for each subscription plan you want to offer. For example, if you have a $9/month plan and a $29/month plan, you will be adding 2 variants, one for each plan.

  2. After you filled out the details and saved the product, click on the Product, go to the Variants section, copy the IDs for each variant, and paste them into the LEMON_SQUEEZY_VARIANT_ID_1 and LEMON_SQUEEZY_VARIANT_ID_2 variables in the .env.local file of your project.

Changing the Project Files

  1. In your RyzeKit project, go to /app/api/webhook/route.ts and make the following change at the top of the file (in the imports section):
    /app/api/webhook/route.ts
    // import * as utils from "@/app/api/stripe/utils";
    import * as utils from "@/app/api/lemonsqueezy/utils";
    Basically, you will be commenting out the stripe/utils line and uncommenting the lemonsqueezy/utils line.
  2. Do the same in the /app/api/webhook/payments.tsx file.
  3. Uncomment the entire /app/api/lemonsqueezy/utils.ts file.
  4. In the /lib/db/schema.ts file, comment out the Stripe fields and uncomment the LemonSqueezy fields. It should look something like this:
    /lib/db/schema.ts
    // // Stripe fields
    // stripeCustomerId: varchar("stripe_customer_id", { length: 256 })
    //   .notNull()
    //   .unique(),
    // stripeSubscriptionId: varchar("stripe_subscription_id", {
    //   length: 256,
    // }).unique(),
    // stripePriceId: varchar("stripe_price_id", { length: 256 }),
    // stripeCurrentPeriodEnd: timestamp("stripe_current_period_ended_at"),
     
    // LemonSqueezy fields
    lemonsqueezyCustomerId: varchar("lemonsqueezy_customer_id", { length: 256 })
        .notNull()
        .unique(),
    lemonsqueezyVariantId: varchar("lemonsqueezy_variant_id", { length: 256 }),
    isPaidUser: boolean("is_paid_user").default(false),
  5. In your terminal, run the following command to update the database with the new schema. Override the existing database if necessary.
pnpm drizzle-kit push

Creating a Webhook

  1. To update the current subscription status of a user and other information in your database, you will need to set up a LemonSqueezy webhook. First, go to the Webhooks page (opens in a new tab) and click "+ Add endpoint".
  2. Next, fill in the details to create the webhook endpoint.
    • For Callback URL, be sure to add /api/webhook to the end of your website name.
    • For Signing secret, input a random string. Then, copy and paste this string into LEMON_SQUEEZY_WEBHOOK_SIGNATURE in the .env.local file of your project.
      • You can generate a random string by running the following command in your terminal.
      openssl rand -base64 32
    • For What updates should we send?, choose the following events:
      • order_created
      • subscription_cancelled
  3. If you are in test mode, you can try purchasing the product or subscription plan with a test payment method (opens in a new tab). In the webhook page, you will then be able to see whether the webhook was configured successfully.

Next Steps

🚀 Congratulations, you've successfully set up payments! Proceed to the next page to continue with setup.