Sell coins and credits with virtual currency
To sell coins with BillDog, create a currency under Monetization → Virtual Currencies, link a consumable product to it with Units on Purchase, and sell that product as usual. BillDog credits the coins when a production store purchase is recorded. Your app reads the balance with getBalance(currencyId:) on iOS or getCurrencyBalance on Android, and your server spends coins with POST /v1/virtual-currency/transactions and a secret key. Apps can't change a balance themselves.
Steps last checked against the SDK source on 16 Nov 2026.
What you'll build
- A
COINScurrency that a consumable coin pack tops up - The current balance shown in your iOS or Android app
- A server endpoint that spends coins safely, once per request
- A sandbox test of the whole loop, from purchase to spend
1. Understand who can change a balance
3 minBalances live on BillDog's server, one per customer and currency. Three things can change one:
- A purchase. When a store purchase of a linked product is recorded, BillDog grants that product's Units on Purchase, multiplied by the quantity bought.
- Your server. A call to
/v1/virtual-currency/transactionswith abd_secret_key grants or spends coins for a customer you name. - A dashboard member. The Grant and Deduct buttons on a customer's Virtual Currencies tab.
The app itself only reads. A public key ships inside every copy of your app, so the server answers any write made with it with "Public keys cannot make write operations". The SDKs still contain the old write methods (addBalance and deductBalance on iOS, addCurrency and deductCurrency on Android, grantCurrency on both), but they are deprecated and always throw. When a user spends coins in your app, the app asks your server, and your server spends them.
2. Create the currency and link the coin pack
6 min- Open Monetization → Products and click New Product. Set Product Type to Consumable and put your coin pack's store IDs in App Store Product ID and Play Store Product ID. The App Store setup and Google Play setup codelabs cover the store side.
- Open Monetization → Virtual Currencies and click Create Currency.
- Enter a Currency Code such as
COINS: 2 to 10 uppercase letters or digits. Codes that match a real currency (USD,EUR) are refused. You can't change the code later, and it's what your code uses. - Enter a Display Name such as
Gold Coins, and an optional Description. Keep Active on. - Under Linked Products, click Add First Product. Choose the coin pack under Product and set Units on Purchase to the number of coins it grants, such as
100. Click Add Product to link more packs. - Click Create Currency.
Auto-expire on billing cycle end is for subscriptions that grant coins each period: the coins from a period expire when that period ends, so unused ones don't roll over. A coin pack has no billing period, so the switch does nothing for it. Leave it off.
Units on Trial Start only matters for subscriptions that open with a free trial. Leave it at 0 for a coin pack.
3. Sell the coin pack
5 minSell the consumable the same way you sell anything else: add it to an offering as a package and give a paywall button a Purchase action (Set up the product, paywall and placement). A coin pack has no entitlement to map, so skip that part.
When the purchase is recorded, BillDog grants the coins:
- Once per purchase. The grant is keyed on the purchase and the currency, so a redelivered store notification never pays twice.
- For a new purchase only. Restores and refunds grant nothing.
- To the customer who owns the purchase, which is the user the app has identified (or its anonymous ID).
Store sandbox purchases grant coins to sandbox customers, the ones your bd_public_test_ key creates. That covers App Store sandbox, Google Play license testers and Stripe test mode. Purchases from the Test Store and the SDK's simulated purchases don't grant coins, and nor does any purchase in a sample or demo project. For those, use a manual grant (the last step).
4. Show the balance in your app
6 minRegister the virtual currency module once, after configure. The balance belongs to whoever the SDK is signed in as: the device's anonymous user, or the user you passed to identify. You can pass the currency code (COINS) or its ID.
import BillDogFull
// Once at launch, right after BillDog.shared.configure(config:)
BillDog.shared.use(module: BillDogVirtualCurrencyManager.shared)
// Anywhere you show the wallet
func refreshCoins() async {
let wallet = BillDogVirtualCurrencyManager.shared
do {
let coins = try await wallet.getBalance(currencyId: "COINS")
showCoins(Int(coins)) // your own UI
} catch {
// Offline or refused: the last balance fetched for this user
showCoins(Int(wallet.getCachedBalance(currencyId: "COINS")))
}
}dependencies {
implementation("io.billdog:billdog-virtual-currency:1.2.0-beta.1")
}import android.app.Application
import io.billdog.core.BillDog
import io.billdog.virtualcurrency.VirtualCurrencyManager
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
BillDog.configure(this, "bd_public_test_YOUR_KEY")
BillDog.use(wallet)
}
companion object {
val wallet = VirtualCurrencyManager()
}
}
// In an Activity:
// lifecycleScope.launch {
// try {
// val coins = MyApp.wallet.getCurrencyBalance("COINS")
// showCoins(coins.toInt()) // your own UI
// } catch (e: Exception) {
// // Not configured, offline, or the server refused
// }
// }To list every active currency with its balance, call getAllCurrencies() on either platform. For the ledger, call getTransactionHistory(currencyId:limit:) on iOS or getTransactionHistory(currencyId, limit) on Android. Fetch the balance again after a purchase or a spend: the SDK does not push balance changes.
5. Spend coins from your server
8 minCreate a secret key under Integration & Data → API Keys with Key Type: Secret (Server), one for each environment. When the user taps "Unlock level 7", the app calls your backend, and the backend spends the coins:
curl -X POST https://api.billdog.io/v1/virtual-currency/transactions \
-H "X-BillDog-API-Key: bd_secret_test_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{
"app_user_id": "user_123",
"currency_id": "COINS",
"type": "SPEND",
"amount": 30,
"reason": "unlock_level_7",
"idempotency_key": "user_123-unlock-level-7"
}'On success, data holds new_balance, transaction_id, type and idempotent_replay. Unlock the content only after this answer comes back.
app_user_idis the ID your app passed toidentify. You can send BillDog'scustomer_idinstead. The customer must already exist: this call never creates one, and an unknown user is a 404.typeisSPENDto debit, orGRANTorEARNto credit (a daily reward, a referral bonus).amountmust be a positive whole number. Fractions are refused.- A spend that would take the balance below zero fails with
VALIDATION_ERRORand "Insufficient balance", and nothing is debited. - Send a fresh
idempotency_keyfor each user action and resend the same key on retries. A repeat with the same key moves nothing and returnsidempotent_replay: true.
To move several currencies at once, such as 50 gems for 200 coins, call POST /v1/virtual-currency/adjustments with legs: an array of { "currency_id", "amount" }, where a negative amount debits. All legs apply together or not at all. BillDog holds no exchange rate, so you state both amounts.
6. Test the loop in sandbox
6 minBuy the coin pack with a store sandbox account (iOS, Google Play) in a build that uses your bd_public_test_ key, or stand in for the purchase with a manual grant:
- Run the app with your
bd_public_test_key and callidentifywith a test user ID, so the sandbox customer exists. - Buy the coin pack with your sandbox account. Or switch the dashboard header to Sandbox, open Audience → Customers and find that user. On the Virtual Currencies tab, click Grant, choose
COINS, enter an Amount of100and click Grant Currency. You can also send the curl call above with"type": "GRANT"and yourbd_secret_test_key. - Refresh the balance in the app. It shows 100.
- Run your server's spend. The app shows 70 after the next fetch, and the customer's Virtual Currencies tab lists both transactions.
- Run the same spend again with the same
idempotency_key. The balance stays at 70 and the answer saysidempotent_replay: true.
After a sandbox purchase, the customer's Virtual Currencies tab (with the header on Sandbox) shows the grant, and the balance went up by Units on Purchase. Sandbox coins stay in sandbox: they're on the sandbox customer and aren't counted in production stats.