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

# Customers and entitlements

> Read everything a customer owns in one call and stay in sync with webhooks.

A **customer** is created automatically on a buyer's first purchase. You can also create one yourself, and you can tie it to your own user with an **external id**.

```ts theme={null}
const customer = await sleetpay.customers.create({ email: "ada@example.com", name: "Ada", externalId: user.id });
const same = await sleetpay.customers.getByExternalId(user.id);
```

## Entitlements in one read

`customers.state` returns everything a customer is entitled to right now:

```ts theme={null}
const state = await sleetpay.customers.stateByExternalId(user.id);

state.active_subscriptions; // active and past_due subscriptions
state.granted_benefits;     // license keys, downloads, credits, custom benefits
state.credit_balances;      // prepaid credits per unit label
state.active_meters;        // usage meters with consumed and remaining units
```

`past_due` means a renewal failed and the customer is inside the dunning grace period. Gate on `status === "active"` if you want to be stricter.

## Stay in sync without polling

Subscribe to the `customer.state_changed` webhook. Its `data` is the same document the state endpoint returns, and bursts of changes (a purchase emits an order, a subscription and several grants) are coalesced into one event. A good pattern:

1. Cache `state` per user.
2. Refresh the cache when `customer.state_changed` arrives.
3. Fall back to a direct read if the cache is missing.

## Listing

```ts theme={null}
for await (const customer of sleetpay.customers.iterate({ email: "ada@example.com" })) console.log(customer.id);
```

## Related

* [API reference](https://docs.sleetpay.com/api-reference/introduction.md)
* [Customer portal](https://docs.sleetpay.com/guides/portal.md)


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