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, yarn or CDN bundle:
npm install @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.
Property | Type | Description |
authenticationKey | string | 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 |
identifier | string (optional) | The |
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 |
timeoutSeconds | number (optional) | The |
client.getFeatureToggle()
The getFeatureToggle()
method allows you to retrieve a feature toggle treatment value.
Parameter | Type | Description |
key | string | The feature toggle key is a unique identifier that allows the SDK to retrieve the treatment value for a given feature toggle. |
fallback | string (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 |
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 correspond 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.