Set up the client

Create the one client object you use for everything in the SDK.

Create the client

Create one client per browser page and import it everywhere. Creating another client can stop the earlier client receiving results. In an SSR app, load the shared module only on the client; do not evaluate it on the server.

// 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();

Configure the client

Every option is optional with a safe default, so most apps pass nothing. Set removeLoading: "manual" to dismiss the native splash screen yourself, and pass onError to centralize error reporting (it receives a BdkError).

// Use this factory call in the shared module instead of its default call.
export const bdk = browser.__bdkClient ??= createBdkNative({
  removeLoading: "automatic",
  pageFit: "normal",
  onError: (error) => console.error(error.code, error.message)
});

Wait for the app to be ready

Wait for device info at startup before using native features. Pass a timeout to wait that many milliseconds; inside the app it resolves as soon as device info arrives, and in a plain browser it resolves to null when the timeout elapses. It never rejects, and it doesn't block rendering — only the code after the await.

const info = await bdk.ready(3000);

if (info) {
  console.log("Running natively on", info.deviceOS, "release", info.bdkRelease);
} else {
  console.log("No native shell — running as a normal web page");
}

Detect native vs. browser

Branch between native-only features and web fallbacks. bdk.isNative() returns true once device info has arrived; bdk.getDeviceInfo() returns the cached BdkDeviceInfo or null. For an instant answer, bdk.environment() reports "native" synchronously on current app builds — treat "unknown" as "wait" and use ready().

isNative() reads false until device info arrives — at page load that's the wrong answer inside the app. Always await bdk.ready(...) before branching.

await bdk.ready(3000);

if (bdk.isNative()) {
  const info = bdk.getDeviceInfo();
  console.log(info?.playerId, info?.pushToken, info?.versionName);
} else {
  // Render a web-only experience
}

Use it in your framework

Vite apps can import the shared browser module at startup. In React/Next, load it in a client component’s effect or event handler, not during server rendering. In Nuxt, import it from a .client.ts plugin without awaiting readiness in the plugin. Run readiness in client startup code and keep rendering independent of it. Public IDs may use the framework’s public environment variables; secrets never may.

import { bdk } from "~/lib/bdk";
export default defineNuxtPlugin(() => ({ provide: { bdk } }));

Clean up in tests

Dispose a client only when that page or test no longer needs it. Do not dispose the shared client when one component unmounts; remove that component’s listeners instead. Hot reload should reuse the shared browser instance.

bdk.dispose();