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

# Better Auth plugin

> @sleetpay/better-auth: checkout, billing portal, entitlements and webhooks keyed to your signed-in users.

`@sleetpay/better-auth` is a [Better Auth](https://better-auth.com) plugin. Your signed-in user's id becomes the SleetPay customer's `external_customer_id`, so there is nothing to sync.

```bash theme={null}
npm install @sleetpay/better-auth @sleetpay/sdk
```

```ts auth.ts theme={null}
import { betterAuth } from "better-auth";
import { SleetPay } from "@sleetpay/sdk";
import { sleetpay } from "@sleetpay/better-auth";

export const auth = betterAuth({
  plugins: [
    sleetpay({
      client: new SleetPay({ apiKey: process.env.SLEETPAY_API_KEY! }),
      products: [{ slug: "pro", productId: "prod_..." }],   // only these can be sold
      successUrl: "https://yourapp.com/welcome?checkout={CHECKOUT_ID}",
      webhooks: {
        secret: process.env.SLEETPAY_WEBHOOK_SECRET!,        // endpoint: /api/auth/sleetpay/webhooks
        handlers: { "customer.state_changed": async ({ data }) => { /* ... */ } },
      },
    }),
  ],
});
```

```ts auth-client.ts theme={null}
import { createAuthClient } from "better-auth/react";
import { sleetpayClient } from "@sleetpay/better-auth/client";

export const authClient = createAuthClient({ plugins: [sleetpayClient()] });

await authClient.checkout({ slug: "pro" });        // sends the browser to checkout
await authClient.portal();                          // sends the browser to the billing portal
const { data } = await authClient.customer.state(); // subscriptions, benefits, credits, meters
```

| Endpoint | Does |
| - | - |
| `POST /sleetpay/checkout` | Body `{ slug or productId, seats?, currency?, discountId?, metadata? }`, returns `{ url, redirect }` |
| `GET /sleetpay/portal` | Returns `{ url, redirect }`, a signed link into the billing portal |
| `GET /sleetpay/state` | What the user owns. A user who never bought gets empty lists, not an error |
| `POST /sleetpay/webhooks` | Standard Webhooks, signature verified on the raw body |

Checkout, portal and state need a session (401 otherwise). The customer is always the session's user, never a value from the request body, and only products listed in `products` can be sold.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.