Amba

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

const balances = await client.currencies.getBalance();
// Returns one entry per currency:
// [
//   { currency_code: "gold", name: "Gold Coins", balance: 500, is_premium: false,
//     lifetime_earned: 700, lifetime_spent: 200 },
//   { currency_code: "gems", name: "Gems", balance: 10, is_premium: true,
//     lifetime_earned: 10, lifetime_spent: 0 }
// ]

Get transactions

// Newest first; the first 50 transactions in that currency.
const transactions = await client.currencies.getTransactions('gold');

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

MethodPathDescription
GET/v1/client/currenciesGet all currency balances
GET/v1/client/currencies/transactionsGet transaction history (query: currency_code, limit, offset)

MCP tools

ToolDescription
amba_currencies_createCreate a currency (soft/premium, auto-recharge, caps)
amba_currencies_listList all currencies
amba_currencies_grantGrant currency to a user (admin). Accepts idempotency_key for exactly-once grants.
amba_currencies_spendDebit currency from a user (admin). Atomic; rejects on insufficient funds. Accepts idempotency_key.
amba_currencies_find_transactionLook up the transaction a keyed grant/spend wrote ("did this spend land?") without spending again.
amba_currencies_get_user_balanceRead a user's live per-currency balances.

Example: Hearts system

Agent: "Create a hearts currency that recharges 1 heart every 30 minutes, max 5"

amba_currencies_create({
  project_id: "proj_xxx",
  code: "hearts",
  name: "Hearts",
  description: "Lives for gameplay",
  is_premium: false,
  initial_balance: 5,
  max_balance: 5,
  auto_recharge_amount: 1,
  auto_recharge_interval_hours: 0.5
})

Currency options

FieldTypeDescription
codestringUnique code (e.g., "gold", "gems")
namestringDisplay name
is_premiumbooleanPremium (real money) currency
initial_balancenumberSpendable starting balance for new users (default 0) — see below
max_balancenumberBalance cap (null for unlimited)
auto_recharge_amountnumberAmount to auto-recharge
auto_recharge_interval_hoursnumberHours between recharges
recharge_capintegerCeiling 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":

{
  "code": "hearts",
  "name": "Hearts",
  "initial_balance": 5,
  "max_balance": 99,
  "recharge_cap": 5,
  "auto_recharge_amount": 1,
  "auto_recharge_interval_hours": 0.5
}

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 (or max_balance when that is lower). Without recharge_cap the ceiling is max_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 add n × auto_recharge_amount, stopping at the ceiling. The clock advances by exactly n intervals, so the refill phase is kept: a heart due at 14:10 stays due on the :10. The total lands as one recharge row 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 holds initial_balance. Their clock starts on their first write when initial_balance is below the ceiling. Set initial_balance equal to recharge_cap to 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:

{ "currency_code": "hearts", "balance": 3, "next_recharge_at": "2026-11-06T14:10:00.000Z" }

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.

// Grant 10 gold for sharing — but at most once per user per day.
amba_currencies_grant({
  project_id: 'proj_xxx',
  app_user_id: user.id,
  currency_code: 'gold',
  amount: 10,
  idempotency_key: `share:${user.id}:${todayYmd}`, // e.g. "share:u_123:2026-05-29"
});

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 amount it carries, with HTTP 200 and idempotent_replay: true. It never answers 409. Events and collection rows follow the same rule. Use a fresh key for a different operation.
  • balance is the original transaction's balance_after. Grants, spends, and recharges since then may have moved the balance, so a replay can report balance: 28 while the user now holds 23. Every grant and spend response also carries current_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.