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

# Prepaid quantity

> Price a plan's credits or a metered feature in prepaid packs, such as 1,000 credits included and more at $10 per 1,000, up to 50,000 a month.

> **Pro:** $20 a month with 1,000 credits. More at $10 per 1,000, up to 50,000 a month.

Prepaid terms price usage in packs bought up front, so revenue arrives at the start of the period and customers know their bill in advance.

A plan's `prepaid` sells its credits, or a metered item's units, in packs of `billingUnits` at `unitAmountCents` each. The included amount (`monthlyCredits`, or the item's `included`) stays free, and packs price what's above it, up to `maxQuantity`. Ledger publishes, versions and syncs the terms to Stripe like any price. The customer picks a quantity when you [attach the plan](#sell-a-quantity), raises or lowers it with [`POST /v1/billing/quantities`](#change-a-quantity), and keeps it through renewals and [upgrades](#plan-changes).

## The catalog

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

export default defineCatalog({
	features: [feature({ id: "messages", name: "Messages", type: "metered" })],
	plans: [
		plan({
			id: "pro",
			name: "Pro",
			monthlyCredits: 1000,
			priceCents: 2000,
			prepaid: { billingUnits: 1000, unitAmountCents: 1000, maxQuantity: 50000 },
		}),
		plan({
			id: "team",
			name: "Team",
			monthlyCredits: 0,
			priceCents: 2000,
			items: [
				{
					featureId: "messages",
					included: 1000,
					prepaid: { billingUnits: 1000, unitAmountCents: 1000, maxQuantity: 50000 },
				},
			],
		}),
	],
});
```

```json ledger.catalog.json theme={"system"}
{
  "schemaVersion": 1,
  "features": [{ "id": "messages", "name": "Messages", "type": "metered" }],
  "plans": [
    { "id": "pro", "name": "Pro", "monthlyCredits": 1000, "priceCents": 2000,
      "prepaid": { "billingUnits": 1000, "unitAmountCents": 1000, "maxQuantity": 50000 } },
    { "id": "team", "name": "Team", "monthlyCredits": 0, "priceCents": 2000,
      "items": [
        { "featureId": "messages", "included": 1000,
          "prepaid": { "billingUnits": 1000, "unitAmountCents": 1000, "maxQuantity": 50000 } }
      ] }
  ]
}
```

| Field | Required | What it sets |
| - | - | - |
| `billingUnits` | Yes | Units in one pack, such as 1000. A whole number, 1 or more |
| `unitAmountCents` | Yes | The price of one pack per billing interval of the plan, above 0 |
| `maxQuantity` | No | The most one customer may hold a period, the included amount counted, in whole packs above it. Left out: no maximum |

A quantity is the total for the period, the included amount counted: with the terms above, 5,000 credits is the free 1,000 plus 4 packs, \$40 a month.

`GET /v1/plans` lists a plan's `prepaid` for its credits and each item's `prepaid`, `null` when there's none, with `maxQuantity` only when it's set. A plan edit that leaves the plan's `prepaid` out, such as the dashboard's plan form or `POST /v1/plans/{id}` without the field, keeps the current version's; `null` removes it. An item's `prepaid` is part of the item, so an edit that sends `items` sends it.

## Stripe

Stripe sync creates one Stripe Price per prepaid entry: one pack at `unitAmountCents`, recurring at the plan's billing interval, with `ledger_price_role: prepaid` and the feature's ID (`credits` for the plan's credits) in its metadata, under a `<Plan> – <Feature> prepaid` Product. Renaming the plan renames that Product in place. A plan with prepaid terms bills through Stripe even if its own price is \$0, and counts as synced once every prepaid Price exists. A new version gets new Prices, so current subscribers keep theirs.

## Sell a quantity

Send `quantities` when you attach the plan, one entry per prepaid feature (`"credits"` for the plan's credits), each the period's total with the included amount counted:

```typescript TypeScript theme={"system"}
const result = await billing.attach({
	customerId: "user_123",
	planId: "pro",
	quantities: [{ featureId: "credits", quantity: 5000 }],
});
```

```python Python theme={"system"}
result = billing.attach(
    customer_id="user_123",
    plan_id="pro",
    quantities=[{"feature_id": "credits", "quantity": 5000}],
)
```

```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", "quantities": [{"featureId": "credits", "quantity": 5000}]}'
```

With the terms above, the first invoice bills Pro's $20 and one line of 4 packs at the prepaid Price, $40, and the period grants 5,000 credits in place of the plan's 1,000. A metered item's quantity is that feature's allowance instead: 5,000 `messages` grants 5,000 messages, which [track](https://docs.integrateledger.com/customers/check-and-track.md) spends and refuses with `limit_reached` past it. A feature left out gets its included amount and no pack line. Each renewal bills the plan and the same packs and grants the quantity again, reset like any plan grant; cancelling keeps it to the period end.

## Change a quantity

[`POST /v1/billing/quantities`](https://docs.integrateledger.com/api-reference/billing/quantities) sets a new total for one feature of the customer's plan:

```typescript TypeScript theme={"system"}
const change = await billing.setQuantities({
	idempotencyKey: `quantity_${quantityChange.id}`, // saved when the customer confirmed
	body: {
		customerId: "user_123",
		planId: "pro",
		quantities: [{ featureId: "credits", quantity: 10000 }],
	},
});
if (change.status === "pending_payment" && change.paymentUrl) {
	return res.redirect(303, change.paymentUrl);
}
```

```python Python theme={"system"}
change = billing.set_quantities(
    customer_id="user_123",
    plan_id="pro",
    quantities=[{"feature_id": "credits", "quantity": 10000}],
    idempotency_key=f"quantity_{quantity_change.id}",  # saved when the customer confirmed
)
if change["status"] == "pending_payment" and change.get("paymentUrl"):
    return redirect(change["paymentUrl"], code=303)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/quantities \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Idempotency-Key: quantity_qc_101" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "user_123", "planId": "pro", "quantities": [{"featureId": "credits", "quantity": 10000}]}'
```

* **A raise** is charged now, prorated for the rest of the period, on the saved card. Once that invoice is paid, the customer holds the new quantity and is granted the extra units prorated by the time left, rounded down: 5,000 more credits a third of the way through a month grants 3,333 now. The renewal then bills and grants the new quantity in full.
* **The same quantity** changes nothing and answers `status: "unchanged"`.
* **A decrease** takes effect at the renewal and answers `status: "scheduled"`, the new total in `pendingQuantity`. This period keeps its packs and grant; the renewal bills the lower packs and grants the lower quantity. A later change replaces a waiting decrease: another decrease, or the held quantity again to drop it.

The answer is `{ itemChangeId, status, paymentUrl, quantities }`, `status` as for an [add-on](https://docs.integrateledger.com/pricing/add-ons.md): `applied`, `pending_payment` (send the customer to `paymentUrl`), `processing` (retry with the same key), `closed`, or for a decrease `scheduled`.

## Plan changes

An [upgrade](https://docs.integrateledger.com/billing/upgrades-and-downgrades.md) to a plan that sells the same prepaid feature keeps the customer's quantity, billed as the new plan's packs above its own included amount: 5,000 credits onto a plan with 2,000 included bills 3 packs on the upgrade invoice and grants 5,000. A quantity below the new plan's included amount becomes the included amount. A plan without the feature drops it, and its packs leave the bill. To change the quantity as well, upgrade, then [change the quantity](#change-a-quantity).

## Coupons

A [coupon](https://docs.integrateledger.com/billing/discounts.md) on a plan covers its prepaid pack Products too, so the discount reaches the pack lines as well as the plan's own.

## Rules

* **Prepaid terms are money terms.** Changing them publishes a new [plan version](https://docs.integrateledger.com/billing/plan-versions.md).
* **A quantity is whole packs, from the included amount to `maxQuantity`.** Below the included amount, or not whole packs above it, is `400 invalid_request` at `quantities.N.quantity` (Ledger refuses 5,500 with packs of 1,000 rather than rounding it up); above `maxQuantity` is `400 max_purchase_exceeded`, on attach and on an upgrade that carries the quantity. A feature the plan sells no prepaid entry for, or one named twice, is `400 invalid_request` at `quantities.N.featureId`.
* **Prepaid sells on its own monthly or yearly bill.** An attach that starts a trial, a plan that bills yearly but resets monthly, or an attach onto another group's bill or an entity is `400 prepaid_attach_unavailable`.
* **Upgrades carry the quantity; downgrades go through a cancel.** A downgrade to or from a plan with prepaid is `400 prepaid_change_unavailable`: upgrade, or cancel at the period end and attach the new plan.
* **One change at a time per bill.** A raise while a decrease waits, or any change while another is pending, is `409 subscription_change_pending`. `POST /v1/billing/quantities` for a plan the customer doesn't hold on an active paid bill is `409 plan_not_held`.
* **Metered items, or the credit pool.** An item's feature is `metered` (`prepaid_feature_type`); sell credits with the plan's own `prepaid`.
* **An item with a limit.** A prepaid item has `included`, not `unlimited` (`prepaid_unlimited`).
* **Base plans that recur.** Prepaid terms go on a monthly or annual base plan, not an [add-on](https://docs.integrateledger.com/pricing/add-ons.md) (`prepaid_add_on_unsupported`) or a [lifetime plan](https://docs.integrateledger.com/pricing/lifetime.md) (`prepaid_must_recur`).
* **A maximum above the included amount, in whole packs.** `maxQuantity` is above `included` or `monthlyCredits` (`prepaid_max_below_included`), by a whole number of packs (`prepaid_max_not_multiple`): with 1,000 included and packs of 1,000, 50,000 fits and 50,500 doesn't.
* **Prepaid or a seat price.** A seat price is for held items and prepaid for metered ones, so an item takes one of the two.
* **Seat plans are priced per seat.** A [license](https://docs.integrateledger.com/pricing/seat-licenses.md)'s seat plan has no prepaid terms (`license_plan_has_prepaid`).
* **One currency.** A plan with prepaid terms is priced in its own [currency](https://docs.integrateledger.com/pricing/currencies.md), without `currencyOptions`.

A push refuses terms that break these with an issue at `prepaid`, `prepaid.maxQuantity`, `items.N.prepaid` or `items.N.prepaid.maxQuantity`. A pack size or maximum that isn't a whole number above 0, or a `unitAmountCents` of 0, is refused as `invalid_request` (`invalid_catalog` on `POST /v1/catalog`). Every code is in [Errors](https://docs.integrateledger.com/errors.md#catalog).

## Related

* [Credit packs](https://docs.integrateledger.com/pricing/credit-packs.md): one-time credits bought through Checkout.
* [Top-ups and auto top-ups](https://docs.integrateledger.com/pricing/top-ups.md): any amount, or bought automatically when the balance runs low.
* [Overage](https://docs.integrateledger.com/pricing/overage.md): bill usage past the allowance after the period.


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