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

# Build a billing page

> Build a full billing page and pricing table from a few Ledger reads, with every button wired to one Ledger call.

A few Ledger reads give you everything for a complete billing page: the customer's plan, credits, every feature's balance and their history, plus a pricing table of your plans. Load the data from your backend, render it, and send each button to one Ledger call.

## Load the page

```typescript TypeScript theme={"system"}
const customerId = session.user.id;

const [customer, subscriptions, purchases, plans, features, packs] =
	await Promise.all([
		billing.getCustomer({ customerId }),
		billing.listCustomerSubscriptions({ customerId }),
		billing.listCustomerPurchases({ customerId }),
		billing.listPlans(),
		billing.listFeatures(),
		billing.listAddOns(),
	]);
```

```python Python theme={"system"}
customer_id = session.user.id

customer = billing.get_customer(customer_id=customer_id)
subscriptions = billing.list_customer_subscriptions(customer_id=customer_id)
purchases = billing.list_customer_purchases(customer_id=customer_id)
plans = billing.list_plans()
features = billing.list_features()
packs = billing.list_add_ons()
```

```bash cURL theme={"system"}
curl -G https://api.integrateledger.com/v1/customers \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  --data-urlencode "customerId=user_123"

curl https://api.integrateledger.com/v1/plans \
  -H "Authorization: Bearer $LEDGER_API_KEY"
```

## The customer's side

| Show | From |
| - | - |
| Current plan and when it renews | `customer.currentPlan.name`, `.currentPeriodEnd` |
| Credits left | `customer.availableCredits` |
| Each metered allowance: "379 of 500 messages left, resets Dec 7" | `customer.features[]` with `type: "metered"`: `balance.remaining`, `.included`, `.resetsAt` |
| Each held limit: "2 of 3 projects" | `type: "held"`: `balance.used` of `balance.included` |
| Seats and what one more costs | `type: "held"` with `billing`: `billing.quantity`, `billing.nextChargeCents` |
| What's on: "SSO included" | `type: "boolean"` |
| Unlimited features | `unlimited: true`, with `balance.included` `null` |
| Overage this period, and the cap | `customer.overage.costCents`, `.capCents`, `.remainingCents` |
| "Confirming your payment", back from Checkout | An item in `subscriptions.items` with `status: "pending_checkout"` |
| Plan and pack history | `subscriptions.items`, `purchases.items` |

Match `featureId` to `features.items` for each feature's display `name`. [Creating customers](https://docs.integrateledger.com/customers/creating-customers.md#read-a-customer) explains every field of the customer.

A customer on a free plan keeps `currentPlan` on that plan until a paid plan's payment lands, so show "Confirming your payment" from a `pending_checkout` subscription.

## The pricing table

`plans.items` lists your active plans, each with its price and what it includes:

```json Response theme={"system"}
{
  "items": [
    {
      "id": "pro",
      "name": "Pro",
      "monthlyCredits": 0,
      "billingInterval": "month",
      "resetInterval": "month",
      "priceCents": 2000,
      "monthlyPriceCents": 2000,
      "overageUnitAmountCents": null,
      "overageBillingUnits": 1,
      "overageCapCents": null,
      "items": [
        { "featureId": "messages", "included": 500, "unlimited": false, "overageUnitAmountCents": 1, "overageBillingUnits": 1, "price": null },
        { "featureId": "projects", "included": null, "unlimited": true, "overageUnitAmountCents": null, "overageBillingUnits": 1, "price": null },
        { "featureId": "sso", "included": null, "unlimited": false, "overageUnitAmountCents": null, "overageBillingUnits": 1, "price": null }
      ],
      "stripeSyncStatus": "synced"
    }
  ]
}
```

* **Sort by `priceCents`**, within each `billingInterval`. A monthly/annual toggle shows the `month` plans or the `year` plans: an [annual plan](https://docs.integrateledger.com/pricing/annual.md) is its own plan. A `one_off` plan is a [lifetime deal](https://docs.integrateledger.com/pricing/lifetime.md).
* **Describe each plan from its items:** "500 messages a month, then 1¢ each" from `included` and `overageUnitAmountCents`; "Up to 3 projects" or "Unlimited projects" from a held item; "SSO" from a boolean item; "\$10 a seat after 3" from a held item's `price`.
* **Label each button** from the customer's plan: "Current plan" for their own, "Upgrade" for pricier plans, "Downgrade" for cheaper ones, "Choose" when they have no plan or a free one.
* **Packs** (`packs.items`) are one-time purchases of credits: show them beside the balance, as "Buy 500 credits for \$10".

## Wire up the buttons

| The customer chooses | Call |
| - | - |
| A plan, with no paid plan today | [`attach`](https://docs.integrateledger.com/billing/checkout-and-attach) with `planId`, then redirect to `checkoutUrl` |
| A credit pack | [`attach`](https://docs.integrateledger.com/billing/checkout-and-attach) with `addOnId`, then redirect to `checkoutUrl` |
| A pricier or cheaper plan, on a paid plan today | [Upgrade or downgrade](https://docs.integrateledger.com/billing/upgrades-and-downgrades.md) |
| Cancel, or undo it | [Stop or resume renewal](https://docs.integrateledger.com/billing/upgrades-and-downgrades.md#cancel-at-period-end), when `cancellation.manageable` is `true` |

```typescript TypeScript theme={"system"}
// POST /billing/choose-plan, from your pricing table's button
const result = await billing.attach({
	customerId: session.user.id,
	planId: request.body.planId,
	successUrl: "https://app.example.com/billing?checkout=success",
});

if ("checkoutUrl" in result) {
	return res.redirect(303, result.checkoutUrl);
}
return res.redirect(303, "/billing");
```

```python Python theme={"system"}
# POST /billing/choose-plan, from your pricing table's button
result = billing.attach(
    customer_id=session.user.id,
    plan_id=request.form["plan_id"],
    success_url="https://app.example.com/billing?checkout=success",
)

if "checkoutUrl" in result:
    return redirect(result["checkoutUrl"], code=303)
return redirect("/billing", code=303)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/attach \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "planId": "pro", "successUrl": "https://app.example.com/billing?checkout=success"}'
```

When the customer lands on your success URL, load the page again: grants land when Stripe's webhook does, usually within seconds. [Checkout and attach](https://docs.integrateledger.com/billing/checkout-and-attach.md#when-grants-land) has the details, test cards and every attach error.

## Rules

* Resolve the signed-in user's `customerId` on your server, never from a value the browser sends, so each customer reads and changes only their own billing.
* Check the `planId` the browser sends against `plans.items`, and call `attach` from your backend.
* Listing plans, features and packs needs a key with **Catalog** read access.


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