Overview
Grantex budget controls let you allocate a monetary budget to any grant, debit amounts on each API call, and receive real-time alerts when spending thresholds are crossed. Key features:- Per-grant budget allocation with atomic debit operations
- Race-condition safe — uses SQL
WHERE remaining_budget >= amount - Threshold alerts at 50% and 80% via event bus (
budget.threshold) - Exhaustion events when the budget hits zero (
budget.exhausted) - JWT
bdgclaim — budget balance embedded in grant tokens for downstream enforcement
Allocating a Budget
Debiting from a Budget
Each debit atomically decrements the remaining balance. If the debit exceeds the remaining budget, the API returns 402 Payment Required with codeINSUFFICIENT_BUDGET.
Retrying a Debit Safely
The current SDK source sends a freshIdempotency-Key for each debit call and
reuses it across that call’s automatic retries. Supply your own 16-256 character
key when retrying the same operation across separate SDK calls. This source
change does not itself publish a new SDK package.
Server replay protection requires migration 139_budget_debit_idempotency.sql
and BUDGET_DEBIT_IDEMPOTENCY_ENABLED=true. The flag defaults off. With it on:
- The key is scoped to the authenticated developer and grant; only its SHA-256 hash is stored. The response and financial writes commit atomically.
- A matching retry within 24 hours returns the original
remaining,transactionIdandgrantId, even if later operations changed the balance. It does not create a ledger entry or emit another threshold event. - Changed amount, description or metadata returns
409 IDEMPOTENCY_CONFLICT. Object-key order does not matter; array order does. Omitted descriptions and metadata are treated asnulland{}respectively. - Revoked or expired grants remain denied. Another tenant cannot read or replay the response. Concurrent matching requests produce one debit.
- After 24 hours the key can identify a new operation. Never blindly retry an uncertain debit after that window: reconcile its transaction before deciding whether to submit a fresh operation. Use a fresh key for each new operation.
- Expired reply state for a grant is removed during its next successful keyed debit. Inactive grants may retain expired hashes until that happens or their grant/ledger data is removed; expiry is a replay deadline, not a claim that a background retention worker has deleted every row. Ledger history is retained.
Replay Rollout and Rollback
- Back up the database and verify restore, then deploy the complete migration
set through
139. This adds a new table and indexes only; no allocation or transaction backfill is required. Keep the flag off during code rollout. - Require the local real-Postgres retry, concurrency, rollback, tenant and expiry tests, SDK socket-failure E2E and all normal release/security gates to pass. Verify the exact deployed image and migration before approving activation.
- Obtain explicit operator approval for activation. Keep the setting consistent on every serving instance; a mixture of enabled and disabled instances is not a safe replay service. Existing customer flags and credentials are not changed by deploying the default-off code.
- Before activation, the previous image remains schema-compatible. After any keyed request has been accepted, do not blindly turn the flag off or send retries to an older image that ignores keys. Preserve replay rows and ledger history and use a deduplication-capable revision or an explicitly approved write-drain/reconciliation plan. Never drop replay state to make a rollback appear successful.
Handling Insufficient Budget
Checking Balance
Transaction History
Budget JWT Claim
When a grant has a budget allocation, the issued grant token includes abdg claim with the current remaining balance at the time of token issuance:
Threshold Alerts
Grantex automatically emits events when spending crosses thresholds:
These events are delivered via:
- Webhooks — to all registered webhook endpoints
- SSE/WebSocket — via the real-time event stream (
GET /v1/events/stream)
Portal Dashboard
Budget allocations are visible in the developer portal under Budgets:- Summary cards showing total allocated, spent, and remaining
- Per-grant usage bars with status badges (Healthy / > 50% / > 80% / Exhausted)
- Transaction drill-down with pagination