Using Flagonic from JavaScript
Flagonic has no JavaScript SDK of its own — by design. You use the official OpenFeature SDKs with their official OFREP providers. There are two flavors: the server SDK for Node services and the web SDK for browsers. Nothing Flagonic-specific ever enters your bundle, so swapping flag vendors later is a config change, not a refactor.
Prerequisites
- A running Flagonic server with a flag defined — see the overview & quickstart.
- An SDK key (
fgn_sdk_…) for your project + environment.
Node services (server SDK)
npm install @openfeature/server-sdk @openfeature/ofrep-provider
Initialize once at startup; evaluations are per-request against Flagonic:
import { OpenFeature } from '@openfeature/server-sdk';
import { OFREPProvider } from '@openfeature/ofrep-provider';
await OpenFeature.setProviderAndWait(
new OFREPProvider({
baseUrl: process.env.FLAGONIC_URL, // e.g. https://flags.example.com
headers: [['Authorization', `Bearer ${process.env.FLAGONIC_SDK_KEY}`]],
}),
);
const client = OpenFeature.getClient('checkout-service');
// per request:
const enabled = await client.getBooleanValue('new-checkout', false, {
targetingKey: user.id, // stable per subject — keeps rollouts sticky
plan: user.plan,
});
All four flag types are available: getBooleanValue,
getStringValue, getNumberValue, getObjectValue —
each takes a default returned on any failure, so outages degrade gracefully.
Browsers (web SDK)
npm install @openfeature/web-sdk @openfeature/ofrep-web-provider
The web provider works differently: it fetches all flags for the current user in one OFREP bulk call (cheap re-checks via ETag), then evaluations are synchronous and instant. Set the evaluation context up front and again whenever the user changes:
import { OpenFeature } from '@openfeature/web-sdk';
import { OFREPWebProvider } from '@openfeature/ofrep-web-provider';
await OpenFeature.setContext({ targetingKey: user.id, plan: user.plan });
await OpenFeature.setProviderAndWait(
new OFREPWebProvider({
baseUrl: 'https://flags.example.com',
headers: [['Authorization', `Bearer ${FLAGONIC_SDK_KEY}`]],
pollInterval: 30000, // re-check for flag changes every 30s
}),
);
const client = OpenFeature.getClient();
const enabled = client.getBooleanValue('new-checkout', false); // sync
Inspect why a value was served
const details = await client.getBooleanDetails('new-checkout', false, ctx);
console.log(details.value, details.variant, details.reason);
// true "on" "TARGETING_MATCH" — other reasons: SPLIT, DEFAULT, STATIC, DISABLED, ERROR
Switching vendors
Both providers speak standard OFREP. Point baseUrl at any other
OFREP-compatible backend and you're done — that's the whole Flagonic deal.