SDKs
Client-side
TypeScript

Typescript SDK

The FeaturesFlow SDK is a library designed to interact with the FeaturesFlow service, allowing developers to manage feature toggles and trigger events within their applications. This documentation provides an overview of the SDK's functionality, including initialization, retrieving feature toggles, and triggering events. The SDK is written in TypeScript and can be used in both TypeScript and JavaScript projects.

Compatibility

The FeaturesFlow SDK is compatible with both Node.js and browser environments. Node.js version 14 or higher is recommended, we don't guarantee compatibility with older versions.

Installation

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

npm install @featuresflow/sdk-js

or

yarn add @featuresflow/sdk-js

API

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

client.init()

The behavior of the SDK can be customized using config object passed to init() function as parameter.

PropertyTypeDescription
authenticationKeystring

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

trafficType'session' | 'request'

The trafficType parameter allows you to specify how the SDK should track user sessions. If you set trafficType to 'session', the SDK will track user sessions and make sure that a given user receives the same treatment for a feature toggle during the same session. If you set trafficType to 'request', the SDK won't track user sessions and will return a new treatment value each time you request a feature toggle.

identifierstring (optional)

The indentifier is a unique identifier that allows the SDK to track user sessions and make sure that a given user receives the same treatment for a feature toggle during the same session. It only needs to be provided when trafficType === 'session'.

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.

client.getFeatureToggle()

The getFeatureToggle() method allows you to retrieve a feature toggle treatment value.

ParameterTypeDescription
keystring

The feature toggle key is a unique identifier that allows the SDK to retrieve the treatment value for a given feature toggle.

fallbackstring (optional)

Optional parameter that is returned when the feature toggle is not found or when the SDK is not yet initialized. By default, the fallback value is set to control.


Usage

Initialization

To initialize the FeaturesFlow client, you need to provide a configuration object of type FeaturesFlowConfig. This configuration includes required parameters such as authenticationKey, trafficType and an optional identifier.

import { FeaturesFlowClient } from 'featuresflow-sdk';
 
const config: FeaturesFlowConfig = {
  authenticationKey: 'your_authentication_key',
  trafficType: 'session',
  identifier: 'your_identifier'
};
 
const client = new FeaturesFlowClient();
await client.init(config);

The init method returns a promise that resolves when the client is initialized.

Targeting rules

You can provide properties to config object. These properties should corespond to targeting rules that you have defined in your feature toggle. This is a very powerful feature that allows you to target specific users or groups of users.

const config: FeaturesFlowConfig = {
  authenticationKey: 'your_authentication_key',
  trafficType: 'session',
  identifier: 'your_identifier'
  properties: {
    countryCode: 'US',
  }
};

Retrieving Feature Toggles

To retrieve feature toggles, you can use the getFeatureToggle method. This method takes a feature toggle name as parameter and returns a promise that resolves with a treatment value.

const treatment = client.getFeatureToggle('feature_toggle_name');

Note

This method is synchronous and will return a treatment value immediately.

Each time you retrive a feature toggle, the SDK will also trigger the feature toggle render. This means that the SDK will send an event to FeaturesFlow service, incrementing traffic recieved for your feature toggle as well as treatment traffic.