LemonSqueezy
Introduction
Follow this guide to get set up with LemonSqueezy.
Create LemonSqueezy Account
- 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
- Create a new API key in the API keys page (opens in a new tab).
- In the
.env.local
file of your project, add the key to theLEMON_SQUEEZY_API_KEY
variable.
Getting the Store ID
- 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:
LEMON_SQUEEZY_STORE_ID=11111
Creating Products and Variants
-
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.
-
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
andLEMON_SQUEEZY_VARIANT_ID_2
variables in the.env.local
file of your project.
Changing the Project Files
- 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):Basically, you will be commenting out the stripe/utils line and uncommenting the lemonsqueezy/utils line./app/api/webhook/route.ts// import * as utils from "@/app/api/stripe/utils"; import * as utils from "@/app/api/lemonsqueezy/utils";
- Do the same in the
/app/api/webhook/payments.tsx
file. - Uncomment the entire
/app/api/lemonsqueezy/utils.ts
file. - 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),
- 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
- 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".
- 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 example: www.myapp.com/api/webhook (opens in a new tab)
- If you are using localhost, you can use a tunneling tool like Tunnelmole (opens in a new tab) or ngrok (opens in a new tab) to expose your local server to the internet. Just be sure to add
/api/webhook
at the end of your tunneled URL.
- 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
- For Callback URL, be sure to add
- 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.