Documentation menu

Wallets and the ledger

How a customer's prepaid balance works, how to read it, and how amounts are written.

Each customer can have a wallet: a prepaid balance with you, in US dollars. You put credit in (grants, top-ups), requests take it out, and every movement is an entry in the customer's ledger, which only ever grows. The balance is the sum of the ledger.

Paying requests from wallets#

Wallets are off until you turn them on for an environment, in Credits → Settings (“Pay customers' requests from their wallets”), or with {"wallet": {"enabled": true}} and PATCH /v1/gateway with a project key. Then:

  • a request that names a customer is paid from that customer's wallet, at the model's price plus your markup;
  • before the provider is called, the gateway reserves an estimate of the request's cost and refuses it with 402 insufficient_credits if the balance cannot cover it;
  • when the request finishes, the reservation is settled at the actual cost.

A request that names no customer, or that runs on the customer's own provider key, is never charged to a wallet. With wallets off, requests are paid from your organization's account and no balance is checked; usage is still recorded per customer.

Amounts#

Every amount in Hixel is an integer number of micro-USD, millionths of a dollar, written as a decimal string:

You meanYou send and read
$1.00"1000000"
$0.25"250000"
−$2.50"-2500000"
$0.000198"198"

Strings keep every amount exact: a JSON number loses precision past 2^53 in most languages, and a sum of many small amounts must not round. A JSON number where an amount is expected is refused. Fields that hold amounts end in _micros. Token counts are strings of integers too.

To show an amount, divide by 1,000,000 in a decimal type, never in floating point:

const dollars = (micros: string) => {
  const value = BigInt(micros);
  const sign = value < 0n ? "-" : "";
  const abs = value < 0n ? -value : value;
  return `${sign}$${abs / 1_000_000n}.${String(abs % 1_000_000n)
    .padStart(6, "0")
    .slice(0, 2)}`;
};
dollars("2512345"); // "$2.51"

Read a wallet#

curl https://api.hixel.dev/v1/customers/org_42/wallet \
  -H "Authorization: Bearer $HIXEL_API_KEY"
{
  "customer": "org_42",
  "wallet_id": "wal_…",
  "balance": {
    "purchased_micros": "20000000",
    "grants_micros": "4200000",
    "total_micros": "24200000"
  },
  "grants": [
    {
      "entry_id": "led_…",
      "key": "plan:2026-11",
      "amount_micros": "5000000",
      "remaining_micros": "4200000",
      "expires_at": "2026-12-01T00:00:00.000Z"
    }
  ],
  "pending_credits": 0,
  "rejected_credits": 0,
  "live": {
    "status": "available",
    "balance": {
      "purchased_micros": "20000000",
      "grants_micros": "4180000",
      "total_micros": "24180000"
    },
    "reserved_micros": "120000",
    "available_micros": "24060000",
    "spend": {
      "day_micros": "20000",
      "week_micros": "820000",
      "month_micros": "820000",
      "total_micros": "820000"
    }
  }
}
  • balance is the ledger's sum: purchased credit plus what is left of unexpired grants.
  • grants lists the grants with credit left. Grants are spent before purchased credit, the one that expires soonest first; purchased credit never expires.
  • live is the gateway's own view of the same wallet: its balance, what running requests hold (reserved_micros), what a new request may spend (available_micros) and the settled spend in each window (UTC; weeks start on Monday). The two balances can differ for a moment while a request's usage is being recorded. When the gateway cannot be asked just now, live is { "status": "unavailable" }.
  • pending_credits counts credits written to the ledger that the gateway has not applied yet (they are retried automatically).

A customer that never had a ledger entry has a zero balance and wallet_id: null.

Read the ledger#

curl "https://api.hixel.dev/v1/customers/org_42/ledger?limit=20" \
  -H "Authorization: Bearer $HIXEL_API_KEY"

Entries come newest first, paginated. Each has a kind, a signed amount_micros (credits positive, usage and expiry negative) and an idempotency_key:

kindWhat it is
grantCredit you added without payment, possibly expiring
top_upPurchased credit: a Stripe Checkout top-up, or one you recorded yourself
auto_refillPurchased credit from an automatic refill of the customer's saved card
usageA request's cost, after markup; usage_event_id links it to its usage record
refundPurchased credit returned (positive) or taken back (negative)
adjustmentA correction you made, in either direction
expiryWhat was left of a grant when it expired

A usage or expiry entry names the grant it drew from (grant_key). A request paid partly from a grant and partly from purchased credit has one usage entry for each.

List wallets#

GET /v1/wallets lists the environment's wallets (customers with at least one ledger entry), lowest balance first, or highest first with sort=balance_desc. Each shows its customer, the customer's name (customer_name), its balance and balance_alert: low once its balance fell below the environment's low-balance threshold, depleted at or below zero, none otherwise. Filter with balance_alert=low or balance_alert=depleted to find the customers who need credit.

Across every customer#

  • GET /v1/ledger lists the environment's ledger across all its customers, newest first, filtered by kind (top_up, auto_refill, …), customer, and from/to.
  • GET /v1/ledger/summary?from=…&to=… sums it by UTC day and kind over at most 92 days: what was sold, granted, used, expired, refunded and adjusted each day.
  • GET /v1/wallets/outstanding is the credit your customers hold right now, purchased and granted, with how many wallets are low or depleted and how many credits are still on their way to the gateway.