In-app purchases

Run App Store and Google Play purchase and consume flows, then read the result from an event.

Read results from events, not the call

Examples reuse the single browser client from Set up the client. Adjust the ~/lib/bdk import to your app’s path; do not create a client in each component or handler.

Awaiting purchaseIos or purchaseAndroid only confirms the command was sent — the result arrives later on purchaseSuccess or purchaseFailed. Subscribe before you call. For the rest of the iap namespace, use the envelope-based helpers on Sell products & subscriptions.

Every purchase and consume takes { id, type }: id is the App Store / Play product identifier, and type is "product" (one-time / consumable) or "subscription". Use the Ios / Android method for the device you're on.

The payload key is id, not product_id.

Results from the device are unverified. Treat purchaseSuccess as "the store reported a transaction", then verify the receipt server-side before granting entitlements. See Verify in-app purchases.

Buy a product on iOS

Opens the App Store purchase sheet. The result lands on purchaseSuccess, or purchaseFailed on cancel or error — both carry { platform, data }.

Wrap the call in try/catch. If it fails, show the normal purchase recovery UI and reconcile with your backend; do not retry a purchase or consumption blindly.

The examples use your app’s showActionUnavailable() UI handler.

import { bdk } from "~/lib/bdk";

try {
  bdk.on("purchaseSuccess", ({ platform, data }) => {
    // data is the raw, UNVERIFIED store payload — send it to your server to verify.
    console.log("purchased on", platform, data);
  });

  bdk.on("purchaseFailed", ({ data }) => {
    console.warn("purchase failed or cancelled", data);
  });

  await bdk.iap.purchaseIos({ id: "pro_monthly", type: "subscription" });
} catch {
  showActionUnavailable();
}

Buy a product on Android

Opens the Google Play purchase sheet. Results surface on the same purchaseSuccess / purchaseFailed events; the platform field tells you which store they came from.

Wrap the call in try/catch. If it fails, show the normal purchase recovery UI and reconcile with your backend; do not retry a purchase or consumption blindly.

The examples use your app’s showActionUnavailable() UI handler.

try {
  bdk.on("purchaseSuccess", ({ platform, data }) => {
    if (platform === "android") void verifyOnServer(data);
  });

  await bdk.iap.purchaseAndroid({ id: "coins_100", type: "product" });
} catch {
  showActionUnavailable();
}

Buy from one shared codebase

Pick the right method after device info arrives. Keep a web fallback and handle a failed call without retrying the purchase.

The example uses your app’s showActionUnavailable() UI handler.

async function buy(id: string, type: "product" | "subscription") {
  const os = (await bdk.ready(3000))?.deviceOS?.toLowerCase();
  try {
    if (os === "ios") return await bdk.iap.purchaseIos({ id, type });
    if (os === "android") return await bdk.iap.purchaseAndroid({ id, type });
  } catch {
    showActionUnavailable();
  }
  // Not running natively — show a web fallback / upsell.
}

await buy("pro_monthly", "subscription");

Consume a product so it can be bought again

Use for consumables (coins, lives, refills) — a consumable can't be repurchased until it's consumed.

Wrap the call in try/catch. If it fails, show the normal purchase recovery UI and reconcile with your backend; do not retry a purchase or consumption blindly.

Consuming on the device only updates local state. The authoritative consume for Google Play happens server-side — see Verify in-app purchases.

The examples use your app’s showActionUnavailable() UI handler.

try {
  await bdk.iap.consumeAndroidPurchase({ id: "coins_100", type: "product" });
} catch {
  showActionUnavailable();
}

IAP events reference

Which event answers which call. Each carries the platform ("ios" or "android") and a data object with the store's purchase details — product id, transaction id, receipt data, and result/error codes. Treat data as the raw store payload and verify it server-side. bdk.on(...) returns an unsubscribe function.

EventFired by
purchaseSuccesspurchaseIos / purchaseAndroid succeeded
purchaseFailedpurchase cancelled or errored
receiptReceiveda store receipt / token was returned
const offs = [
  bdk.on("purchaseSuccess", (e) => console.log("success", e.platform, e.data)),
  bdk.on("purchaseFailed", (e) => console.log("failed", e.data)),
  bdk.on("receiptReceived", (e) => console.log("receipt", e.data))
];

// later
offs.forEach((off) => off());

Verify on the server

The step that actually grants entitlements. Send the data from purchaseSuccess / receiptReceived to your backend and validate it. See Verify in-app purchases for verifyIosReceipt, verifyAndroidReceipt, readAndroidReceipt, and server-side consumeAndroidPurchase.

import { verifyAndroidReceipt } from "@thebdk/native/server/iap";

// In your API handler, with the purchaseToken forwarded from the client:
const validation = await verifyAndroidReceipt({
  packageName: "com.example.app",
  productId: "pro_monthly",
  purchaseToken,
  productType: "subscription"
});

if (validation.isValid) {
  // Inspect validation.payload / validation.raw before granting entitlements.
}