Currencies
Virtual currency definitions, admin grants, per-user transaction history.
Currencies are named tokens (code like "gems") with an optional balance cap (max_balance), auto-recharge rules, and an optional recharge ceiling (recharge_cap). user_balances holds per-user state. Grants are performed atomically under a FOR UPDATE row lock with a pre-flight zero-row insert so first-time grants can't race.
Endpoints
| Method | Path | Description |
|---|---|---|
| POST | /admin/projects/:projectId/currencies | Create a currency definition. |
| GET | /admin/projects/:projectId/currencies | List currency definitions. |
| PATCH | /admin/projects/:projectId/currencies/:currencyId | Partial update. |
| DELETE | /admin/projects/:projectId/currencies/:currencyId | Delete. |
| POST | /admin/projects/:projectId/currencies/grant | Grant currency to a user (admin source). |
| POST | /admin/projects/:projectId/currencies/spend | Debit currency from a user (admin source). |
| GET | /admin/projects/:projectId/currencies/transactions/:userId | Paginated transaction history for a user; filter by idempotency_key to check whether a grant/spend landed. |
| GET | /admin/projects/:projectId/currencies/balances/:userId | Live per-currency balances for a user. |
POST /admin/projects/:projectId/currencies
Request
| Field | Type | Required | Default |
|---|---|---|---|
code | string | yes | — |
name | string | yes | — |
description | string | no | null |
is_premium | boolean | no | false |
initial_balance | number | no | 0 |
max_balance | number | no | null (uncapped) |
auto_recharge_amount | number | no | null |
auto_recharge_interval_hours | number | no | null |
recharge_cap | integer | no | null |
recharge_cap is the ceiling auto-recharge refills to. Grants and purchases are bounded only by max_balance, so a currency can "refill to 5" while bought coins stack past 5. When recharge_cap is null, recharge stops at max_balance (the behavior before recharge_cap existed). When both are set, recharge stops at the lower of the two. A balance already at or above the ceiling is left alone by recharge. Recharge is applied lazily, on every balance read and write; see How auto-recharge works for the clock rules.
When initial_balance > 0, that starting amount is spendable. On a user's first grant or spend of this currency, Amba lazily credits the starting balance into the ledger as a system transaction before applying the operation, so the balance read and the summed transaction history stay in agreement and the starting amount can be spent immediately. A currency with initial_balance of 0 credits nothing — the user starts empty.
Response 201
Try it:
/admin/projects/%7B%7BprojectId%7D%7D/currenciescurl -X POST 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies' \
-H 'Content-Type: application/json' \
-d '{
"code": "gems",
"name": "Gems"
}'Curl:
GET /admin/projects/:projectId/currencies
Try it:
/admin/projects/%7B%7BprojectId%7D%7D/currenciescurl -X GET 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies'Curl:
PATCH /admin/projects/:projectId/currencies/:currencyId
Allowed fields: name, description, is_premium, initial_balance, max_balance, auto_recharge_amount, auto_recharge_interval_hours, recharge_cap. Send "recharge_cap": null to clear it.
Try it:
/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7Dcurl -X PATCH 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7D'Curl:
DELETE /admin/projects/:projectId/currencies/:currencyId
Try it:
/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7Dcurl -X DELETE 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7D'Curl:
POST /admin/projects/:projectId/currencies/grant
Grant currency to a user. Clamped to max_balance if set; a fully-clamped grant still returns 201 with a zero-delta currency_transactions row for auditability.
Request (GrantCurrencyInput)
| Field | Type | Required | Description |
|---|---|---|---|
app_user_id | uuid | yes | Recipient. |
currency_code | string | yes | Must exist in currency_definitions. |
amount | number | yes | Must be finite and > 0. |
reason | string | no | Free-form reference id stored on the transaction. |
idempotency_key | string | no | ≤255 chars. Repeating the same key for this user + currency replays the original transaction without re-applying the balance change. See Idempotency. |
Response 201
Response 200 (idempotent replay)
A repeat with a previously-seen idempotency_key (same user + currency) returns the original transaction with "idempotent_replay": true and HTTP 200. The balance is not changed. balance is the original transaction's balance_after; current_balance is the live balance. See Idempotency.
Errors
400 INVALID_AMOUNT— amount isn't a positive finite number.404 CURRENCY_NOT_FOUND— unknowncurrency_code.500 GRANT_FAILED.
Try it:
/admin/projects/%7B%7BprojectId%7D%7D/currencies/grantcurl -X POST 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/grant' \
-H 'Content-Type: application/json' \
-d '{
"app_user_id": "<app_user_id>",
"currency_code": "gems",
"amount": 100
}'Curl:
POST /admin/projects/:projectId/currencies/spend
Debit currency from a user. Atomic — returns 400 INSUFFICIENT_FUNDS if the balance is too low. No partial debit; the request either fully succeeds or fully fails.
The ledger records the spend as a negative amount (signed delta), so SUM(amount) across a user's transactions reconstructs the current balance.
Request (SpendCurrencyInput)
| Field | Type | Required | Description |
|---|---|---|---|
app_user_id | uuid | yes | User to debit. |
currency_code | string | yes | Must exist in currency_definitions. |
amount | number | yes | Positive, finite. The endpoint records it as a negative delta on the ledger. |
reason | string | no | Free-form reference id stored on the transaction. |
idempotency_key | string | no | ≤255 chars. Repeating the same key for this user + currency replays the original transaction without re-debiting. A spend that failed with INSUFFICIENT_FUNDS stores no key, so that key can be retried. See Idempotency. |
Response 201
Response 200 (idempotent replay)
A repeat with a previously-seen idempotency_key (same user + currency) returns the original transaction with "idempotent_replay": true and HTTP 200. The balance is not changed. balance is the original transaction's balance_after; current_balance is the live balance. See Idempotency.
Errors
400 INVALID_AMOUNT— amount isn't a positive finite number.400 INSUFFICIENT_FUNDS— current balance is belowamount. The error message includes both values. No key is stored, so the sameidempotency_keymay be retried after the user tops up.404 CURRENCY_NOT_FOUND— unknowncurrency_code.500 SPEND_FAILED.
Try it:
/admin/projects/%7B%7BprojectId%7D%7D/currencies/spendcurl -X POST 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/spend'Curl:
Idempotency
Both grant and spend accept an optional idempotency_key (string, ≤255 chars). Sending the same key again for the same app_user_id + currency_code makes the operation exactly-once:
- The first request applies the balance change and responds
201with"idempotent_replay": false. - Any later request with the same key returns the original transaction unchanged, responds
200with"idempotent_replay": true, and does not re-apply the delta.
Two parts of the replay contract matter when you retry:
- A replay ignores the repeated request's amount. A reused key replays the original transaction whatever
amount(orreason) the repeat carries, and never answers409. This matches every other idempotent Amba write (events and collection rows): the key identifies the operation, and the first request's effect is the answer. Use a fresh key for a different operation. Grants and spends share one key space per user and currency, so a spend sent with a key a grant already used replays that grant. balanceon a replay is historical. It is the original transaction'sbalance_after. Grants, spends, and recharges since then may have moved the balance, so a replay of a 28-coin purchase can reportbalance: 28while the user now holds 23. Readcurrent_balancefor the live balance; it is read in the same request.
Every grant and spend response has the same shape:
| Field | Description |
|---|---|
balance | The transaction's balance_after. On a replay, the balance right after the original write. |
current_balance | The user's live balance for this currency, read in the same request (pending auto-recharge included). |
next_recharge_at | When the next auto-recharge unit lands (ISO 8601), or null when the currency has no auto-recharge or current_balance is at its recharge ceiling. |
idempotent_replay | true when the key matched an earlier transaction, false otherwise. |
transaction | The ledger row (the original row on a replay). |
To learn whether a keyed grant or spend landed without sending it again (after a timeout, say), look it up with GET …/transactions/:userId?idempotency_key=…: one row means it landed, an empty list means it didn't and the same key is safe to retry.
The key is permanent — it's anchored to the ledger row it created, with no expiry window. The caller controls the dedup window through what they encode into the key. To cap a reward at "one grant per share per day," embed the date: idempotency_key: "share:{userId}:{yyyy-mm-dd}". The first call that day lands; the rest are no-op replays; the next day's key is different so the grant lands again — no guard collection of your own required.
A spend that fails with INSUFFICIENT_FUNDS stores no key (no ledger row was written), so the same key can be retried after the user tops up.
The same idempotency_key field is available on the amba_currencies_grant and amba_currencies_spend MCP tools, with identical semantics. amba_currencies_find_transaction is the MCP lookup.
GET /admin/projects/:projectId/currencies/transactions/:userId
Paginated transaction history for a single user, most recent first. Rows are ordered by (created_at DESC, seq DESC): rows written in one database transaction share created_at (a fresh user's initial_balance seed row and their first spend, for example), and seq is the insertion sequence that breaks the tie, so the first row is always the latest write.
Query
| Param | Default | Description |
|---|---|---|
limit | 50 | Page size. |
offset | 0 | Rows to skip. |
currency_id | — | Only this currency (definition id). |
currency_code | — | Only this currency (code, e.g. coins). |
idempotency_key | — | Only the transaction a grant/spend wrote under this key. Returns one row, or an empty list. |
Did this spend land?
{ "data": [ { …, "idempotency_key": "spin:42", "amount": -3 } ], "total": 1 } means it landed. { "data": [], "total": 0 } means it didn't, so retrying the spend with the same key is safe. The lookup never spends.
Response 200
Try it:
/admin/projects/%7B%7BprojectId%7D%7D/currencies/transactions/%7B%7BuserId%7D%7Dcurl -X GET 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/transactions/%7B%7BuserId%7D%7D'Curl:
GET /admin/projects/:projectId/currencies/balances/:userId
The user's live balances: the materialized value grants, spends, purchases, and recharges update, which is what the client SDK reads. Pending auto-recharge is applied on this read. One row per currency the user holds; an empty array (HTTP 200) when they hold none.
Query
| Param | Description |
|---|---|
currency_id | Only this currency (definition id). |
Response 200
last_recharged_at is the start of the current recharge interval, null while the clock is stopped. next_recharge_at is when the next unit lands, null when the currency has no auto-recharge or the balance is at or above its recharge ceiling.
Grant and spend responses already carry current_balance, so this read is for outside a mutation.
Errors
404 CURRENCY_NOT_FOUND— unknowncurrency_id.500 FETCH_FAILED.
Curl: