Getting Started
Add validated in-app purchases to your B4X app. The whole integration is a handful of lines — here's how to get up and running.
How it works
Your app uses b4xlib to handle purchases. When a user buys something, the library sends the receipt to our validator, which checks it directly with Google or Apple and returns the result. No server setup, no store APIs to deal with.
Set up in-app purchases on the stores
Before you can sell anything, you need an app listing and at least one product configured on Google Play or App Store Connect:
• Android— create your app in Google Play Console, upload a build to Internal Testing, and add your in-app products. You'll also need your Billing Key (Base64 RSA public key from Monetize → Monetization setup) for your B4A code. See the Android App Setup Guide.
• iOS— create an explicit Bundle ID, set up your app in App Store Connect, and add your in-app purchases. You'll also need your Shared Secret (from General → App Information) to upload in the next step. See the iOS App Setup Guide.
Register your app in the B4X Purchase Manager dashboard
Go to Applications → Register New App. Enter your App ID, name, platform, and package name.
An API key is generated automatically — you'll need it for your code. On your app's detail page, scroll down to Store Credentials:
• iOS — upload the Apple Shared Secret you got in Step 1. See the Apple Shared Secret Guide.
• Android— if you haven't already done so, invite our service account to your Google Play Console so we can verify purchases on your behalf. This only needs to be done once — it covers all your apps. Once set up, your app's detail page will show a green Status: Connected badge to confirm everything is working. See the Google Play Setup Guide.
Add b4xlib to your project and initialize
Add the library to your project, set your credentials, and initialize. That's the entire setup:
Download B4XPurchaseManager.b4xlibPrivate PurchaseManager As B4XPurchaseManager
' Credentials from the dashboard
Private Const APP_ID As String = "my-app"
Private Const API_KEY As String = "bpm_your_api_key_here"
#If B4A
' Android — from Play Console → Monetize → Monetization setup
Private Const BILLING_KEY As String = "MIIBIjANBgkq..."
#Else
' iOS — not used
Private Const BILLING_KEY As String = ""
#End If
Sub B4XPage_Created(Root1 As B4XView)
PurchaseManager.Initialize(Root1, BILLING_KEY, APP_ID, API_KEY)
PurchaseManager.AddProduct("your_product_id", "Premium Unlock", "Unlock all features")
End SubThe BILLING_KEY is your Base64 RSA public key from Google Play Console — iOS doesn't use it, so the #if directive handles that automatically.
Make a purchase
Three lines to show a purchase screen to your user:
' Show the purchase screen
Wait For (PurchaseManager.ShowInAppPurchase) Complete (Success As Boolean)
If Success Then
Log("Purchase successful!")
End IfThe library handles the entire purchase flow — showing the store UI, collecting payment, and validating the receipt with our server. You get back a true or false result.
Check purchase status
Use CheckStatus to check the cached purchase or subscription status. It only triggers a live server validation if the cache has expired:
' Check purchase/subscription status
PurchaseManager.CheckStatus
' For one-time purchases:
If PurchaseManager.IsUnlocked Then
' User has premium — show full features
End If
' For subscriptions:
If PurchaseManager.IsSubscriptionActive Then
' User is subscribed — show full features
End IfCall this on app resume, page appear, or before any gated action. It's instant — reads from the local cache with no blocking.
' For consumables:
If PurchaseManager.UseConsumables(1) Then
' Deducted successfully — grant the item
Else
' Not enough consumables — prompt to buy more
Wait For (PurchaseManager.ShowInAppPurchase) Complete (Success As Boolean)
If Success Then
PurchaseManager.AddConsumables(10) ' e.g. pack of 10
End If
End IfCheckStatus does not always call the server
CheckStatus reads from the local on-device cache first — it's instant and free to call as often as you like. A background server validation is only triggered automatically when the cache expires. Cache durations:
• New purchases — re-validated every 24 hours during the 14-day refund window
• Established purchases — re-validated every 14 days once past the refund window
• Subscriptions — re-validated every 24 hours to catch renewals and cancellations
• No purchase history — no automatic checks (zero API calls until the user makes a purchase or taps Restore)
All re-validation happens in the background — your UI is never blocked.
Testing
Use test/sandbox accounts so you're not charged real money:
• Android — Internal Testing + License Testers. See the Android App Setup Guide.
• iOS — TestFlight + Sandbox accounts. See the iOS App Setup Guide.
What's next?
• Check your Analytics to monitor validation requests and success rates
• See the Troubleshooting Guide if something's not working