Skip to main content

Overview

The @flagsync/js-sdk integrates into web applications for client-side feature management and event tracking—ideal for single-user contexts like browser environments.

npm

Latest version available on npm

GitHub

Explore the source code on GitHub

Installation

Install the SDK with your preferred package manager:

Quickstart

A basic example of using the SDK in a JavaScript application:

Initialization

Get Your SDK Key

Find your client-side SDK key in your workspace settings—safe for web/mobile apps (keep server-side keys private).

Initialize the SDK

Initialize the SDK with your SDK key and a user context (e.g., user ID):
Ensure the key in FsUserContext is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.

Wait for Readiness

Use events or promises to ensure the SDK is ready before evaluating flags. Always wait for SDK initialization before evaluating any flags. Initialization usually completes within 15–30ms, depending on flag count and ruleset complexity.
1

Promises

2

Events

This event only fires when using the localstorage storage type; the default is memory.

SDK Not Ready

If the SDK isn’t ready, it returns the defaultValue or control:

SDK Ready

Once ready, flag() returns the server-evaluated value:
  • Client-side SDKs can bootstrap via LocalStorage or an initial flag set—values apply until the SDK is ready.
  • See Flag Evaluation: Overview.

Usage

Evaluate Flags

Evaluates a feature flag for the user context. Applies targeting rules, rollouts, and defaults values.

FlagSync CLI

When using the CLI, flag values are automatically inferred from the generated types:
Without generated types, you must manually specify the type:
An Impression is automatically registered when flag() is called.

Track Events

Submit user actions with track()—use eventValue for numeric data or properties for rich context:
There are two types of events:

Numeric Events

Captures measurable data like time or counts.

Property Events

Captures rich context like purchase details.
Create metrics like “Average Purchase Price” from events—see Metrics Overview.

SDK Event Listeners

The SDK emits these events for SDK lifecycle management:
  • SDK_UPDATE: Emitted when flags are updated
  • SDK_READY: Emitted when the SDK is ready
  • SDK_READY_FROM_STORE: Emitted when flags are loaded from storage
  • ERROR: Emitted when an error occurs during initialization
SDK_UPDATE does not fire if syncing is disabled.

Error Handling

All exposed errors are normalized to FsServiceError.

Configuration

Configure the SDK with the FsConfig interface:

Custom Attributes

Define user attributes for targeting in Flags: User Segments.
Ensure the key in FsUserContext is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.

Flag Syncing

Configure flag update strategies with the sync object: ws, sse, poll, or off.
By default, flag updates propagate in milliseconds over a WebSocket connection, ensuring the latest values are used in evaluations.
1

WebSocket (Default)

Stream updates over a WebSocket connection—flag updates are reevaluated on the server and pushed to the client in real time:
2

SSE

Stream updates via server-sent events (SSE):
3

Polling

Poll the server on an interval:
4

Off

Disable syncing:

Bootstrapping

Initialize the SDK with a set of bootstrap flags:
Bootstrapped values apply before the SDK is ready—see Flag Evaluation: Overview.

Storage

Choose between memory and LocalStorage storage types:
1

Memory (Default)

2

LocalStorage

The SDK_READY_FROM_STORE event fires when loading from LocalStorage—no network request needed, but data may be stale.

Best Practices

  • Wait for SDK readiness before evaluating flags.
  • Select a sync strategy (ws/sse/poll/off) based on your application’s needs.
  • Add user attributes for targeted feature rollouts.
  • Consider bootstrapping for faster initial renders.

Environment Variables

Set the following environment variable:
  • FLAGSYNC_SDK_KEY: Your client-side FlagSync SDK key (required)