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

# Top-ups and auto top-ups

> Let customers add any amount of credit, and keep their balance topped up automatically with their saved card.

> **Add funds:** any amount from $5 to $500. **Auto-recharge:** add $25 when your balance falls below $1.

Top-ups let customers add exactly the credit they want, and auto top-ups refill the balance with their saved card when it runs low, so a customer never runs dry mid-task.

A [credit pack](https://docs.integrateledger.com/pricing/credit-packs.md) sells a fixed number of credits for a fixed price. A top-up sells credits for any amount the customer picks, within your bounds. An auto top-up buys a pack or an amount with the customer's saved card when their balance falls below a threshold.

Top-up credits join the shared pool, never expire, and are spent with packs, after plan credits ([spend order](https://docs.integrateledger.com/concepts/balances.md#spend-order-for-credits)).

## The catalog

`topUps` sets the least and most one top-up may charge, in cents, and how many credits a dollar buys. A plan's `autoTopUp` is the default for its subscribers.

```ts ledger.config.ts theme={"system"}
import { creditPack, defineCatalog, feature, plan } from "integrateledger-cli";

export default defineCatalog({
	topUps: { minCents: 500, maxCents: 50000, creditsPerUsd: 100 },
	features: [feature({ id: "generate_image", name: "Generate an image", creditCost: 1 })],
	plans: [
		plan({
			id: "pro",
			name: "Pro",
			monthlyCredits: 1000,
			priceCents: 2000,
			autoTopUp: { thresholdCredits: 100, addOnId: "credits_500", maxPerPeriod: 3 },
		}),
	],
	creditPacks: [
		creditPack({ id: "credits_500", name: "Top-up", creditAmount: 500, priceCents: 1000 }),
	],
});
```

```json ledger.catalog.json theme={"system"}
{
  "schemaVersion": 1,
  "topUps": { "minCents": 500, "maxCents": 50000, "creditsPerUsd": 100 },
  "features": [{ "id": "generate_image", "name": "Generate an image", "creditCost": 1 }],
  "plans": [
    {
      "id": "pro",
      "name": "Pro",
      "monthlyCredits": 1000,
      "priceCents": 2000,
      "autoTopUp": { "thresholdCredits": 100, "addOnId": "credits_500", "maxPerPeriod": 3 }
    }
  ],
  "addOns": [
    { "id": "credits_500", "name": "Top-up", "type": "credit_pack", "creditAmount": 500, "priceCents": 1000 }
  ]
}
```

A top-up of `amountCents` buys `amountCents × creditsPerUsd / 100` credits, rounded down: $25 buys 2,500 credits here. On a [dollar balance](https://docs.integrateledger.com/pricing/dollar-balance.md) (the catalog's own `creditsPerUsd`), top-ups buy at the balance's rate, `topUps` needs only `minCents` and `maxCents`, and invoices read "Balance top-up: $25.00".

| `autoTopUp` field | What it sets |
| - | - |
| `thresholdCredits` | Buy when available credits fall below this. A balance exactly at it buys nothing |
| `addOnId` | The credit pack to buy, or: |
| `amountCents` | An amount to buy, within `topUps`, at its rate |
| `maxPerPeriod` | The most auto top-ups per billing period, 1 to 31. Without a plan, the period is the UTC month |
| `maxAmountCentsPerPeriod` | Optional: the most auto top-ups may charge per billing period, in cents |

An auto top-up is part of the plan's version, so changing it publishes a [new version](https://docs.integrateledger.com/billing/plan-versions.md) and current subscribers keep the terms they signed up to. A plan's default applies to `active` subscriptions.

## Top up any amount

Send the amount the customer picked. By default the answer has a Stripe Checkout URL, and the credits land when the customer pays:

```typescript TypeScript theme={"system"}
const topUp = await billing.topUp({ customerId, amountCents: 2500 });
if (topUp.checkoutUrl) {
	return res.redirect(303, topUp.checkoutUrl);
}
```

```python Python theme={"system"}
top_up = billing.top_up(customer_id=customer_id, amount_cents=2500)
return redirect(top_up["checkoutUrl"], code=303)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/top-up \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "amountCents": 2500}'
```

With `paymentMethod: "saved"`, Ledger charges the customer's default card at once and answers the top-up `succeeded`, credits granted:

```json Response theme={"system"}
{
  "id": 81,
  "customerId": "user_123",
  "status": "succeeded",
  "failureReason": null,
  "credits": 2500,
  "amountCents": 2500,
  "stripeInvoiceId": "in_1QxY2Z...",
  "checkoutUrl": null
}
```

A saved-card top-up is a one-item Stripe invoice, paid at once, so the customer gets a receipt and, with [Stripe Tax](https://docs.integrateledger.com/billing/tax.md) on, tax. When the saved card can't be charged, send the customer through Checkout: a declined card is `402 payment_failed`, a bank that wants the customer present is `402 authentication_required`, and no saved card is `400 no_payment_method`.

## Auto top-up per customer

A customer's own setting replaces their plan's default whole. Let customers pick their threshold and amount on your billing page, then save it:

```typescript TypeScript theme={"system"}
await billing.setCustomerAutoTopUp({
	customerId,
	body: {
		autoTopUp: { enabled: true, thresholdCredits: 100, amountCents: 2500, maxPerPeriod: 4 },
	},
});
```

```python Python theme={"system"}
billing.set_customer_auto_top_up(
    customer_id=customer_id,
    auto_top_up={"enabled": True, "thresholdCredits": 100, "amountCents": 2500, "maxPerPeriod": 4},
)
```

```bash cURL theme={"system"}
curl "https://api.integrateledger.com/v1/customers/auto-top-up?customerId=user_123" \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"autoTopUp": {"enabled": true, "thresholdCredits": 100, "amountCents": 2500, "maxPerPeriod": 4}}'
```

* **`enabled: false`** turns auto top-ups off for the customer, plan default included.
* **`autoTopUp: null`** clears the customer's setting, so their plan's applies again.

[`GET /v1/customers/auto-top-up`](https://docs.integrateledger.com/api-reference/customers/get-auto-top-up) answers the setting that applies with its `source` (`customer` or `plan`), what one top-up buys, whether failures hold it back, and the customer's last 10 top-ups.

## When it buys

The use that takes the balance below the threshold (a track, a reserve or a settle) queues the auto top-up, and Ledger buys a few seconds later. A burst of tracks buys once: one auto top-up is pending per customer at a time, and the next one waits for the next crossing.

Ledger also tops up a customer who is already below the threshold, even at zero with every use refused:

* **When it's turned on.** Saving an enabled setting for a customer whose balance is below the threshold buys at once, with no use needed.
* **When a wait ends.** After a declined top-up's 24-hour wait, or once a new period lifts `maxPerPeriod` or `maxAmountCentsPerPeriod`, Ledger buys again for a customer still below the threshold. Another decline starts another wait.

The [top-up](https://docs.integrateledger.com/api-reference/billing/top-up.md) and [auto top-up](https://docs.integrateledger.com/api-reference/customers/auto-top-up.md) endpoints have every field.

Each outcome sends a webhook: [`auto_top_up.succeeded`](https://docs.integrateledger.com/webhooks#events), beside `credits.granted`, or `auto_top_up.failed` with its `failureReason`: `no_payment_method`, `card_declined`, `authentication_required`, `payment_incomplete` or `stripe_unavailable`.

## Rules

* **An auto top-up buys one of `addOnId` or `amountCents`.** Sending both returns `400 invalid_auto_top_up`.
* **Failures back off.** After a failed auto top-up, Ledger waits 24 hours, then tries again. After three in a row it suspends auto top-ups until the setting is saved again or the customer's default card changes. `GET` shows `suspended` and `coolingDownUntil`.
* **Period limits hold.** Once `maxPerPeriod` or `maxAmountCentsPerPeriod` is reached, auto top-ups resume in the next billing period.
* **Refunds happen in Stripe.** A full refund or a lost dispute of a top-up expires its unused credits, as for a pack. Credits already spent stay spent.

## Related

* [Credit packs](https://docs.integrateledger.com/pricing/credit-packs.md): fixed amounts at fixed prices, through Checkout.
* [Prepaid dollar balance](https://docs.integrateledger.com/pricing/dollar-balance.md): a wallet customers top up.
* [Webhooks](https://docs.integrateledger.com/webhooks.md): `balance.low`, `auto_top_up.succeeded` and `auto_top_up.failed`.


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