Virtual Currencies
Soft and premium currencies with auto-recharge, balance caps, and transaction history.
Define virtual currencies for your app. Currencies can be soft (earned through gameplay) or premium (purchased with real money), with optional auto-recharge for time-gated mechanics.
SDK usage
Get balances
Get transactions
getTransactions takes the currency code and returns an array. For other pages, call GET /v1/client/currencies/transactions?currency_code=gold&limit=20&offset=20; its response carries total, offset and limit next to data.
Client API reference
| Method | Path | Description |
|---|---|---|
GET | /v1/client/currencies | Get all currency balances |
GET | /v1/client/currencies/transactions | Get transaction history (query: currency_code, limit, offset) |
MCP tools
| Tool | Description |
|---|---|
amba_currencies_create | Create a currency (soft/premium, auto-recharge, caps) |
amba_currencies_list | List all currencies |
amba_currencies_grant | Grant currency to a user (admin). Accepts idempotency_key for exactly-once grants. |
amba_currencies_spend | Debit currency from a user (admin). Atomic; rejects on insufficient funds. Accepts idempotency_key. |
amba_currencies_find_transaction | Look up the transaction a keyed grant/spend wrote ("did this spend land?") without spending again. |
amba_currencies_get_user_balance | Read a user's live per-currency balances. |
Example: Hearts system
Currency options
| Field | Type | Description |
|---|---|---|
code | string | Unique code (e.g., "gold", "gems") |
name | string | Display name |
is_premium | boolean | Premium (real money) currency |
initial_balance | number | Spendable starting balance for new users (default 0) — see below |
max_balance | number | Balance cap (null for unlimited) |
auto_recharge_amount | number | Amount to auto-recharge |
auto_recharge_interval_hours | number | Hours between recharges |
recharge_cap | integer | Ceiling auto-recharge refills to (null: recharge stops at max_balance) |
Refill to a lower ceiling than bought coins
max_balance bounds the whole balance. recharge_cap bounds only auto-recharge. Set both for "hearts refill to 5 for free, but hearts the player buys stack up to 99":
Recharge tops a balance up to recharge_cap and leaves a balance already at or above it alone; grants and purchases are bounded only by max_balance. With recharge_cap unset, recharge stops at max_balance. When both are set, recharge stops at the lower one.
How auto-recharge works
Auto-recharge is applied whenever a balance is read or written: the balance reads (client SDK and admin), grants, spends, store and inventory purchases, referral rewards, and event-triggered grants. Pending refills are added in the same database transaction as the read or write, under the balance row lock, so the balance you get back always includes them and two concurrent requests never credit the same interval twice. Nothing runs in the background, so a user who never opens the app costs nothing.
- Ceiling. Recharge refills up to
recharge_cap(ormax_balancewhen that is lower). Withoutrecharge_capthe ceiling ismax_balance, and without either recharge has no ceiling. - Catch-up. When a balance is touched, every whole interval since the last refill is credited at once:
n = floor(elapsed / auto_recharge_interval_hours)intervals addn × auto_recharge_amount, stopping at the ceiling. The clock advances by exactlynintervals, so the refill phase is kept: a heart due at 14:10 stays due on the :10. The total lands as onerechargerow in the transaction history. - The clock runs only below the ceiling. While the balance is at or above the ceiling, no time accrues. When a spend or purchase takes a full balance below the ceiling, the clock starts at that moment, and the first refill lands one interval later. A grant that leaves the balance below the ceiling keeps the running clock. A grant that lifts it to or past the ceiling (bought hearts stacking past 5) stops it.
- New users start at
initial_balance. A user with no balance yet holdsinitial_balance. Their clock starts on their first write wheninitial_balanceis below the ceiling. Setinitial_balanceequal torecharge_capto start every user full. - Changing the settings. A balance with no running clock (for example one written before recharge was configured) starts its clock the next time it's read or written. Nothing is credited for the time before that.
Every balance read and every grant/spend response carries next_recharge_at: when the next unit lands, as an ISO 8601 timestamp. It is null when the currency has no auto-recharge or the balance is at or above the ceiling. Show "next heart in 23m" from it, and read the balance again once that time passes:
Spendable starting balances
When a currency sets initial_balance > 0, that starting amount is real, spendable currency — not just a number the balance read displays. The first time a user is granted or spends the currency, Amba lazily credits the starting balance into the user's transaction history as a system transaction, then applies the grant or spend on top. From that point on, the balance read and the transaction history always agree: summing a user's transactions reconstructs their current balance.
This means a user can spend their starting balance immediately, even before their first grant. Currencies with initial_balance of 0 are unaffected — nothing is credited and the user starts empty.
Idempotent grants and spends
Both grants and spends accept an optional idempotency_key (string, ≤255 characters). It makes the operation exactly-once: if you replay a request with the same key for the same user and currency, Amba returns the original transaction with idempotent_replay: true and does not apply the balance change again.
The key is permanent — it's anchored to the ledger row it created, so there's no expiry window to manage. You control the dedup window entirely through what you encode into the key. Want to cap a reward at "one grant per share, per day"? Encode the day into the key.
Grants and spends are developer operations: call the amba_currencies_grant tool, or POST /v1/admin/projects/:projectId/currencies/grant from your server with a developer credential. The client SDK reads balances and transactions only.
The first call that day credits the balance and returns idempotent_replay: false. Every later call with the same key that day is a no-op replay that returns the original transaction — no double-grants, and no guard collection of your own to maintain. The next day's key is different, so the grant lands again.
One exception worth knowing: a spend that fails with insufficient funds stores no key (no ledger row was created), so retrying the same key after the user tops up is allowed and will go through.
What a replay returns
- A reused key replays at any amount. The key identifies the operation, so a repeat replays the original transaction whatever
amountit carries, with HTTP 200 andidempotent_replay: true. It never answers 409. Events and collection rows follow the same rule. Use a fresh key for a different operation. balanceis the original transaction'sbalance_after. Grants, spends, and recharges since then may have moved the balance, so a replay can reportbalance: 28while the user now holds 23. Every grant and spend response also carriescurrent_balance, the live balance read in the same request, so you never need a second read.
Did this spend land?
After a timeout or a dropped response, look the key up instead of guessing. GET /v1/admin/projects/:projectId/currencies/transactions/:userId?idempotency_key=spin%3A42 (or the amba_currencies_find_transaction tool) returns the transaction that key wrote, or an empty list. An empty list means the spend didn't land and retrying with the same key is safe. The lookup never spends.
Live balance
GET /v1/admin/projects/:projectId/currencies/balances/:userId returns the user's live balances, with pending recharge applied: { "data": [{ "app_user_id", "currency_code", "balance", "lifetime_earned", "lifetime_spent", "last_recharged_at", "next_recharge_at", "updated_at" }] }, one row per currency they hold. last_recharged_at is the start of the current recharge interval (null while the clock is stopped). The transaction history is newest first with ties (rows written together, like a fresh user's starting balance and first spend) broken by insertion order, so its first row is always the latest write.
Event-triggered grants
Grant currency automatically when a user action fires — no admin credentials needed on the client.
See Event-triggered Currency Grants for setup, grant modes, daily caps, and how it works.