Sell products & subscriptions

List store products, start a purchase, restore entitlements, and consume consumables from the App Store and Google Play.

Load your product catalog

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.

Before using these helpers, create and configure the products in App Store Connect and Play Console. BDK does not create or price them. BDK Preview uses its own test products; test your product IDs in your own build. The browser reports a purchase outcome, not server authority to unlock content.

In these examples, verifyPurchaseOnServer is your application’s authenticated backend verification flow. It retrieves and verifies store proof before enabling access; a matching product id alone must never grant access.

Fetch localized products for a paywall. Unknown store ids come back in invalidIds. Each product is a BdkIapProduct — kind is consumable, nonConsumable, subscription, or nonRenewingSubscription.

In a plain browser the command doesn't run; the returned object has no ok field and triggered: false.

This feature can be switched off in a given app build. Check await bdk.capabilities.has("iap") before showing the paywall. When it is off, the call resolves ok: false with code: "common/feature_disabled". See Detect features.

The previous-generation purchase flow (purchaseIos / purchaseAndroid) is documented at In-app purchases and remains supported.

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

async function runAction1() {
  if (!(await bdk.capabilities.has("iap"))) {
    // Hide the paywall — this build does not include the store.
    return;
  }

  const result = await bdk.iap.products(["coins_100", "pro_monthly"]);

  if (!("ok" in result) || !result.ok) {
    // Disabled, or not running in the app (no `ok`, `triggered: false`).
    return;
  }

  for (const product of result.products) {
    console.log(product.id, product.kind, product.price.formatted);
  }

  console.log("unknown ids", result.invalidIds);
}
// Call runAction1() from this UI’s user action.

Start a purchase

Open the store sheet. The call resolves { state: "launched" } — that is not a sale. The outcome arrives on iap.purchaseCompleted, so subscribe before you call.

Pass planId and offerId from the product's subscription plans when you buy a subscription.

PropertyTypeDescription
idstring · requiredThe App Store / Play product identifier.
planIdstringSubscription plan to buy.
offerIdstringOffer on that plan.
replacesstringAndroid product id of the active subscription this purchase replaces.
requestIdstringOptional correlation id, at most 64 characters.

Subscribe to iap.purchaseCompleted before you call. A deferred grant re-emits later — keep a persistent listener, not a one-shot.


// Register once when this UI mounts.
const offPurchase = bdk.on("iap.purchaseCompleted", (event) => {
  if (event.state === "purchased") verifyPurchaseOnServer();
  if (event.state === "pending") showWaitingForApproval();
});

function cleanupPurchaseUI() {
  offPurchase();
}
// Call cleanupPurchaseUI() when this UI unmounts.

async function runAction2() {
  const result = await bdk.iap.purchase({
    id: "pro_monthly",
    planId: "monthly",
    offerId: "trial"
  });

  if (!("ok" in result) || !result.ok) return;
  // result.state === "launched" — the store sheet opened. Wait for the event.
}
// Call runAction2() from this UI’s user action.

Purchase and wait

Wait for the first outcome (purchased, pending, cancelled, or failed). pending is deferred approval — the grant arrives later on iap.purchaseCompleted, so keep a persistent listener.

purchaseAndWait takes the same options as purchase, plus timeoutMs to bound the wait for that first outcome (the store sheet, not a later deferred grant).

A pending result is not a grant. Do not unlock. The persistent listener receives the later purchased event.


// Alternative setup: register once when this UI mounts.
const offPurchase = bdk.on("iap.purchaseCompleted", (event) => {
  // All grants, including deferred approvals, use backend verification.
  if (event.state === "purchased") verifyPurchaseOnServer();
});

function cleanupPurchaseUI() {
  offPurchase();
}
// Call cleanupPurchaseUI() when this UI unmounts.

async function runAction3() {
  const result = await bdk.iap.purchaseAndWait({ id: "pro_monthly" });

  if (!("state" in result)) {
    // Disabled, timed out, or not running in the app.
    return;
  }

  // The persistent listener handles purchased outcomes.
  if (result.state === "pending") showWaitingForApproval();
  if (result.state === "cancelled") return;
  if (result.state === "failed") console.warn(result.code);
}
// Call runAction3() from this UI’s user action.

Check entitlements

Read what the store currently says the user owns. Each item is a BdkIapEntitlement — state is active, gracePeriod, billingRetry, revoked, or expired.


async function runAction4() {
  const result = await bdk.iap.entitlements();

  if (!("ok" in result) || !result.ok) return;

  for (const entitlement of result.entitlements) {
    console.log(entitlement.productId, entitlement.kind, entitlement.state);
  }
}
// Call runAction4() from this UI’s user action.

Restore purchases

Ask the store to restore previous purchases and subscriptions. Use it on a restore button. The call returns the restored entitlements.


async function runAction5() {
  const result = await bdk.iap.restore();

  if (!("ok" in result) || !result.ok) return;

  for (const entitlement of result.restored) {
    console.log(entitlement.productId, entitlement.state);
  }
}
// Call runAction5() from this UI’s user action.

Get the raw receipt

Read the material your server needs to verify a purchase. On iOS you get receipt plus jws; on Android you get purchases (productId, purchaseToken, packageName, productType).

The SDK’s receipt verification helper does not verify a StoreKit 2 signed transaction. Do not pass jws to a receipt verifier or trust it on the client. Have your backend verify the store proof and bind the verified app, product and transaction to the authenticated user before granting access.

Bind every verified purchase to the authenticated account. Check app and product identity, revocation and replay before granting access. Track subscription expiry separately from one-time entitlements. Full StoreKit 2 and Google Play verification recipes are coming.

Treat a client purchased event as the store reporting a transaction. Verify on the server before granting access — see Verify purchases.


async function runAction6() {
  const result = await bdk.iap.receipt();

  if (!("ok" in result) || !result.ok) return;

  if (result.platform === "ios") {
    await sendToServer({ receipt: result.receipt, jws: result.jws });
  } else {
    await sendToServer({ purchases: result.purchases });
  }
}
// Call runAction6() from this UI’s user action.

Consume a consumable

Mark a consumable as used so the user can buy it again. Pass the product id. On iOS, remaining is the locally tracked count after the consume.


async function runAction7() {
  const result = await bdk.iap.consume("coins_100");

  if (!("ok" in result) || !result.ok) return;

  console.log(result.consumed, result.remaining);
}
// Call runAction7() from this UI’s user action.

React to renewals and revocations

Listen for subscription renewals, revocations, and transactions that started outside your UI. Subscribe once at startup, then refresh entitlements.

bdk.on("iap.transactionUpdated", (event) => {
  // event.reason is "renewal", "revoked", or "external"
  void refreshEntitlements();
});

See every native feature with its demo on thebdk.com.