Billing

Read your balance, credit grants, receipts and monthly usage statements. Reads only — no money moves through this API.

What your organization has, what it owes, what it bought and what it used. Reads only.

EndpointScopeToken owner's role
GET /billing/balancebilling:readAdmin
GET /billing/grantsbilling:readAdmin
GET /billing/receiptsbilling:readAdmin
GET /billing/receipts/{id}billing:readAdmin
GET /billing/statementsbilling:readAdmin
GET /billing/statements/{period}billing:readAdmin
GET /billing/invoicesbilling:readAdmin
GET /billing/invoices/{id}billing:readAdmin

Billing is admin-only: the token owner must currently be an organization Admin (403 insufficient_scope otherwise). Payments, payment methods, credit purchases and billing-mode changes are handled in the portal.

Money is integer nano-USD (*_nanousd, 10⁻⁹ USD — exact), with an exact decimal *_usd string alongside. Receipts and statements are the same documents the portal's Billing page shows (Top-up invoices and Usage statements), read from the same record as Usage.

Python examples share this setup:

import os
import requests

BASE = "https://portal.<domain>/api/v1/mgmt"
s = requests.Session()
s.headers["Authorization"] = f"Bearer {os.environ['TF_PAT']}"

Retrieve the balance

GET /api/v1/mgmt/billing/balance

How the organization pays (mode) and both balances: the prepaid credit balance, and the invoiced credit line when staff have set one. The block in force has active: true.

FieldTypeNotes
modestringprepaid, invoiced (billed by invoice against a credit line) or unlimited.
prepaidobjectThe prepaid credit balance (always present).
prepaid.activebooleantrue when the organization is prepaid.
prepaid.available_nanousd / available_usdinteger / stringCredit available.
prepaid.wallet_presentbooleanfalse when the organization has never been granted credit.
invoicedobject | nullThe invoiced credit line; null when there has never been one.
invoiced.activebooleantrue when the organization is invoiced.
invoiced.credit_limit_*integer / stringThe approved credit line.
invoiced.used_*integer / stringUsage on the line since it started (prepaid periods excluded).
invoiced.paid_*integer / stringPayments received against the line.
invoiced.outstanding_*integer / stringused − paid.
invoiced.remaining_*integer / stringcredit_limit − outstanding, never below 0. Inference is refused at 0.
available_nanousd, available_usd, currency, wallet_presentThe prepaid credit balance again, kept from the first version of this endpoint (available_usd is a number here).
curl https://portal.<domain>/api/v1/mgmt/billing/balance \
  -H "Authorization: Bearer $TF_PAT"
{
  "mode": "invoiced",
  "prepaid": {
    "active": false,
    "available_nanousd": 0,
    "available_usd": "0.00",
    "wallet_present": true
  },
  "invoiced": {
    "active": true,
    "credit_limit_nanousd": 500000000000,
    "credit_limit_usd": "500.00",
    "used_nanousd": 100000000000,
    "used_usd": "100.00",
    "paid_nanousd": 40000000000,
    "paid_usd": "40.00",
    "outstanding_nanousd": 60000000000,
    "outstanding_usd": "60.00",
    "remaining_nanousd": 440000000000,
    "remaining_usd": "440.00"
  },
  "available_nanousd": 0,
  "available_usd": 0,
  "currency": "USD",
  "wallet_present": true
}

If billing data is temporarily unavailable the call is 502 upstream_unavailable — retry.

List credit grants

GET /api/v1/mgmt/billing/grants

Every credit movement on the organization — grants, top-ups, refunds — newest first. Paginated.

FieldTypeNotes
idstringA stable id for the row (grant_…).
created_atstring
sourcestringWhere the credit came from, e.g. stripe (a top-up), signup, promo, admin_grant, refund. Treat it as an open set.
memostringHuman description.
amount_nanousdintegerNegative for refunds and reversals.
amount_usdnumberThe same amount in USD.
referencestring | nullThe payment reference for a top-up, when there is one.
curl https://portal.<domain>/api/v1/mgmt/billing/grants \
  -H "Authorization: Bearer $TF_PAT"
{
  "data": [
    {
      "id": "grant_9f2c41d07a5b3e18",
      "created_at": "2026-09-01T12:00:00Z",
      "source": "stripe",
      "memo": "Credit top-up",
      "amount_nanousd": 2000000000000,
      "amount_usd": 2000,
      "reference": "pi_3P…"
    }
  ],
  "has_more": false,
  "next_cursor": null
}

List receipts

GET /api/v1/mgmt/billing/receipts

One receipt per credit purchase — the portal's Billing → Top-up invoices. Paginated, newest first.

FieldTypeNotes
idstringReceipt id.
numberstring | nullThe invoice number printed on the PDF.
stripe_invoice_idstringThe payment provider's invoice id.
amount_nanousd / amount_usdinteger / stringThe amount paid.
currencystring
statusstringe.g. paid.
created_atstring
pdf_availablebooleanfalse for the short time before the provider finalizes the PDF.
curl https://portal.<domain>/api/v1/mgmt/billing/receipts \
  -H "Authorization: Bearer $TF_PAT"
{
  "data": [
    {
      "id": "5b1e2f7a-3c4d-4e5f-8a9b-0c1d2e3f4a5b",
      "number": "TF-0042",
      "stripe_invoice_id": "in_1Q…",
      "amount_nanousd": 10000000000,
      "amount_usd": "10.00",
      "currency": "USD",
      "status": "paid",
      "created_at": "2026-11-10T10:00:00Z",
      "pdf_available": true
    }
  ],
  "has_more": false,
  "next_cursor": null
}

Retrieve a receipt

GET /api/v1/mgmt/billing/receipts/{id}

The receipt plus its document links: pdf_url (the same PDF the portal opens) and hosted_url (the provider's invoice page). Both are null while the provider is still finalizing the document — retry a little later. An id that is not one of your organization's receipts returns 404.

{
  "id": "5b1e2f7a-3c4d-4e5f-8a9b-0c1d2e3f4a5b",
  "number": "TF-0042",
  "amount_nanousd": 10000000000,
  "amount_usd": "10.00",
  "status": "paid",
  "created_at": "2026-11-10T10:00:00Z",
  "pdf_available": true,
  "pdf_url": "https://pay.stripe.com/invoice/…/pdf",
  "hosted_url": "https://invoice.stripe.com/i/…"
}

List usage statements

GET /api/v1/mgmt/billing/statements?months=12

The current month (status: in_progress, totals null) and the last months closed calendar months (1–36, default 12), newest first — the portal's Billing → Usage statements. Not paged: limit and cursor are 422 unsupported_filter.

FieldTypeNotes
periodstringYYYY-MM (UTC).
period_start, period_endstringDates; period_end is exclusive.
statusstringclosed or in_progress.
total_nanousd / total_usdinteger / string | nullThe month's usage cost; null while in progress.
credit_applied_nanousdinteger | nullHow much was settled from prepaid credit.
has_usagebooleanfalse for a closed month with no usage (total 0).

Retrieve a usage statement

GET /api/v1/mgmt/billing/statements/{period}[?format=json|csv|pdf]

One closed month. JSON (the default) carries exact amounts: total_*, credit_applied_nanousd, one line per provider/model/unit/region, a subtotal per provider, and downloads with the csv and pdf forms — the same files the portal downloads, fetched with the same token. A month that has not ended yet is 422 validation_failed (param period).

curl https://portal.<domain>/api/v1/mgmt/billing/statements/2026-10 \
  -H "Authorization: Bearer $TF_PAT"

curl -o statement-2026-10.pdf \
  "https://portal.<domain>/api/v1/mgmt/billing/statements/2026-10?format=pdf" \
  -H "Authorization: Bearer $TF_PAT"
{
  "period": "2026-10",
  "period_start": "2026-10-01T00:00:00Z",
  "period_end": "2026-11-01T00:00:00Z",
  "generated_at": "2026-11-19T08:00:00Z",
  "currency": "USD",
  "total_nanousd": 421193004112,
  "total_usd": "421.193004112",
  "credit_applied_nanousd": 421193004112,
  "lines": [
    {
      "provider": "openai",
      "model": "openai/gpt-4o",
      "unit": "token",
      "region": "eu",
      "quantity_in": 81234567,
      "quantity_out": 9876543,
      "cost_nanousd": 401193004112,
      "cost_usd": "401.193004112",
      "price_digest": "sha256:…"
    }
  ],
  "by_provider": [
    { "provider": "openai", "cost_nanousd": 401193004112, "cost_usd": "401.193004112" }
  ],
  "price_digests": ["sha256:…"],
  "downloads": {
    "csv": "/api/v1/mgmt/billing/statements/2026-10?format=csv",
    "pdf": "/api/v1/mgmt/billing/statements/2026-10?format=pdf"
  }
}

Reconcile usage with billing

The monthly statement is the document usage reconciles to.

  1. Read the statement for a closed month: GET /billing/statements/2026-10 → total_nanousd.
  2. Read daily usage for the same month: GET /usage?group_by=day&from=2026-10-01T00:00:00Z&to=2026-11-01T00:00:00Z.
  3. The sum of the days' cost_nanousd (and totals.cost_nanousd) equals the statement's total_nanousd exactly.
  4. Credit purchases are the receipts; between them, watch GET /billing/balance fall as usage accrues.
st = s.get(f"{BASE}/billing/statements/2026-10", timeout=30).json()
usage = s.get(f"{BASE}/usage", params={
    "group_by": "day",
    "from": "2026-10-01T00:00:00Z",
    "to": "2026-11-01T00:00:00Z",
}, timeout=60).json()
days = sum(d["cost_nanousd"] for d in usage["breakdown"])
assert days == st["total_nanousd"], (days, st["total_nanousd"])

Both sides are the same record, so this is a check, not a fix-up — if they disagree, contact support and quote the X-Request-Id of both responses. Invoiced organizations reconcile the same way, against the monthly statement.

Legacy subscription invoices

GET /api/v1/mgmt/billing/invoices
GET /api/v1/mgmt/billing/invoices/{id}

Subscription invoices from the earlier billing model, kept so existing integrations keep working. New integrations should use receipts and usage statements. Paginated, newest first.

FieldTypeNotes
idstringOpaque id.
stripe_invoice_idstringThe payment provider's invoice id.
amount_centsintegerInvoice total in cents of currency.
currencystring
statusstringe.g. open or paid.
period_start, period_endstring | nullThe billed period.
pdf_urlstring | nullThe provider-hosted PDF, once issued.
created_atstring

Errors specific to billing

StatusCodeWhen
400invalid_requestA malformed period, months or format.
403insufficient_scopeToken lacks billing:read, or the owner is not currently an Admin.
404not_foundReceipt or invoice absent or in another organization.
404feature_disabledReceipts, statements or invoices are switched off on this deployment.
422validation_failedThe statement month has not ended yet.
422unsupported_filterAn unsupported query parameter.
502upstream_unavailableBilling data is temporarily unavailable. Retry.

We use strictly necessary cookies to record your choice and run this site. We do not use cookies for advertising or tracking. See the Cookie Policy.