Billing

Lemon Squeezy

Manage your customers data and subscriptions using Lemon Squeezy.

Lemon Squeezy is another billing provider available for TurboStarter. Here we'll go through the configuration and how to set it up as a provider for your app.

To switch to Lemon Squeezy you need to set it as provider in the environment variables:

apps/web/.env.local
BILLING_PROVIDER="lemon-squeezy"

Then, let's configure the integration:

Get API keys

After you have created your account and a store for Lemon Squeezy, you will need to create a new API key. You can do this by going to the API page in the settings and clicking on the plus button. You will need to give your API key a name and then click on the Create button. Once you have created your API key, you will need to copy the API key to use it in the setup of the integration.

For local development, make sure to use Test Mode to not mess with the real transactions.

Set environment variables

You need to set the following environment variables:

apps/web/.env.local
LEMONSQUEEZY_API_KEY="" # Your Lemon Squeezy API key
LEMONSQUEEZY_SIGNING_SECRET="" # Your Lemon Squeezy webhook signing secret
LEMONSQUEEZY_STORE_ID="" # Your Lemon Squeezy store ID (can be found under Settings > Stores next to your store url, e.g #12345)

Please do not add the secret keys to the .env file in production. During development, you can place them in .env.local as it's not committed to the repository. In production, you can set them in the environment variables of your hosting provider.

Create products

For your users to choose from the available subscription plans, you need to create those Products first on the Products page. You can create as many products as you want.

Create one product per plan you want to offer. You can add multiple variant within the product to offer multiple models or different billing intervals.

Lemon Squeezy Products

To offer multiple intervals for each plan, you can use the Variant feature of Lemon Squeezy. Just create one variant for each interval/model you want to offer.

Lemon Squeezy Variants

Match the variant id with configuration

You need to make sure that the price ID you set in the configuration matches the ID of the variant you created in Lemon Squeezy.

See configuration for more information.

Create a webhook

To sync the current subscription status or checkout conclusion and other information to your database, you need to set up a webhook.

The webhook handling code comes ready to use with TurboStarter, you just have to create the webhook in the Lemon Squeezy dashboard and insert the URL for your project.

To configure a new webhook, go to the Webhooks page in the Lemon Squeezy settings and click the Plus button.

Lemon Squeezy Webhook

Select the following events:

  • For subscriptions:
    • subscription_created
    • subscription_updated
    • subscription_cancelled
  • For one-off payments:
    • order_created

You will also have to enter a Signing secret which you can get by running the following command in your terminal:

openssl rand -base64 32

Copy the generated string and paste it into the Signing secret field.

You also need to add this secret to your environment variables:

apps/web/.env.local
LEMONSQUEEZY_WEBHOOK_SECRET=your-signing-secret

To get the callback URL for the webhook, you can either use a local development URL or the URL of your deployed app:

Local development

If you want to test the webhook locally, you can use ngrok to create a tunnel to your local machine. Ngrok will then give you a URL that you can use to test the webhook locally.

To do so, install ngrok and run it with the following command (while your TurboStarter web development server is running):

ngrok http 3000

Ngrok

This will give you a URL (see the Forwarding output) that you can use to create a webhook in Lemon Squeezy. Just use that url and add /api/webhooks/billing to it.

Lemon Squeezy Webhooks

docs.lemonsqueezy.com

Production deployment

When going to production, you will need to set the webhook URL and the events you want to listen to in Lemon Squeezy.

The webhook path is /api/webhooks/billing. If your app is hosted at https://myapp.com then you need to enter https://myapp.com/api/webhooks/billing as the URL.

All the relevant events are automatically handled by TurboStarter, so you don't need to do anything else. If you want to handle more events please check Webhooks for more information.

Add discount

You can add a discount for your customers that will apply on a specific price.

You can create the discount on Discounts page.

Lemon Squeezy Discounts

You can set there a details of discount such as products that it should apply to, amount off, duration, max redemptions and more.

Lemon Squeezy Discounts

lemonsqueezy.com

You need to add also the discount code and details to TurboStarter billing configuration to enable displaying it in the UI, creating checkout sessions with it and calculate prices.

See discounts configuration for more details.

That's it! 🎉 You have now set up Lemon Squeezy as a billing provider for your app.

Feel free to add more products, prices, discounts and manage your customers data and subscriptions using Lemon Squeezy.

Ensure configuration matches

Make sure that the data you set in the configuration matches the details of things you created in Lemon Squeezy.

See configuration for more information.

Last updated on

On this page

Ship your startup everywhere. In minutes.