For the complete documentation index, see llms.txt. This page is also available as Markdown.

Node.js SDK

Use the Harness FME Node.js SDK for secure, server-side feature management and controlled rollouts in JavaScript backends.

This guide provides detailed information about our Node.js SDK. All of our SDKs are open source. Go to our Node.js SDK GitHub repository to learn more.

Before you begin

The JavaScript SDK supports Node.js version 14.x or later.

RULE-BASED SEGMENTS SUPPORT

Rule-based segments are supported in SDK versions 11.4.0 and above. No changes are required to your SDK implementation, but updating to a supported version is required to ensure compatibility.

Older SDK versions will return the control treatment for flags using rule-based segments and log an impression with a special label for unsupported targeting rules.

Initialization

Set up FME in your code base with two simple steps.

1. Import the SDK into your project

The SDK is published using npm, so it's fully integrated with your workflow.

npm install --save @splitsoftware/splitio

2. Instantiate the SDK and create a new SDK factory client

var SplitFactory = require('@splitsoftware/splitio').SplitFactory;

var factory = SplitFactory({
  core: {
    authorizationKey: 'YOUR_SDK_KEY'
  }
});

var client = factory.client();
import { SplitFactory } from '@splitsoftware/splitio';

const factory: SplitIO.ISDK = SplitFactory({
  core: {
    authorizationKey: 'YOUR_SDK_KEY'
  }
});

const client: SplitIO.IClient = factory.client();

NOTICE FOR TYPESCRIPT

With the SDK package on NPM, you get the SplitIO namespace, which contains useful types and interfaces for you to use.

Feel free to dive in to the declaration files if IntelliSense is not enough!

We recommend instantiating the SDK factory once as a singleton and reusing it throughout your application.

Configure the SDK with the SDK key for the FME environment that you would like to access. In legacy Split (app.split.io) the SDK key is found on your Admin settings page, in the API keys section. Select a server-side SDK API key. See API keys to learn more.

Use the SDK factory client to evaluate treatments.

Use the SDK

Basic use

When the SDK is instantiated, it kicks off background jobs to update an in-memory cache with small amounts of data fetched from Harness servers. This process can take up to a few hundred milliseconds. While the SDK is in this intermediate state, if it is asked to evaluate which treatment to show to the logged in customer for a specific feature flag, it may not have data necessary to run the evaluation. In this case, the SDK does not fail, rather, it returns the control treatment.

To make sure the SDK is properly loaded before asking it for a treatment, block until the SDK is ready (as shown below). We set the client to listen for the SDK_READY event triggered by the SDK before asking for an evaluation.

When the SDK_READY event fires, you can use the getTreatment method to return the proper treatment based on the key and FEATURE_FLAG_NAME attributes you provided.

Then use an if-else-if block as shown below and insert the code for the different treatments that you defined in Harness FME. Remember the final else branch in your code to handle the client returning the control treatment.

Attribute syntax

To target based on custom attributes, the SDK's getTreatment method needs to be passed an attribute map at runtime.

In the example below, we are rolling out a feature flag to users. The provided attributes plan_type, registered_date, permissions, paying_customer, and deal_size are passed to the getTreatment call. These attributes are compared and evaluated against the attributes used in the rollout plan as defined in Harness FME to decide whether to show the on or off treatment to this account.

The getTreatment method has a number of variations that are described below. Each of these additionally has a variation that takes an attributes argument, which can defines attributes of the following types: strings, numbers, dates, booleans, and sets. The proper data type and syntax for each are:

  • Strings: Use type String.

  • Numbers: Use type Number.

  • **Dates: ** Express the value for these attributes in milliseconds since epoch and as objects of class DateTime.

  • Booleans: Use type Boolean.

  • Sets: Use type Array.

You can pass your attributes in the same way to the client.getTreatments method.

Multiple evaluations at once

In some instances, you may want to evaluate treatments for multiple feature flags at once. Use the different variations of getTreatments from the SDK factory client to do this.

  • getTreatments: Pass a list of the feature flag names you want treatments for.

  • getTreatmentsByFlagSet: Evaluate all flags that are part of the provided set name and are cached on the SDK instance.

  • getTreatmentsByFlagSets: Evaluate all flags that are part of the provided set names and are cached on the SDK instance.

You can also use the Split Manager to get all of your treatments at once.

Get treatments with configurations

To leverage dynamic configurations with your treatments, you should use the getTreatmentWithConfig method.

This method will return an object containing the treatment and associated configuration.

The config element will be a stringified version of the configuration JSON defined in Harness FME. If there is no configuration defined for a treatment, the SDK will return null for the config parameter.

This method takes the exact same set of arguments as the standard getTreatment method. See below for examples on proper usage:

If you need to get multiple evaluations at once, you can also use the getTreatmentsWithConfig methods. These methods take the exact same arguments as the getTreatments methods but return a mapping of feature flag names to TreatmentResults instead of strings. Example usage below:

If a flag cannot be evaluated, the SDK returns the fallback treatment value (default "control" unless overridden globally or per flag). For more information, see Fallback treatments.

Append properties to impressions

Impressions are generated by the SDK each time a getTreatment method is called. These impressions are periodically sent back to Harness servers for feature monitoring and experimentation.

You can append properties to an impression by passing an object of key-value pairs to the getTreatment method. These properties are then included in the impression sent by the SDK and can provide useful context to the impression data.

Three types of properties are supported: strings, numbers, and booleans.

Shutdown

Call the client.destroy() method before letting a process using the SDK exit, as this method gracefully shuts down the SDK by stopping all background threads, clearing caches, closing connections, and flushing the remaining unpublished impressions.

After destroy() is called and finishes, any subsequent invocations to getTreatment/getTreatments or manager methods result in control or empty list, respectively.

Track

Use the track method to record any actions your customers perform. Each action is known as an event and corresponds to an event type. Calling track through one of our SDKs or via the API is the first step to and allows you to measure the impact of your feature flags on your users’ actions and metrics.

Learn more about using track events in features.

In the examples below, you can see that the .track() method can take up to five arguments. The proper data type and syntax for each are:

  • key: The key variable used in the getTreatment call and firing this track event. The expected data type is String.

  • TRAFFIC_TYPE: The traffic type of the key in the track call. The expected data type is String. You can only pass values that match the names of traffic types that you have defined in your instance of feature flag.

  • EVENT_TYPE: The event type that this event should correspond to. The expected data type is String. Full requirements on this argument are:

    • Contains 63 characters or fewer.

    • Starts with a letter or number.

    • Contains only letters, numbers, hyphen, underscore, or period.

    • This is the regular expression we use to validate the value: [a-zA-Z0-9][-_\.a-zA-Z0-9]{0,62}

  • VALUE: (Optional) The value to be used in creating the metric. This field can be sent in as null or 0 if you intend to purely use the count function when creating a metric. The expected data type is Integer or Float.

  • PROPERTIES: (Optional) An object of key value pairs that can be used to filter your metrics. Learn more about event property capture in the Events guide. FME currently supports three types of properties: strings, numbers, and booleans.

The track method returns a boolean value of true or false to indicate whether or not the SDK was able to successfully queue the event to be sent back to Harness servers on the next event post. The SDK will return false if the current queue size is equal to the config set by eventsQueueSize or if an incorrect input to the track method has been provided.

In the case that a bad input has been provided, you can read more about our SDK's expected behavior here

Configuration

The SDK has a number of knobs for configuring performance. Each knob is tuned to a reasonable default. However, you can override the value while instantiating the SDK. The parameters available for configuration are shown below.

Configuration

Description

Default value

core.labelsEnabled

Disable labels from being sent to the Harness servers. Labels may contain sensitive information.

true

core.IPAddressesEnabled

Disable machine IP and Hostname from being sent to Harness servers. IP and Hostname may contain sensitive information.

true

startup.readyTimeout

Maximum amount of time in seconds to wait before notifying a timeout. Zero means no timeout, so no SDK_READY_TIMED_OUT event is fired.

15

startup.requestTimeoutBeforeReady

Time to wait for a request before the SDK is ready. If this time expires, Node.js SDK tries again retriesOnFailureBeforeReady times before notifying its failure to be ready. Zero means no timeout.

15

startup.retriesOnFailureBeforeReady

Number of quick retries we do while starting up the SDK.

1

scheduler.featuresRefreshRate

The SDK polls Harness servers for changes to feature rollout plans. This parameter controls this polling period in seconds.

60

scheduler.segmentsRefreshRate

The SDK polls Harness servers for changes to segment definitions. This parameter controls this polling period in seconds.

60

scheduler.impressionsRefreshRate

The SDK sends information on who got what treatment at what time back to Harness servers to power analytics. This parameter controls how often this data is sent to Harness servers. The parameter should be in seconds.

300

scheduler.impressionsQueueSize

The max amount of impressions we queue. If the queue is full, the SDK flushes the impressions and resets the timer.

30000

scheduler.eventsPushRate

The SDK sends tracked events to Harness servers. This setting controls that flushing rate in seconds.

60

scheduler.eventsQueueSize

The max amount of events we queue. If the queue is full, the SDK flushes the events and resets the timer.

500

scheduler.telemetryRefreshRate

The SDK caches diagnostic data that it periodically sends to Harness servers. This configuration controls how frequently this data is sent back to Harness servers (in seconds).

3600 seconds (1 hour)

sync.splitFilters

Filter specific feature flags to be synced and evaluated by the SDK. This is formed by a type string property and a list of string values for the given criteria. Using the types 'bySet' (recommended, flag sets are available in all tiers) or 'byName', pass an array of strings defining the query. If empty or unset, all feature flags are downloaded by the SDK.

[]

sync.impressionsMode

This configuration defines how impressions (decisioning events) are queued on the SDK. Supported modes are OPTIMIZED, NONE, and DEBUG. In OPTIMIZED mode, only unique impressions are queued and posted to Harness; this is the recommended mode for experimentation use cases. In NONE mode, no impression is tracked in Harness FME and only minimum viable data to support usage stats is tracked, so never use this mode if you are experimenting with instance impressions. Use NONE when you want to optimize for feature flagging only use cases and reduce impressions' network and storage load. In DEBUG mode, ALL impressions are queued and sent to Harness; this is useful for validations. This mode doesn't impact the impression listener which receives all generated impressions locally. Keep in mind that both the OPTIMIZED and DEBUG modes utilize an internal cache which uses heap memory incrementally up to a maximum limit without a memory leak.

OPTIMIZED

sync.enabled

Controls the SDK continuous synchronization flags. When true, a running SDK processes rollout plan updates performed in Harness FME (default). When false, it fetches all data upon init, which ensures a consistent experience during a user session and optimizes resources when these updates are not consumed by the app.

true

sync.requestOptions.agent

A custom Node.js HTTP(S) Agent used to perform the requests to the Harness servers. See Proxy for details.

undefined

sync.requestOptions.getHeaderOverrides

A callback function that can be used to override the Authentication header or append new headers to the SDK's HTTP(S) requests.

undefined

storage.type

Storage type to be used by the SDK. Possible values are MEMORY, and REDIS.

MEMORY

storage.options

Options to be passed to the storage instance. Only usable with REDIS type storage for now. See Redis configuration for details.

{} No default options

storage.prefix

An optional prefix for your data, to avoid collisions.

SPLITIO

mode

The SDK mode. Possible values are standalone and consumer.

standalone

debug

Boolean flag or log level string ('ERROR', 'WARN', 'INFO', or 'DEBUG') for activating SDK logs.

false

streamingEnabled

Boolean flag to enable the streaming service as default synchronization mechanism. In the event of an issue with streaming, the SDK will fallback to the polling mechanism. If false, the SDK will poll for changes as usual without attempting to use streaming.

true

fallbackTreatments

Configure fallback treatments for the SDK.

undefined

To set each of the parameters defined above, use the following syntax.

State sharing with Redis

Configuring this Redis integration section is optional for most setups. Read below to determine if it might be useful for your project.

By default, the SDK factory client stores the state it needs to compute treatments (rollout plans, segments, and so on) in memory. As a result, it is easy to get set up with FME: simply instantiate a client and start using it.

This simplicity hides one important detail that is worth exploring. Because each SDK factory client downloads and stores state separately, a change in a feature flag is picked up by every client on its own schedule. Thus, if a customer issues back-to-back requests that are served by two different machines behind a load balancer, the customer can see different treatments for the same feature flag because one SDK factory client may not have picked up the latest change. This drift in clients is natural and usually ignorable as long as each client sets an aggressive value for FeaturesRefreshRate and SegmentsRefreshRate. You can learn more about setting these rates in the Configuration section below.

However, if your application requires a total guarantee that SDK clients across your entire infrastructure pick up a change in a feature flag at the exact same time or you need an async data store, then the only way to ensure that is to externalize the state of the SDK factory client in a data store hosted on your infrastructure.

We currently support Redis for this external data store.

To use the Node.js SDK with Redis, set up the Split Synchronizer and instantiate the SDK in consumer mode.

Split Synchronizer

Follow the steps in our Split Synchronizer documents to get everything set to sync data to your Redis cache. After you do that, come back to set up the SDK in consumer mode!

Consumer mode

In consumer mode, a client can be embedded in your application code and respond to calls to getTreatment by retrieving state from the data store (Redis in this case).

Here is how to configure and get treatments for a SDK factory client in consumer mode.

Redis configuration

The SDK in consumer mode connects to Redis to function, using URL redis://localhost:6379/0 by default. You can override this URL and other Redis connection parameters with the SDK storage.options configuration object. The available parameters are shown below.

TROUBLESHOOTING REDIS CONNECTIVITY

The SDK's debug option does not include logs from the underlying ioredis client. To troubleshoot Redis connection issues, enable ioredis debug logging by setting the DEBUG environment variable:

Configuration

Description

Default value

host

Hostname where the Redis instance is.

localhost

port

HTTP port to be used in the connection.

6379

db

Numeric database to be used.

0

pass

Redis DB password. Don't define if no password is used.

undefined

url

Redis URL. If set, host, port, db and pass params will be ignored. Example: redis://:authpassword@127.0.0.1:6379/0

undefined

tls

TLS configuration object. See ioredis TLS Options for details.

undefined

connectionTimeout

The milliseconds before a timeout occurs during the initial connection to the Redis server.

10000

operationTimeout

The milliseconds before Redis commands are timeout by the SDK. Method calls that involve Redis commands, like client.getTreatment or client.track calls, are resolved when the commands success or timeout.

5000

Localhost mode

For testing, a developer can put code behind feature flags on their development machine without the SDK requiring network connectivity. To achieve this, the SDK can be started in localhost mode (aka off-the-grid mode). In this mode, the SDK neither polls nor updates Harness servers. Instead, it uses an in-memory data structure to determine what treatments to show to the logged in customer for each of the feature flags.

To use the SDK in localhost mode, set the authorizationKey config property to "localhost", as shown in the example below:

In this mode, the SDK loads a mapping of feature flag name to treatment from a file at $HOME/.split. For a given feature flag, the treatment specified in the file is returned for every customer. Should you want to use another file, you just need to set the features key in the configuration object passed at instantiation time, to the full path of the desired file.

getTreatment calls for a feature flag only return the one treatment that you defined in the file. You can then change the treatment as necessary for your testing in the file. Any feature flag that is not provided in the features map returns the control treatment if the SDK is asked to evaluate them.

Here is a sample .split file. The format of this file is two columns separated by a whitespace. The left column is the feature flag name, and the right column is the treatment name.

Since version 10.7.0, our SDK supports a new type of localhost feature flag definition file, using the YAML format. This new format allows the user to map different keys to different treatments within a single feature flag, and also add configurations to them for a given treatment. The new format is a list of single-key maps (one per mapping split-keys-config), defined as follows:

In the example above, we have 3 entries:

  • The first one defines that for feature flag my_feature, the key mock_user_id will return the treatment on and the on treatment will be tied to the configuration {"desc" : "this applies only to ON treatment"}.

  • The second entry defines that the feature flag some_other_feature will always return the off treatment and no configuration.

  • The third entry defines that my_feature will always return off for all keys that don't match another entry (in this case, any key other than mock_user_id).

In addition, there are some extra configuration parameters that can be used when instantiating the SDK in localhost mode.

Configuration

Description

Default value

scheduler.offlineRefreshRate

The refresh interval for the mocked feature flags treatments.

15

features

The path to the file with the mocked feature flag data.

$HOME/.split

Manager

Use the Split Manager to get a list of feature flags available to the SDK factory client.

To instantiate a Manager in your code base, use the same factory that you used for your client.

The Manager instance has the following methods available.

The SplitView object referenced above has the following structure:

Listener

FME SDKs send impression data back to Harness servers periodically when evaluating feature flags. To send this information to a location of your choice, define and attach an impression listener. Use the SDK's impressionListener parameter, where you can add an implementation of ImpressionListener. This implementation must define the logImpression method. It receives data in the following schema.

Name

Type

Description

impression

Object / SplitIO.Impression

Impression object that has the feature flag, key, treatment, label, etc.

attributes

Object / SplitIO.Attributes

A map of attributes passed to getTreatment/getTreatments (if any).

ip

String

The IP address of the machine where the SDK is running.

hostname

String

The hostname of the OS where the SDK is running.

sdkLanguageVersion

String

The version of the SDK. In this case the language is nodejs plus the version currently running.

Implement a custom impression listener

Here is an example of how to implement a custom impression listener.

An impression listener is called asynchronously from the corresponding evaluation, but is almost immediate.

The SDK does not fail if there is an exception in the listener, but be careful to avoid blocking the call stack.

Logging

To enable SDK logging in your Node.js app, set the SPLITIO_DEBUG environment variable as follows.

Since v9.2.0 of the SDK, you can enable logging via SDK settings and programmatically by calling the Logger API.

By default, the SDK uses the console.log method to output log messages for all log levels.

Since v11.7.0 of the SDK, you can provide a custom logger to handle SDK log messages by setting the logger configuration option or using the Logger API.

The logger object must implement the SplitIO.Logger interface, which is compatible with the console object and logging libraries such as winston, pino, and log4js. The interface is defined as follows:

The following example creates an instance of the winston logger, passes it to the SDK, and then switches to the console object as a logger.

Configure fallback treatments

Fallback treatments let you define a treatment value (and optional configuration) to be returned when a flag cannot be evaluated. By default, the SDK returns control, but you can override this globally or for individual flags at the SDK level.

This is useful when you want to:

  • Maintain a predictable user experience during outages or evaluation failures (avoid unexpected control in production)

  • Protect critical user flows by returning a safe, stable treatment (for example, forcing off during an incident)

  • Customize behavior per flag so each evaluation inherits appropriate safe defaults if something goes wrong

Global fallback treatment

Set a global fallback treatment when initializing the SDK factory. This value is returned whenever any flag cannot be evaluated.

Flag-level fallback treatment

You can also set a fallback treatment per flag when calling getTreatment or getTreatmentWithConfig. This flag-level fallback always takes precedence over the global fallback treatment, so if both are defined, the SDK will return the flag-level value when that flag cannot be evaluated.

For more information, see Fallback treatments.

Proxy

If your environment requires routing traffic through a proxy, you can configure the Node.js SDK to use one.

If you need to use a standard network proxy, provide a custom Node.js HTTPS Agent by setting the sync.requestOptions.agent configuration variable. The SDK uses this agent to perform requests through the proxy.

For example:

Integrate with the Harness Proxy

The Harness Proxy allows SDK traffic to securely route through a centralized, authenticated point before reaching the Harness SaaS backend. This provides full visibility and control over network traffic while keeping API keys secure and isolated.

To use the proxy, configure the SDK to point to the proxy host and port during initialization. All SDK requests are then routed through the proxy.

Configure the proxy

You can configure the Node.js SDK to route traffic through a forward proxy by setting a custom HTTP agent. This overrides the default SDK agent and allows you to define proxy URLs, authentication headers, and mTLS certificates.

In Node.js, network proxies are configured via the agent option in the SDK’s requestOptions. Harness recommends using the https-proxy-agent library to simplify proxy setup and handle secure connections.

The following proxy configuration parameters are available:

Configuration
Description
Required

agent

Custom HTTP agent that defines proxy behavior.

Yes

ca

Certificate authority (CA) file for self-signed certificates.

Optional

cert / key

Client certificate and private key for mTLS authentication.

Optional

headers

Custom headers for proxy authentication (Basic or Bearer Token).

Optional

Harness Proxy (URL Only)

To use a proxy with no authentication, specify only the proxy URL in the configuration. Follow the pattern: http(s)://host:port.

Harness Proxy with Username/Password Authentication

To authenticate using a username and password, implement the BasicCredentialsProvider interface and override the required methods.

Harness Proxy with JWT Token Authentication

To authenticate using a bearer token (e.g., JWT), include the Proxy-Authentication header with bearer authentication. If your token expires, consider renewing it proactively as shown below:

Harness Proxy with a custom CA certificate or mTLS Authentication

To provide a custom CA X.509 certificate for self-signed certificates or configure mutual TLS (mTLS) authentication, use the corresponding agent properties to supply the required files.

Verify connection

After initialization, all SDK requests are routed through the configured proxy. You can verify successful routing by checking your proxy logs or a network monitor for SDK traffic.

Advanced use cases

This section describes advanced use cases and features provided by the SDK.

Subscribe to events

SUPPORTED SDK VERSIONS

SDK events and event metadata are supported in the Node.js SDK version 11.1.0 or later.

You can listen for four different events from the SDK.

  • SDK_READY. This event fires once the SDK is ready to evaluate treatments using the most up-to-date version of your rollout plan, downloaded from Harness servers.

  • SDK_READY_TIMED_OUT . This event fires if the SDK could not fully download the data from Harness servers (SDK_READY event), within the time specified by the ready setting of the SplitClientConfig object. This event does not indicate that the SDK initialization was interrupted. The SDK continues downloading the rollout plan and fires the SDK_READY event when finished. This delayed SDK_READY event may happen with slow connections or large rollout plans with many feature flags, segments, or dynamic configurations.

  • SDK_UPDATE. This event fires whenever your rollout plan is changed. Listen for this event to refresh your app whenever a feature flag or segment is changed in Harness FME.

These events provide hooks to run custom logic whenever the SDK state changes.

Include metadata

metadata provides additional context for events:

  • SDK_READY: Includes initialCacheLoad (true if no cached data from a previous session was available) and lastUpdateTimestamp (milliseconds since epoch when the cache was last updated).

  • SDK_UPDATE: Includes Type (FLAGS_UPDATE or SEGMENTS_UPDATE) and Names (list of impacted flags; empty for segment-only updates).

  • SDK_READY_TIMED_OUT: No metadata is included.

Working with both sync and async storage

You can write code that works with all type of SDK storage. For example, you might have an application that you want to run on both REDIS and MEMORY storage types. To accommodate this, check if the treatments are thenable objects to decide when to execute the code that depends on the feature flag.

Troubleshooting

Dependency on Old Version of Package url-parse

Node.js SDK has a dependency on an old version of package url-parse (< 1.5.9), which is flagged as vulnerable in security scans.

This package is part of a dependency chain in the eventsource package: @splitsoftware/splitio > eventsource > original > url-parse.

To upgrade the url-parse package, you can use the following commands depending on your package manager:

For npm environment:

For yarn environment:

Alternatively, you can add a resolutions field to your app’s package.json to force the use of a fixed version:

Then run:

Using getTreatment() in Localhost Mode Does Not Work with then() and catch() Blocks

When implementing the Node.js SDK with Redis storage, the getTreatment method returns a Promise, so it works fine with .then() and .catch() blocks.

However, when testing the SDK in localhost mode like this:

It throws the error:

In Redis storage mode, getTreatment() returns a Promise because it wraps a Redis fetch call. But in localhost mode, there is no Redis call, so getTreatment() does not return a Promise, causing .then() to fail.

Wrap the SDK client creation and getTreatment() call inside an async function, and use then() and catch() on the returned Promise as shown below:

While using Localhost mode, error generated: Cannot find name 'path'

Using Node.js SDK, when trying to run the code below in Typescript file using Localhost mode:

The following error is thrown:

This is a node issue. TypeScript needs typings for any module, except if that module is not written in TypeScript.

You need to install the following package by running the command: npm i @types/node -D.

"/node_modules/@splitsoftware/splitio/types"' has no exported member 'SplitIO'

Using Node.js SDK, when trying to import SplitIO as a namespace in TypeScript:

The following error is thrown:

TypeScript implicitly imports SplitIO namespace when doing import { SplitFactory } from '@splitsoftware/splitio';, and even the “typeRoots” config is not affecting it because the declaration file is included in the SDK package and the “types” field is properly configured.

You can explicitly import the SplitIO namespace (for example, on modules/files where SplitFactory is not being imported). To achieve this, include the line:

This requires including "allowSyntheticDefaultImports": true in tsconfig.

Last updated

Was this helpful?