# LemonSqueezy Checkout API (https://talivia.com/docs/revenue-guides/lemonsqueezy/checkout-api)



Use this flow when your backend calls `POST /v1/checkouts` and returns the generated checkout URL to the browser.

## Send the session to your backend [#send-the-session-to-your-backend]

Read the current session on the tracked page and include it in your own checkout request:

```javascript title="Start checkout from the browser"
const sessionId = window.talivia.getSessionId();

const response = await fetch('/api/create-checkout', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ sessionId }),
});

const { checkoutUrl } = await response.json();
window.location.assign(checkoutUrl);
```

Treat the session ID only as attribution context. Validate the customer, product, price, and authorization independently on your backend.

## Add custom checkout data [#add-custom-checkout-data]

Merge the received value into `attributes.checkout_data.custom` in the LemonSqueezy request:

```javascript title="Create the LemonSqueezy checkout"
const checkout = await fetch('https://api.lemonsqueezy.com/v1/checkouts', {
  method: 'POST',
  headers: {
    Accept: 'application/vnd.api+json',
    'Content-Type': 'application/vnd.api+json',
    Authorization: `Bearer ${process.env.LEMONSQUEEZY_API_KEY}`,
  },
  body: JSON.stringify({
    data: {
      type: 'checkouts',
      attributes: {
        checkout_data: {
          custom: {
            talivia_session_id: sessionId,
          },
        },
      },
      relationships: {
        store: { data: { type: 'stores', id: process.env.LEMONSQUEEZY_STORE_ID } },
        variant: { data: { type: 'variants', id: variantId } },
      },
    },
  }),
}).then(response => response.json());

return Response.json({ checkoutUrl: checkout.data.attributes.url });
```

LemonSqueezy copies custom checkout data to `meta.custom_data` in the signed webhook. This works for hosted custom checkouts and programmatically opened overlays.

Keep the LemonSqueezy API key on the server and preserve any other custom fields your application already sends.
