Skip to main content
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.
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.
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.

Create a pool

There is one pool per customer and pricing unit. A pool does not need a plan or a subscription.
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.

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