Skip to content
AppDues
Log in
Contact salesSign up

Monetize an AI app with virtual currencies: setup, backend spending and sandbox tests

Use coins to charge for AI actions such as image generation, LLM responses or story generation, instead of relying only on subscriptions or one-time purchases. This guide covers creating a currency and product, showing a balance and paywall in a React Native app, checking and deducting balances on the backend, and testing the full flow.

Prerequisites and example shape

Before you start, confirm you have a project with working in-app purchases. For React Native, use react-native-purchases 9.1.0 or higher and react-native-purchases-ui 9.1.0 or higher. Other platforms should meet the minimum SDK versions in the virtual currency documentation, and capabilities can differ slightly by platform.

The worked example is a React Native app with Expo that generates cat-themed bedtime stories. Each story costs one coin. The app talks to a backend API route, and that backend also calls the AI provider, so balance checks and deductions stay server-side. The same pattern can be adapted in Swift, Flutter or Kotlin.

Create a product, then a currency

Open Product Catalog, then Virtual Currencies in the dashboard.

Create the sellable product first. Virtual currency must be associated with a product that exists in App Store Connect and Play Store, and it can be linked to either an in-app purchase or a subscription. In the example, create a consumable product with identifier 100_coin_pack and name 100 coins, then create the matching product in App Store Connect when prompted. A consumable can grant a fixed pack once; a subscription can grant an amount each billing period.

Then create the currency with code COINS, name Coins, and a description such as Used to generate AI responses. An icon is optional. Associate the product you just created. That association is the dashboard configuration needed to start.

For subscriptions, renewals, free trials, proration and upgrades or downgrades are handled automatically. If a subscription includes a grant, the customer receives the amount at the start of each billing period.

Show the balance and sell coins in the app

Initialize the SDK on the first screen, fetch the current COINS balance, display it, and present a paywall to buy more. After a successful purchase, refresh the balance so the cached amount updates. If loading fails, show a clear error instead of a stale zero.

Use the environment keys shown in protected module code0002: the public iOS SDK key is bundled with the app, while the project ID, secret API key and AI provider key must stay secret and never be exposed in client code. The secret API key is generated in the dashboard and used only by the backend.

Add a generate action that sends a POST request to the backend with the customer ID from the SDK. The app should not decide the balance itself; it asks the backend to perform the paid action and then updates its display from the response. Protected modules code0001 and code0003 contain the exact client contract for this step.

Check and deduct on the backend

Keep balance monitoring and deduction on the backend. If the client reported its own balance, a user could spoof it and call the AI feature without paying.

The backend flow is: validate that customerId is present; fetch all virtual currency balances for that customer and find the requested currency; if the balance is below the cost, return HTTP 402; otherwise generate the content; deduct exactly the cost with a negative adjustment; fetch the balance again; and return the generated content with the remaining balance so the app can refresh without a separate check.

In the example, the backend is an Expo API route. The story generation uses an AI chat completion with a calming cat-storyteller system prompt, a gentle user prompt, a token limit and a temperature setting. Streaming is possible but not required for the basic flow. Protected module code0004 is the private exact backend module and is referenced here only by behavior.

Test purchases, spending and edge cases

Decision checklist before shipping:

  • Confirm the product is associated with the correct currency in the dashboard.
  • Confirm you are inspecting the correct app user ID.
  • Confirm Sandbox Testing Access is not set to Nobody during purchase tests.
  • Verify the balance in the app and in the dashboard after a sandbox purchase.
  • Spend coins through the backend and confirm the balance drops in both places.
  • Try spending more than the balance and confirm the backend refuses and the UI explains it.
  • Simulate network failures and retries and confirm there is no double charge or double grant.

Sandbox Testing Access has three modes. Anybody lets all sandbox purchases grant entitlements and currency and is useful early in development. Allowed App User IDs only limits grants to allowlisted test accounts. Nobody records sandbox purchases without granting entitlements or currency. Once the flow works, consider moving from Anybody to Allowed App User IDs only so only QA accounts can manipulate sandbox balances.

Conclusion

When the pieces are combined, users can buy coins through a paywall, see their balance in real time, and spend coins on AI actions such as generating a story. Server-side checks and deductions keep the economy trustworthy, while the same model can extend to chatbots, image creation and other metered AI features.

Sandbox Testing Access modes
Sandbox modeEffectUse it when
AnybodyAll sandbox purchases grant entitlements and add virtual currencyEarly development and quick iteration
Allowed App User IDs onlyOnly allowlisted app user IDs receive entitlements and coins from sandbox purchasesLimiting tests to a small QA set
NobodySandbox purchases are recorded but do not grant entitlements or virtual currencyTesting purchase flows without polluting balances

Modes are controlled in Project settings under General, Sandbox Testing Access.

Frequently asked questions

Which name should I use in code?

The customer-facing name is In-App Currency, while SDK and API identifiers still use Virtual Currency for objects, functions, properties, fields, endpoints and events. Keep the documented technical names unchanged.

Can balances be transferred or merged between accounts?

Balances are non-transferable and permanently tied to the receiving Customer ID. Merging accounts, transferring balances or moving currency between users is not currently supported, so identity logic should be stable before granting currency.

How does sandbox access affect virtual currency grants?

Sandbox Testing Access controls whether test purchases grant entitlements and virtual currency: Anybody grants to all sandbox purchases, Allowed App User IDs only grants to allowlisted IDs, and Nobody records purchases without granting. Tightening the setting removes entitlements from users who no longer qualify, but virtual currency already granted stays.