Launch offer for the first 500 accounts: $10K MTR free for 24 months + $100 credit →
    .io
    Monetization
    Any platform
    Intermediate
    34 min

    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 COINS currency 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

    What you'll need

    • An app that already shows a BillDog paywall and makes purchases (iOS, Android)
    • A consumable in-app purchase in App Store Connect or Google Play (a coin pack)
    • A backend you control, to hold a secret key

    1. Understand who can change a balance

    3 min

    Balances 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/transactions with a bd_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
    1. 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.
    2. Open Monetization → Virtual Currencies and click Create Currency.
    3. 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.
    4. Enter a Display Name such as Gold Coins, and an optional Description. Keep Active on.
    5. 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.
    6. 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 min

    Sell 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 min

    Register 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.

    iOS (Swift)
    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")))
        }
    }
    build.gradle.kts
    dependencies {
        implementation("io.billdog:billdog-virtual-currency:1.2.0-beta.1")
    }
    Android (Kotlin)
    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 min

    Create 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:

    Terminal
    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_id is the ID your app passed to identify. You can send BillDog's customer_id instead. The customer must already exist: this call never creates one, and an unknown user is a 404.
    • type is SPEND to debit, or GRANT or EARN to credit (a daily reward, a referral bonus).
    • amount must be a positive whole number. Fractions are refused.
    • A spend that would take the balance below zero fails with VALIDATION_ERROR and "Insufficient balance", and nothing is debited.
    • Send a fresh idempotency_key for each user action and resend the same key on retries. A repeat with the same key moves nothing and returns idempotent_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 min

    Buy 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:

    1. Run the app with your bd_public_test_ key and call identify with a test user ID, so the sandbox customer exists.
    2. 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 of 100 and click Grant Currency. You can also send the curl call above with "type": "GRANT" and your bd_secret_test_ key.
    3. Refresh the balance in the app. It shows 100.
    4. Run your server's spend. The app shows 70 after the next fetch, and the customer's Virtual Currencies tab lists both transactions.
    5. Run the same spend again with the same idempotency_key. The balance stays at 70 and the answer says idempotent_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.

    Frequently asked questions

    No. Writes need your project's secret key, and the server refuses them from a public key. The old SDK write methods (addBalance and deductBalance on iOS, addCurrency and deductCurrency on Android, grantCurrency on both) are deprecated and always throw. The app reads balances and history; your server grants and spends with POST /v1/virtual-currency/transactions.

    Yes, for sandbox customers: an App Store sandbox, Google Play license tester or Stripe test-mode purchase made with a bd_public_test_ key grants the product's Units on Purchase. Test Store and SDK simulated purchases don't, and nor do purchases in sample or demo projects; grant coins by hand from the customer's Virtual Currencies tab instead. A sandbox purchase in a build that uses your production key (App Review, TestFlight) is treated like its entitlements: by default it grants coins too.

    Send an idempotency_key with each spend and reuse it when you retry the same action. BillDog applies the first request and answers repeats with idempotent_replay set to true, without moving the balance again. Reusing a key for a different customer or currency is an error.

    A refund never grants coins, and coins already granted are not taken back automatically. If you want to claw them back, listen for the refund on your server and call the transactions endpoint with type SPEND, keeping in mind it fails rather than going below zero.

    Only when no customer holds a balance in it, and then it is archived, not erased, so its ledger stays. Turning off Active stops every grant and spend in that currency, so zero out balances first (let customers spend them, or deduct them from the customer's page) before you retire it.

    Reference

    Ship it on a free project

    Create a project, grab your SDK key and follow along. Sandbox purchases and data stay separate from production, so test transactions never mix with real revenue.

    We use cookies and process your data to provide our services. By continuing, you agree to our Terms and Privacy Policy.