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.
Prerequisites
- A merchant API key. The examples use
$PAYGENTIC_API_KEYagainst 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.
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
SetpricingUnitId 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.
Create a pool
There is one pool per customer and pricing unit. A pool does not need a plan or a subscription.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.Spend order
When usage draws credits down, the pool spends grants in this order:- Lowest
prioritynumber first. - Then the soonest
expiresAt. Grants with no expiry go last. - Then the oldest grant.
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.
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.
startingBalance, endingBalance, and entries. Each entry has an entryType, a signed amount, its own startingBalance and endingBalance, and the grant it relates to.
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).
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.