> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shipthing.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lemonsqueezy

> Learn how to set up Lemonsqueezy with ShipThing.

[Lemon Squeezy](https://www.lemonsqueezy.com/) is an all-in-one platform for running your SaaS business. It handles payments, subscriptions, global tax compliance, fraud prevention, multi-currency, and more. Here’s how to switch the default payment processor from Stripe to Lemon Squeezy.

## Get the api key

After you have created your account for Lemonsqueezy, you will need to get the API key. You can do this by going to the [API page](https://app.lemonsqueezy.com/settings/api) in the dashboard. Here you will find the `Secret key` and the `Publishable key`. You will need the `Secret key` for the integration to work.

### Add environment variables

To use the Lemonsqueezy integration, you need to define the following environment variables to your `.env.local` as well as your production environment:

```env .env.local theme={"system"}
LEMON_SQUEEZY_API_KEY=
LEMON_SQUEEZY_WEBHOOK_SECRET=
```

## In-App Purchases

You can use Lemonsqueezy anywhere in your application by importing a `lemonsqueezy` object:

```ts page.tsx theme={"system"}
import { lemonsqueezy } from '@repo/payments';
```

### Creating a Checkout Session

We need to create a checkout session to charge your users , and you only need modify some code. It consists of two parts:

* Client

It sends a request to create a session to the `/api/lemonsqueezy/checkout` endpoint.

```tsx apps/web/[locale]/components/payment/price-form.tsx theme={"system"}
      const response = await fetch('/api/lemonsqueezy/checkout', { // change to `/api/lemonsqueezy/checkout`
        method: 'POST',
        body: JSON.stringify({
          priceId: item.priceId,
          interval: item.interval,
        }),
      });
```

* Server

Implementing business logic in `apps/web/api/lemonsqueezy/checkout/route.ts`.

<Tip>
  Lemonsqueezy allows using the product's [Purchase Link](https://docs.lemonsqueezy.com/guides/developer-guide/taking-payments#creating-checkouts-from-the-lemon-squeezy-dashboard) without creating a checkout session through the API.
</Tip>

## Webhooks

### Create a webhook

To configure a new webhook, go to the [Webhooks page](https://app.lemonsqueezy.com/settings/webhooks) in the lemonsqueezy dashboard. Click the Add endpoint button and select at least 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:

```sh Terminal theme={"system"}
openssl rand -base64 32
```

### Webhook Handler

Stripe webhooks are handled in the `POST /webhooks/lemonsqueezy` route in the `apps/api` app. This route constructs the event and then switches on the event type to determine how to process the event.

<Note>
  We have implemented the basic structure of the `Webhook` handler for you. You can modify it as needed.
</Note>
