SDKs
Server-side
Node.js

Node.js SDK

The FeaturesFlow Node.js SDK is a library designed to interact with FeaturesFlow in backend services running on Node or Bun. This documentation will guide you through the process of installing and using the SDK.

Installation

To install the FeaturesFlow SDK, you can use npm or yarn:

npm install @featuresflow/sdk-js-server

API

Under this section you can find the API reference for the Node SDK.

constructor

The behavior of the SDK can be customized using config object passed to the constructor of FeaturesFlowClient.

PropertyTypeDescription
authenticationKeystring

The authentication key is a unique identifier for a FeaturesFlow project that allows the SDK to authenticate with the FeaturesFlow service. You can find out more about SDK keys here.

syncIntervalnumber (optional)

Optional property defining how often the SDK should synchronize feature toggles with the FeaturesFlow service. The value is in milliseconds. The default value is 60000 (60 seconds).

cacheProviderobject (optional)

Optional property that allows you to use your own cache provider for example a Redis instance. The cache provider must implement the following methods:
get: (key: string) => Promise[string | undefined]
set: (key: string, value: string) => Promise[void]
remove: (key: string) => Promise[void]
The default cache provider is an in-memory cache based on node-cache.

verboseboolean (optional)

Optional property that enables verbose logging. The default value is false.

timeoutSecondsnumber (optional)

The timeoutSeconds parameter allows you to specify the maximum time in seconds that the SDK should wait for the initialization to complete. By default, the timeout is set to 5 seconds.


init

After creating the FeaturesFlowClient object, you need to call the init method to start the synchronization process. This method returns a promise that resolves when the SDK is ready to use. You can await this promise before starting your application or serve your users default treatments when SDK is initializing.


getFeatureToggle(): string

The getFeatureToggle method is used to get the treatment for a feature toggle. The method returns a promise that resolves to the treatment for the feature toggle. The method takes an object as an argument with the following properties:

PropertyTypeDescription
featureToggleKeystring

The key of the feature toggle for which you want to get the treatment.

trafficTyperequest | session

The traffic type for which you want to get the treatment. The value can be either request or session. Request type is used for short-lived requests like API calls, while session type is used when you want to maintain the same treatment for a longer period of time for a given user.

identifierstring (optional)

This property is only required when the traffic type is session. It is a unique identifier for the user for which you want to get the treatment. The identifier will be maintained across different treatment evaluations for the same user.

propertiesRecord[string, string]

The properties object allows you to provide values for targeting rules that you have defined in your feature toggle. They correspong to properties that you can create in IF and IF-ELSE blocks in your feature toggle's targeting rules.

fallbackstring (optional)

The fallback is the treatment that will be returned if the feature toggle is not found or if the SDK is not ready. The default value is control.


getFeatureToggles(): Record<string,string>

The getFeatureToggles method is used to get the treatments for multiple feature toggles. The method returns a promise that resolves to an object with feature toggle keys as keys and treatments as values. The method works exactly the same as getFeatureToggle but instead of a single feature toggle key, you provide an array with multiple feature toggle keys.

PropertyTypeDescription
featureToggleKeysstring[]

The keys of the feature toggles for which you want to get the treatment. The method will return an object with the same keys and treatments as values.


triggerEvent(): void

The triggerEvent method is used to trigger an event in FeaturesFlow. The method takes an object as an argument with the following properties:

PropertyTypeDescription
eventKeystring

The key of the event that you want to trigger.

paramsRecord[string, string | number | boolean]

The params object allows you to pass values to the event. They can represent things such as order value or product category.

identifierstring

The identifier is a unique identifier for the user that triggered the event. The identifier has to correspond to the identifier used in the getFeatureToggle method when the traffic type is session.


Usage

Here is an example of how you can use the FeaturesFlow SDK in your Node.js application:

  const ff = new FeaturesFlowClient({
    authenticationKey: 'YOUR_AUTHENTICATION_KEY',
    syncInterval: 120000
  })
 
  await ff.init()
 
  await ff.getFeatureToggle({
    featureToggleKey: 'YOUR_FEATURE_TOGGLE_KEY',
    trafficType: 'session',
    identifier: 'YOUR_USER_IDENTIFIER',
  })

That's it! You are now ready to use the FeaturesFlow SDK in your Node.js application.