Amba

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

MethodPathDescription
POST/admin/projects/:projectId/currenciesCreate a currency definition.
GET/admin/projects/:projectId/currenciesList currency definitions.
PATCH/admin/projects/:projectId/currencies/:currencyIdPartial update.
DELETE/admin/projects/:projectId/currencies/:currencyIdDelete.
POST/admin/projects/:projectId/currencies/grantGrant currency to a user (admin source).
POST/admin/projects/:projectId/currencies/spendDebit currency from a user (admin source).
GET/admin/projects/:projectId/currencies/transactions/:userIdPaginated transaction history for a user; filter by idempotency_key to check whether a grant/spend landed.
GET/admin/projects/:projectId/currencies/balances/:userIdLive per-currency balances for a user.

POST /admin/projects/:projectId/currencies

Request

FieldTypeRequiredDefault
codestringyes—
namestringyes—
descriptionstringnonull
is_premiumbooleannofalse
initial_balancenumberno0
max_balancenumbernonull (uncapped)
auto_recharge_amountnumbernonull
auto_recharge_interval_hoursnumbernonull
recharge_capintegernonull

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

{
  "data": {
    "id": "…",
    "code": "gems",
    "name": "Gems",
    "is_premium": false,
    "initial_balance": 0,
    "max_balance": 9999
  }
}

Try it:

POST/admin/projects/%7B%7BprojectId%7D%7D/currencies
developer auth
curl -X POST 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies' \
  -H 'Content-Type: application/json' \
  -d '{
  "code": "gems",
  "name": "Gems"
}'
Loading auth… Configure auth in the settings drawer (top-right) to run this request.

Curl:

curl -X POST "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies" \
  -H "Authorization: Bearer ${DEV_TOKEN}" \
  -H 'Content-Type: application/json' \
  -d '{"code":"gems","name":"Gems"}'

GET /admin/projects/:projectId/currencies

{ "data": [{ "code": "gems", "name": "Gems", "is_premium": false }] }

Try it:

GET/admin/projects/%7B%7BprojectId%7D%7D/currencies
developer auth
curl -X GET 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies'
Loading auth… Configure auth in the settings drawer (top-right) to run this request.

Curl:

curl -X GET "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies" \
  -H "Authorization: Bearer ${DEV_TOKEN}"

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:

PATCH/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7D
developer auth
curl -X PATCH 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7D'
Loading auth… Configure auth in the settings drawer (top-right) to run this request.

Curl:

curl -X PATCH "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies/{currencyId}" \
  -H "Authorization: Bearer ${DEV_TOKEN}" \
  -H 'Content-Type: application/json' \
  -d '{}'

DELETE /admin/projects/:projectId/currencies/:currencyId

{ "data": { "deleted": true } }

Try it:

DELETE/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7D
developer auth
curl -X DELETE 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/%7B%7BcurrencyId%7D%7D'
Loading auth… Configure auth in the settings drawer (top-right) to run this request.

Curl:

curl -X DELETE "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies/{currencyId}" \
  -H "Authorization: Bearer ${DEV_TOKEN}"

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)

FieldTypeRequiredDescription
app_user_iduuidyesRecipient.
currency_codestringyesMust exist in currency_definitions.
amountnumberyesMust be finite and > 0.
reasonstringnoFree-form reference id stored on the transaction.
idempotency_keystringno≤255 chars. Repeating the same key for this user + currency replays the original transaction without re-applying the balance change. See Idempotency.

Response 201

{
  "data": {
    "balance": 150,
    "current_balance": 150,
    "next_recharge_at": null,
    "idempotent_replay": false,
    "transaction": {
      "id": "…",
      "app_user_id": "…",
      "currency_code": "gems",
      "amount": 50,
      "balance_after": 150,
      "transaction_type": "grant",
      "source": "admin",
      "reference_id": null,
      "created_at": "…"
    }
  }
}

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 — unknown currency_code.
  • 500 GRANT_FAILED.

Try it:

POST/admin/projects/%7B%7BprojectId%7D%7D/currencies/grant
developer auth
curl -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
}'
Loading auth… Configure auth in the settings drawer (top-right) to run this request.

Curl:

curl -X POST "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies/grant" \
  -H "Authorization: Bearer ${DEV_TOKEN}" \
  -H 'Content-Type: application/json' \
  -d '{"app_user_id":"<app_user_id>","currency_code":"gems","amount":100}'

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)

FieldTypeRequiredDescription
app_user_iduuidyesUser to debit.
currency_codestringyesMust exist in currency_definitions.
amountnumberyesPositive, finite. The endpoint records it as a negative delta on the ledger.
reasonstringnoFree-form reference id stored on the transaction.
idempotency_keystringno≤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

{
  "data": {
    "balance": 70,
    "current_balance": 70,
    "next_recharge_at": null,
    "idempotent_replay": false,
    "transaction": {
      "id": "…",
      "app_user_id": "…",
      "currency_code": "gems",
      "amount": -30,
      "balance_after": 70,
      "transaction_type": "spend",
      "source": "admin",
      "reference_id": "ai_generation",
      "created_at": "…"
    }
  }
}

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 below amount. The error message includes both values. No key is stored, so the same idempotency_key may be retried after the user tops up.
  • 404 CURRENCY_NOT_FOUND — unknown currency_code.
  • 500 SPEND_FAILED.

Try it:

POST/admin/projects/%7B%7BprojectId%7D%7D/currencies/spend
developer auth
curl -X POST 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/spend'
Loading auth… Configure auth in the settings drawer (top-right) to run this request.

Curl:

curl -X POST "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies/spend" \
  -H "Authorization: Bearer ${DEV_TOKEN}" \
  -H 'Content-Type: application/json' \
  -d '{
        "app_user_id": "…",
        "currency_code": "gems",
        "amount": 30,
        "reason": "ai_generation"
      }'

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 201 with "idempotent_replay": false.
  • Any later request with the same key returns the original transaction unchanged, responds 200 with "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 (or reason) the repeat carries, and never answers 409. 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.
  • balance on a replay is historical. It is the original transaction's balance_after. Grants, spends, and recharges since then may have moved the balance, so a replay of a 28-coin purchase can report balance: 28 while the user now holds 23. Read current_balance for the live balance; it is read in the same request.

Every grant and spend response has the same shape:

FieldDescription
balanceThe transaction's balance_after. On a replay, the balance right after the original write.
current_balanceThe user's live balance for this currency, read in the same request (pending auto-recharge included).
next_recharge_atWhen 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_replaytrue when the key matched an earlier transaction, false otherwise.
transactionThe 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

ParamDefaultDescription
limit50Page size.
offset0Rows 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?

curl "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies/transactions/${USER_ID}?idempotency_key=spin%3A42&currency_code=coins" \
  -H "Authorization: Bearer ${DEV_TOKEN}"

{ "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

{
  "data": [
    {
      "id": "…",
      "currency_code": "gems",
      "amount": 50,
      "balance_after": 150,
      "transaction_type": "grant",
      "idempotency_key": null,
      "seq": "1042",
      "created_at": "…"
    }
  ],
  "total": 42,
  "offset": 0,
  "limit": 50
}

Try it:

GET/admin/projects/%7B%7BprojectId%7D%7D/currencies/transactions/%7B%7BuserId%7D%7D
developer auth
curl -X GET 'https://api.amba.dev/v1/admin/projects/%7B%7BprojectId%7D%7D/currencies/transactions/%7B%7BuserId%7D%7D'
Loading auth… Configure auth in the settings drawer (top-right) to run this request.

Curl:

curl -X GET "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies/transactions/{userId}" \
  -H "Authorization: Bearer ${DEV_TOKEN}"

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

ParamDescription
currency_idOnly this currency (definition id).

Response 200

{
  "data": [
    {
      "app_user_id": "…",
      "currency_code": "coins",
      "balance": 23,
      "lifetime_earned": 33,
      "lifetime_spent": 10,
      "last_recharged_at": "2026-11-06T13:10:00.000Z",
      "next_recharge_at": "2026-11-06T14:10:00.000Z",
      "updated_at": "…"
    }
  ]
}

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 — unknown currency_id.
  • 500 FETCH_FAILED.

Curl:

curl -X GET "${BASE_URL}/admin/projects/${PROJECT_ID}/currencies/balances/{userId}" \
  -H "Authorization: Bearer ${DEV_TOKEN}"