Get started

Drive native iOS and Android features from your web app with @thebdk/native.

Create the client

Create the client once with createBdkNative() and reuse it everywhere.

Always create the client with createBdkNative(), never new BdkNativeClient(). It's ready the moment it returns.

// lib/bdk.ts — import only from browser-side code.
import { createBdkNative } from "@thebdk/native/browser";

type Client = ReturnType<typeof createBdkNative>;
const browser = window as Window & { __bdkClient?: Client };
export const bdk = browser.__bdkClient ??= createBdkNative();

Start readiness from your browser startup code rather than awaiting it in the shared module. Reuse the exported client throughout the page.

Detect the native app

Use bdk.ready(timeoutMs) to wait for device info and bdk.isNative() to branch your code. In a plain browser there's no app, so render a web-only fallback.

  • bdk.ready(0) (the default) resolves immediately — cached BdkDeviceInfo if it arrived, else null.
  • bdk.ready(3000) waits up to the timeout (ms) for the deviceInfo event.

Browser method options are typed — pass the option names each method documents, and your editor will catch mismatches.

const info = await bdk.ready(3000);

if (bdk.isNative()) {
  // Cached device info: playerId, pushToken, deviceOS, bdkRelease, ...
  console.log(bdk.getDeviceInfo()?.deviceOS);
} else {
  // No native shell — render web fallbacks.
}

Get a command's result

Legacy commands return NativeCommandResult; use !result.triggered and subscribe before calls whose data arrives on events. A browser tab returns pending, while a runtime without window returns skipped. Envelope helpers return { ok: true, ... } or { ok: false, code, message }; use isBdkError(result) or !result.ok, not triggered. New iap.* and health.* helpers can also return a bare command result: check "ok" in result first. Read each helper’s result type; do not apply one check to all methods.

Match the event to the command: bdk.media.capturePhoto() emits photoCaptured, bdk.media.pickPhoto() emits photoSelected. Both deliver a MediaResult ({ fileUrl, dataUri, data, contentType }).

// 2) Dispatch a command, then receive its result via an event.
const off = bdk.on("photoCaptured", (result) => {
  console.log("Captured:", result.fileUrl, result.contentType);
  off(); // unsubscribe when you are done
});

const dispatch = await bdk.media.capturePhoto();
console.log(dispatch.triggered, dispatch.queued); // dispatch status, not the photo

Browser and server SDKs

Use @thebdk/native/browser (or the package root; CDN at dist/cdn/bdk-native.global.js) in your web app. Use @thebdk/native/server or a focused /server/* entry on your backend — await returns the typed result.

Never import @thebdk/native/server/* into a browser bundle — those modules are server-only and may carry secrets. Only the root and /browser entry points are browser-safe.

// Server-only. Awaiting resolves to the real, typed outcome.
import { sendPushNotification } from "@thebdk/native/server/onesignal";

const result = await sendPushNotification({
  title: "New message",
  message: "You have a new reply",
  playerIds: ["a-onesignal-player-id"]
});

console.log(result.sentSuccessfully, result.numberOfRecipients);

Start an in-app purchase

Subscribe once when the UI mounts, then start a purchase from a user action. Keep the listener for deferred approvals and unsubscribe when the UI unmounts. verifyAndUnlock is your application’s authenticated backend flow: verify the store proof before granting access.

Create the store products first and test your product IDs in your own build. See Purchases and Verify purchases.

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

// Register once when this UI mounts, before any purchase.
const offPurchase = bdk.on("iap.purchaseCompleted", async (event) => {
  if (event.state !== "purchased") return;
  await verifyAndUnlock(event.transaction);
});

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

async function buyProduct(id: string) {
  try {
    const result = await bdk.iap.purchase({ id });
    if (!("ok" in result) || !result.ok) return;
    // The store sheet opened. The listener handles purchased outcomes,
    // including approvals that arrive after a pending outcome.
  } catch {
    showPurchaseUnavailable();
  }
}