JavaScript quickstart
Wire the browser SDK into your app and see your first flag fetch.
Wire the browser SDK into your app in three steps. This assumes you've already
created a flag and copied a development SDK key — if not, start with the
Quickstart.
1. Install
npm install switchbox-js
switchbox-js has zero runtime dependencies — it uses only the browser's
fetch and Web Crypto APIs.
2. Create a client
Use the Switchbox.create() factory — it constructs the client and awaits the
first config fetch, so the client is ready to evaluate as soon as it resolves.
Create it once and reuse it.
import { Switchbox } from 'switchbox-js';const client = await Switchbox.create({sdkKey: 'your-sdk-key-from-dashboard',});
3. Check a flag
Evaluation is async (it uses Web Crypto for rollout hashing), so await it:
if (await client.enabled('new_checkout', { user_id: '42' })) {showNewCheckout();} else {showOldCheckout();}
The user object is optional — pass it when a flag uses targeting rules or a percentage rollout. After the first fetch, evaluation is a local lookup against the cached config; the SDK refreshes it in the background every 30 seconds.
When you're done (e.g. tearing down a session), stop the poller:
client.destroy();
See it connect
Load the page that creates the client. Within a few seconds, the development
environment in the dashboard shows a Connected ✓ badge — your client is
fetching its config from the edge.
Flip the flag in the dashboard and Save; the running page picks up the change on its next poll (within ~30 seconds).
Where to next
- JavaScript SDK reference — methods, options, and the
onEvaluationhook for analytics. - React quickstart — if you're using React, use the hooks instead.
- Targeting rules and rollouts.