The Unleash JavaScript SDK is a lightweight client with no external dependencies (beyond browser APIs) that lets you evaluate feature flags in any JavaScript application. It is not tied to any framework, so you can use it with React, Vue, Angular, or plain JavaScript.
The SDK stores flag configuration in localStorage and synchronizes with Unleash or Unleash Edge in the background. Because flags are stored in the browser, the SDK can bootstrap itself the next time the user visits the same page.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
Configure the client according to your needs. The following example provides only the required options. Refer to configuration options for the full list.
Set url to the /api/frontend endpoint of your Unleash instance or your Unleash Edge instance. Set clientKey to a frontend API token.
You should wait for the client’s ready or initialized events before you start working with it. Before it’s ready, the client might not report the correct state for your features.
The difference between the events is described in the section on available events.
Once the client is ready, you can start checking features in your application. Use the isEnabled method to check the state of any feature you want:
You can use the getVariant method to get the variant of an enabled feature that has variants. If the feature is disabled or if it has no variants, then you will get back the disabled variant
The Unleash context holds properties such as user ID, session ID, and custom fields that drive flag evaluation, including strategy targeting, constraints, and stickiness. To update and configure the Unleash context in this SDK, use the updateContext, setContextField, and removeContextField methods.
The context you set in your app will be passed along to the Unleash Edge or the Frontend API as query parameters for feature evaluation.
The updateContext method will replace the entire
(mutable part) of the Unleash context with the data that you pass in.
The setContextField method only acts on the property that you choose. It does not affect any other properties of the Unleash context.
The removeContextField method only acts on the property that you choose. It does not affect any other properties of the Unleash context.
The Unleash SDK takes the following options:
The client is also an event emitter. This means that your code can subscribe to updates from the client. This is a neat way to update a single page app when toggle state updates.
The SDK emits the following events:
unleash.start(). If you register them after you have started the SDK you risk losing important events.Impact metrics are lightweight, application-level time-series metrics stored and visualized directly inside Unleash. Use them to connect application data, such as request counts, error rates, or latency, to your feature flags and release plans.
The SDK automatically attaches appName, and environment labels to all impact metrics.
Initialize the SDK to start publishing impact metrics.
Use counters for cumulative values that only increase, such as the total number of requests or errors.
Impact metrics are batched and sent on the same interval as regular SDK metrics. They are ingested via the regular metrics endpoint.
You may provide a custom session id via the “context”. If you do not provide a sessionId this SDK will create a random session id, which will also be stored in the provided storage (local storage). By always having a consistent sessionId available ensures that even “anonymous” users will get a consistent experience when feature flags are evaluated, in combination with a gradual (percentage based) rollout.
You can stop the Unleash client by calling the stop method. Once the client has been stopped, it will no longer check for updates or send metrics to the server.
A stopped client can be restarted.
This SDK can work with React Native storage @react-native-async-storage/async-storage or react-native-shared-preferences and many more to backup feature flags locally. This is useful for bootstrapping the SDK the next time the user comes back to your application.
You can provide your own storage implementation.
Examples:
This SDK can also be used in node.js applications (from v1.4.0). Please note that you will need to provide a valid “fetch” implementation. Only ECMAScript modules is exported from this package.
index.mjs
The Developer Toolbar wraps your Unleash client to provide runtime flag overrides during development. Install @unleash/toolbar and wrap your client:
The wrapped client has the same API as the original. Refer to the Developer Toolbar documentation for configuration options and framework guides.
You can bootstrap the SDK with your own feature flag configuration to make flags available immediately, without waiting for an API call. This is useful when you need flags in a specific state right after initialization.
Pass a bootstrap array and an optional bootstrapOverride flag when you create the client:
When bootstrapOverride is true (the default), the bootstrap data replaces any locally cached data. When bootstrapOverride is false, the bootstrap data is only used if the local cache is empty.
You can opt out of the Unleash feature flag auto-refresh mechanism and metrics update by settings the refreshInterval and/or metricsInterval options to 0.
In this case, it becomes your responsibility to call updateToggles and/or sendMetrics methods.
This approach is useful in environments that do not support the setInterval API, such as service workers.