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

# Credit pools

> Price usage in credits instead of currency, fund a customer's credit pool, and read its balance and ledger

> [!NOTE]
>
> Credit pools need a pricing unit, a customer, and (to draw credits down) a metered price on one of the customer's active
> subscriptions. For the shared balance fields on entitlements, see
> [Metered Entitlements](https://docs.paygentic.io/platform/billing/metered-entitlements.md).

A credit pool holds a customer's credits for one pricing unit. You grant credits into the pool, usage on every metered price in that unit draws them down, and you can sell top-ups with a hosted checkout.

## Prerequisites

* A merchant API key. The examples use `$PAYGENTIC_API_KEY` against the sandbox.
* A customer (`cus_...`).

## Pricing units

A pricing unit is a named unit of credit that converts to real currency, such as "credits" at \$0.01 each. It holds no balance itself.

```bash theme={null}
curl -X POST "https://api.sandbox.paygentic.io/v0/pricingUnits" \
  -H "Authorization: Bearer $PAYGENTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "merchantId": "org_abc123",
    "identifier": "credits",
    "displayName": "AI credits",
    "shortName": "credits",
    "conversionRate": "0.01",
    "currency": "USD"
  }'
```

| Field | Type | Required | Description |
| - | - | - | - |
| `merchantId` | string | Yes | Your organization ID |
| `identifier` | string | Yes | Slug, unique per merchant |
| `displayName` | string | Yes | Name shown to people |
| `shortName` | string | Yes | Short label used on invoice lines |
| `conversionRate` | string | Yes | Currency per credit, as a decimal string |
| `currency` | string | Yes | ISO 4217 currency code |

The response is a `pricingUnit` with an `id` that starts with `pu_`. You can create, list (`GET /v0/pricingUnits`) and get (`GET /v0/pricingUnits/{id}`) pricing units. You cannot edit or delete them.

### Price usage in credits

Set `pricingUnitId` on a metered price to charge that price in credits instead of currency. The price's rate, for example `unitPrice`, is then the number of credits per unit of usage. Every usage event on that price draws down the customer's pool for the unit. All features priced in the same unit draw from one shared balance.

`pricingUnitId` only works on metered prices. It cannot be combined with a negative unit price or a volume price. See [Prices](https://docs.paygentic.io/platform/pricing/prices.md).

## Create a pool

There is one pool per customer and pricing unit. A pool does not need a plan or a subscription.

```bash theme={null}
curl -X POST "https://api.sandbox.paygentic.io/v0/creditPools" \
  -H "Authorization: Bearer $PAYGENTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "customerId": "cus_abc123",
    "pricingUnitId": "pu_abc123"
  }'
```

The call is idempotent. It returns `201` when it creates the pool and `200` with the existing pool when one already exists. List pools with `GET /v0/creditPools` and filter by `merchantId`, `customerId` or `pricingUnitId`.

## Grant credits

A grant adds credits to the pool. The balance lives on grants, not on the pool.

```bash theme={null}
curl -X POST "https://api.sandbox.paygentic.io/v0/creditPools/crp_abc123/grants" \
  -H "Authorization: Bearer $PAYGENTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": 5000,
    "priority": 0,
    "expiresAt": "2027-01-01T00:00:00Z",
    "idempotencyKey": "welcome-credits-cus_abc123"
  }'
```

| Field | Type | Required | Description |
| - | - | - | - |
| `amount` | number | Yes | Credits to add. Greater than 0 |
| `priority` | integer | No | Lower numbers are spent first. Default `0` |
| `effectiveAt` | date-time | No | When the grant starts. Default: now |
| `expiresAt` | date-time | No | When unused credits lapse. Must be after `effectiveAt` |
| `recurrencePeriod` | string | No | ISO 8601 duration, such as `P1M`. The `amount` is added again each period. Minimum `P1D` |
| `recurrenceAnchor` | date-time | No | Start of the recurrence. Only with `recurrencePeriod` |
| `idempotencyKey` | string | No | Repeating a key returns the existing grant |

### Spend order

When usage draws credits down, the pool spends grants in this order:

1. Lowest `priority` number first.
2. Then the soonest `expiresAt`. Grants with no expiry go last.
3. Then the oldest grant.

You can also fund a pool from a plan with `creditAllocations`. Each allocation names a `pricingUnitId` and an `amount`, and it funds that pool when a subscription activates.

## Sell a top-up

`purchase` sells credits to a customer. The price comes from the pricing unit's `conversionRate`, so you never send a price.

```bash theme={null}
curl -X POST "https://api.sandbox.paygentic.io/v0/creditPools/crp_abc123/purchase" \
  -H "Authorization: Bearer $PAYGENTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "grantAmount": 1000,
    "idempotencyKey": "topup-cus_abc123-2026-11",
    "subscriptionId": "sub_abc123",
    "successUrl": "https://example.com/thanks"
  }'
```

This creates an invoice for the credits and returns a hosted payment URL in `paymentSessions[0].url`. The credits arrive as a grant when the customer pays the invoice, not when you call `purchase`. The customer needs an active subscription; otherwise you get `400`. Send `subscriptionId` when the customer has more than one active subscription.

If settlement is still running, you get `202` with `processing: true` and an `invoiceId`. Retry with the same `idempotencyKey`.

## Read the balance and ledger

The ledger shows how a pool's balance changed over a window. `from` and `to` are required, `to` must be after `from`, and the window can be at most 366 days.

```bash theme={null}
curl "https://api.sandbox.paygentic.io/v0/creditPools/crp_abc123/ledger?from=2026-11-01T00:00:00Z&to=2026-12-01T00:00:00Z" \
  -H "Authorization: Bearer $PAYGENTIC_API_KEY"
```

The response has `startingBalance`, `endingBalance`, and `entries`. Each entry has an `entryType`, a signed `amount`, its own `startingBalance` and `endingBalance`, and the `grant` it relates to.

| Entry type | Meaning | Amount |
| - | - | - |
| `increment` | A grant funded the pool, including recurring refills | Positive |
| `decrement` | Usage drew credits down | Zero or negative |
| `void` | A grant was ended early | Zero or negative |
| `expiration` | Unused credits lapsed at expiry | Zero or negative |

Add `granularity` (`hour`, `day` or `month`) to get `buckets` instead of entries. Buckets use UTC calendar boundaries, skip periods with no activity, and report `incremented`, `decremented`, `voided`, `expired` and `entryCount`. Use `entryType` to filter, and `limit` and `offset` to page.

Entry IDs are only stable for the first `increment` of a grant, where the entry `id` equals the grant ID. When you poll the ledger, key entries on `(grant.id, entryType, at)`.

> [!WARNING]
>
> The ledger is recomputed every time you read it. It is not a stored record, so entries for past periods can change.
> Usage is priced using the current price rate and the customer's currently active subscriptions, not the rate or
> subscriptions in effect at the time. After you change a price's rate, or after a subscription is cancelled or a new one
> starts, reading the same past window can give different `decrement` amounts and balances. If you need a fixed
> record of a period, store the response when you read it.

### Overage

`startingBalance` and `endingBalance` are the sum of the grant balances minus overage. Overage is usage that was more than every active grant could cover. When usage outruns the grants, the balance falls below the grants' total and can go below zero. Overage does not appear as its own ledger entry, so the entries alone may not add up to `endingBalance` when overage exists.

Overage is not invoiced today. Keep the pool funded with grants or top-ups.


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