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
.
Property | Type | Description |
authenticationKey | string | 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. |
syncInterval | number (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). |
cacheProvider | object (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: |
verbose | boolean (optional) | Optional property that enables verbose logging. The default value is |
timeoutSeconds | number (optional) | The |
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:
Property | Type | Description |
featureToggleKey | string | The key of the feature toggle for which you want to get the treatment. |
trafficType | request | session | The traffic type for which you want to get the treatment. The value can be either |
identifier | string (optional) | This property is only required when the traffic type is |
properties | Record[string, string] | The properties object allows you to provide values for targeting rules that you have defined in your feature toggle. They correspong to |
fallback | string (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 |
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.
Property | Type | Description |
featureToggleKeys | string[] | 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:
Property | Type | Description |
eventKey | string | The key of the event that you want to trigger. |
params | Record[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. |
identifier | string | The identifier is a unique identifier for the user that triggered the event. The identifier has to correspond to the identifier used in the |
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.