A lightweight, feature-rich client-side SDK for FlipFlag (https://flipflag.dev) β a powerful feature flag management platform.
The SDK is designed to be simple, declarative, and safe by default. It supports both browser and Node.js environments with automatic feature flag synchronization and usage tracking.
npm install @flipflag/sdk
# or
yarn add @flipflag/sdk
# or
pnpm add @flipflag/sdkimport { FlipFlag } from "@flipflag/sdk";
const manager = new FlipFlag({
publicKey: "YOUR_PUBLIC_KEY",
privateKey: "YOUR_PRIVATE_KEY", // optional (read-only mode without it)
});
await manager.init();
if (manager.isEnabled("newFeature")) {
console.log("Feature is enabled!");
}import { FlipFlag } from "@flipflag/sdk";
const manager = new FlipFlag(
{
publicKey: "YOUR_PUBLIC_KEY",
privateKey: "YOUR_PRIVATE_KEY",
},
{
// Initial configuration
newFeature: {
contributor: "dev@example.com",
times: [
{
started: "2025-12-01T00:00:00.000Z",
finished: "2025-12-31T23:59:59.000Z",
},
],
},
}
);
await manager.init();- β Dual environment support: Node.js and Browser
- β
Read-only mode: Use only
publicKeyto fetch flags - β
Full management: Use
privateKeyto declare features and track usage - β YAML configuration: Declarative feature definitions (Node.js)
- β Programmatic API: Declare features via code (Browser)
- β Auto-sync: Periodic polling for flags and statistics
- β Usage tracking: Automatic feature usage analytics
- β Time windows: Define feature activation periods
- β TypeScript: Full type safety out of the box
- β Lightweight: Minimal dependencies
interface IManagerOptions {
/**
* FlipFlag API base URL.
* @default "https://api.flipflag.dev"
*/
apiUrl?: string;
/**
* Public key for fetching feature flags (required).
* Get it from your FlipFlag project settings.
*/
publicKey: string;
/**
* Private key for declaring features and syncing usage (optional).
* Without it, SDK works in read-only mode.
*/
privateKey?: string;
/**
* Path to .flipflag.yml config file (Node.js only).
* @default "<process.cwd()>/.flipflag.yml"
*/
configPath?: string;
/**
* If true, missing config file won't throw an error.
* @default true
*/
ignoreMissingConfig?: boolean;
/**
* Polling interval for fetching feature flags (milliseconds).
* @default 30000 (30 seconds)
*/
pollingInterval?: number;
/**
* Sync interval for sending declarations and usage stats (milliseconds).
* @default 90000 (90 seconds)
*/
syncInterval?: number;
}Perfect for client-side applications where you only need to check feature states:
const manager = new FlipFlag({
publicKey: "pub_xxxxxxxxxxxxx",
});
await manager.init();
// Check feature state
if (manager.isEnabled("darkMode")) {
enableDarkMode();
}
// Cleanup when done
manager.destroy();For applications that manage features and track usage:
const manager = new FlipFlag({
publicKey: "pub_xxxxxxxxxxxxx",
privateKey: "priv_xxxxxxxxxxxxx",
});
await manager.init();
// Features will be automatically declared from .flipflag.yml
// and usage will be trackedconst manager = new FlipFlag({
publicKey: "pub_xxxxxxxxxxxxx",
pollingInterval: 10_000, // Check flags every 10 seconds
syncInterval: 60_000, // Sync usage every 60 seconds
});Force immediate synchronization without waiting for intervals:
await manager.init();
// ... some time later
await manager.sync(); // Syncs declarations and usage immediatelyimport { FlipFlag } from "@flipflag/sdk";
const manager = new FlipFlag(
{
publicKey: "pub_xxxxxxxxxxxxx",
privateKey: "priv_xxxxxxxxxxxxx",
},
{
newPaymentFlow: {
description: "New Stripe integration",
contributor: "payments-team@example.com",
type: "feature",
times: [
{
started: "2025-01-15T00:00:00.000Z",
finished: null, // No end date
},
],
},
legacyCheckout: {
contributor: "payments-team@example.com",
times: [
{
started: "2024-01-01T00:00:00.000Z",
finished: "2025-03-01T00:00:00.000Z", // Will be disabled after this date
},
],
},
}
);
await manager.init();const manager = new FlipFlag({
publicKey: "pub_xxxxxxxxxxxxx",
privateKey: "priv_xxxxxxxxxxxxx",
});
await manager.init();
// Add features programmatically
manager.declareFromObject({
experimentalUI: {
contributor: "ui-team@example.com",
times: [
{
started: new Date().toISOString(),
finished: null,
},
],
},
});
// Sync immediately
await manager.sync();The SDK automatically loads .flipflag.yml from your project root during init().
newFeature:
contributor: epolevov@emd.one
times:
- started: "2025-12-01T00:00:00.000Z"
finished: "2025-12-31T23:59:59.000Z"
anotherFeature:
contributor: dev@company.com
times:
- started: "2026-01-01T00:00:00.000Z"
finished: nullseasonalFeature:
description: "Holiday sale banner"
contributor: marketing@example.com
type: "feature"
times:
# Black Friday 2025
- started: "2025-11-24T00:00:00.000Z"
finished: "2025-11-30T23:59:59.000Z"
# Christmas 2025
- started: "2025-12-20T00:00:00.000Z"
finished: "2025-12-26T23:59:59.000Z"
betaFeature:
description: "New analytics dashboard"
contributor: analytics-team@example.com
type: "experiment"
times:
- started: "2025-01-01T00:00:00.000Z"
finished: null # No end date - always activeconst manager = new FlipFlag({
publicKey: "pub_xxxxxxxxxxxxx",
configPath: "./config/features.yml",
});// Node.js
new FlipFlag(options: IManagerOptions)
// Browser
new FlipFlag(options: IManagerOptions, initialConfig?: FlipFlagYaml)Initializes the SDK:
- Loads YAML config (Node.js only)
- Fetches current feature flags from server
- Syncs declared features
- Starts automatic polling and sync intervals
await manager.init();Checks if a feature is enabled. Also:
- Tracks usage automatically
- Creates feature on server if it doesn't exist (requires
privateKey)
if (manager.isEnabled("darkMode")) {
// Feature is enabled
}Programmatically declares features without YAML file.
manager.declareFromObject({
myFeature: {
contributor: "dev@example.com",
times: [{ started: "2025-01-01T00:00:00.000Z", finished: null }],
},
});Manually triggers synchronization:
- Syncs feature declarations
- Sends usage statistics
await manager.sync();Stops all timers and clears local state:
- Stops polling interval
- Stops sync interval
- Clears cached flags and usage data
manager.destroy();type FlipFlagYaml = Record<string, YamlFeature>;
interface YamlFeature {
description?: string;
contributor?: string;
type?: string;
times?: YamlTime[];
}
interface YamlTime {
started: string; // ISO 8601 date string
finished: string | null; // ISO 8601 date string or null for no end
}interface IDeclareFeatureTime {
email: string;
start: string; // ISO 8601 date string
end?: string; // ISO 8601 date string
}interface IFeatureFlag {
enabled: boolean;
}const manager = new FlipFlag({
publicKey: process.env.FLIPFLAG_PUBLIC_KEY!,
privateKey: process.env.FLIPFLAG_PRIVATE_KEY,
});// app.ts
const manager = new FlipFlag({ /* ... */ });
await manager.init();
// Now use throughout your app
export { manager };process.on("SIGTERM", () => {
manager.destroy();
process.exit(0);
});For security, only use publicKey in client-side production code:
// β
Good - read-only
const manager = new FlipFlag({
publicKey: import.meta.env.VITE_FLIPFLAG_PUBLIC_KEY,
});
// β Bad - exposes private key
const manager = new FlipFlag({
publicKey: import.meta.env.VITE_FLIPFLAG_PUBLIC_KEY,
privateKey: import.meta.env.VITE_FLIPFLAG_PRIVATE_KEY, // Don't do this!
});try {
await manager.init();
} catch (error) {
console.error("Failed to initialize FlipFlag:", error);
// Fallback to default behavior
}// Default to false if something goes wrong
const isEnabled = manager?.isEnabled("newFeature") ?? false;Error: FlipFlag: cannot read config at /path/to/.flipflag.yml: ENOENT
Solution: Set ignoreMissingConfig: true or create the config file:
const manager = new FlipFlag({
publicKey: "pub_xxxxxxxxxxxxx",
ignoreMissingConfig: true,
});Error: FlipFlag: invalid "started" date in myFeature: not-a-date
Solution: Use ISO 8601 format for dates:
myFeature:
times:
- started: "2025-01-01T00:00:00.000Z" # β
Correct
# - started: "2025-01-01" # β WrongError: Public key is missing. Please provide a valid publicKey in the SDK configuration.
Solution: Always provide a publicKey:
const manager = new FlipFlag({
publicKey: "pub_xxxxxxxxxxxxx", // Required!
});If features declared in YAML aren't appearing on the server:
- Check that you provided
privateKey(read-only mode can't create features) - Check network connectivity to FlipFlag API
- Verify your API keys are correct
- Check browser/server console for errors
- Node.js: v16+ (ESM and CommonJS)
- Browsers: Modern browsers with
fetchAPI support - TypeScript: 4.5+
The package provides optimized builds for different environments:
{
"exports": {
".": {
"types": "./dist/browser.d.ts",
"browser": "./dist/browser.js",
"node": "./dist/node.js",
"default": "./dist/browser.js"
}
}
}Node.js automatically uses the Node-specific build with filesystem support, while browsers get the lightweight browser build.
- Website: https://flipflag.dev
- Documentation: https://docs.flipflag.dev
- GitHub: https://github.com/flipflag-dev/sdk
- NPM: https://www.npmjs.com/package/@flipflag/sdk
- Issues: https://github.com/flipflag-dev/sdk/issues
MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Made with β€οΈ by the FlipFlag team