Browser SDK
StatsKitClient runs in the browser (and any public/mobile context). The server pre-evaluates your flags and returns a sanitized payload, resolved values only, no targeting rules cross the wire, so the key is safe to ship in your bundle.
Install
Section titled “Install”npm install @statskit/sdkInitialize
Section titled “Initialize”Create the client with your context and await init(). The last payload is cached in localStorage, so a returning visitor evaluates flags on the very first line, before any network request completes.
import { StatsKitClient } from "@statskit/sdk";
export const statskit = await new StatsKitClient({ clientKey: "csk_live_xxx", // public key, safe in the browser context: { key: user.id, attributes: { plan: user.plan, country: user.country } },}).init();Fresh config is fetched in the background and polled every 60s. To update instantly on publish, enable the SSE stream, see Server SDK → updates (the same options apply).
Evaluate
Section titled “Evaluate”Pass the flag key and a default. Evaluation never throws, a missing flag or a network outage returns your default.
if (statskit.boolVariation("new-checkout", false)) { // show the new checkout}const theme = statskit.stringVariation("banner-theme", "default");const limit = statskit.numberVariation("upload-limit", 5);const config = statskit.jsonVariation("home-layout", { hero: "a" });// Generic typed readconst cohort = statskit.variation<string>("cohort", "control");
// Every flag at onceconst all = statskit.allFlags();
// Value + reason + config versionconst { value, reason, version } = statskit.detail("new-checkout", false);Context and attributes
Section titled “Context and attributes”The browser client holds one identity at a time: set it once, and every variation() uses it. There’s no per-call context argument.
The context is { key, attributes }: key is the stable bucketing unit (deterministic assignment), and attributes are what the server’s targeting rules reference.
Re-target after login or when an attribute changes with identify(). This re-fetches flags for the new identity (and re-renders the React hooks):
await statskit.identify({ key: user.id, attributes: { plan: "premium", country: "US" } });
statskit.boolVariation("new-checkout", false); // uses the current contextstatskit.getContext(); // read it backExposures
Section titled “Exposures”Every variation() call records an exposure (which context saw which variation) and batches it to StatsKit for experiment analytics. Only the context key is sent, the server hashes it, and attributes are never included. Disable with:
new StatsKitClient({ clientKey: "csk_live_xxx", sendExposures: false });Full example
Section titled “Full example”import { StatsKitClient } from "@statskit/sdk";
const statskit = await new StatsKitClient({ clientKey: "csk_live_xxx", context: { key: currentUser.id, attributes: { plan: currentUser.plan } }, plugins: [ { name: "logger", onEvaluation: (d) => console.log(d.flagKey, "→", d.value) }, ],}).init();
function renderCheckout() { if (statskit.boolVariation("new-checkout", false)) { return new NewCheckout(); } return new OldCheckout();}
// on loginasync function onLogin(user) { await statskit.identify({ key: user.id, attributes: { plan: user.plan } }); renderCheckout();}Using React? See React binding. For the full options table and error handling, see the SDK overview.