> ## 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.

# Chargebee

> Learn how to set up Creem with ShipThing.

## Get the api key

After you have created your account for Creem, you will need to get the API key. You can do this by going to the [API page](https://www.chargebee.com/docs/2.0/api_keys.html) 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 Creem integration, you need to define the following environment variables to your `.env.local` as well as your production environment:

```env .env.local theme={"system"}
CHARGEBEE_API_KEY=
CHARGEBEE_SITE=
CREEM_API_KEY=""
```

## In-App Purchases

You can use Chargebee anywhere in your application by importing a `chargebee` object:

```ts page.tsx theme={"system"}
import { chargebee } 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/chargebee/checkout` endpoint.

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

* Server

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

## Webhooks

### Create a webhook

To configure a new webhook, go to the Webhooks page in the chargebee dashboard. Click the Add endpoint button and select at least the following events:

* `Subscription Created`
* `Subscription Changed`
* `Subscription Cancelled`

### Webhook Handler

Stripe webhooks are handled in the `POST /webhooks/chargebee` 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.
