Skip to main content
Version: Config V2

OpenFeature Provider for Node.js

ConfigCat OpenFeature Provider for Node.js on GitHub

Getting started

1. Install the provider

npm i @openfeature/config-cat-provider

2. Initialize the provider

The ConfigCatProvider.create() function takes the SDK key and an optional options argument containing additional configuration options for the ConfigCat Node.js SDK:

import { OpenFeature } from "@openfeature/server-sdk";
import { ConfigCatProvider } from '@openfeature/config-cat-provider';
import { createConsoleLogger, LogLevel, PollingMode } from "configcat-node";

// Build options for the ConfigCat SDK.
const options = {
logger: createConsoleLogger(LogLevel.Info),
setupHooks: (hooks) => hooks.on('clientReady', () => console.log('Client is ready!')),
// ...
}

// Configure the provider.
await OpenFeature.setProviderAndWait(ConfigCatProvider.create('#YOUR-SDK-KEY#', PollingMode.AutoPoll, options));

// Create a client.
const client = OpenFeature.getClient();

For more information about all the configuration options, see the Node.js SDK documentation.

3. Evaluate your feature flag

const isAwesomeFeatureEnabled = await client.getBooleanValue('isAwesomeFeatureEnabled', false);
if (isAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}

4. Cleaning up

On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client.

await OpenFeature.clearProviders();

Evaluation Context

An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.

The following table shows how the different context attributes are mapped to User Object attributes.

Evaluation contextUser ObjectRequired
targetingKeyidentifier
emailemail
countrycountry
Any othercustom

To evaluate feature flags for a context, use the OpenFeature Evaluation API:

const context = {
targetingKey: '#SOME-USER-ID#',
email: '[email protected]',
country: 'CountryID',
};

const isAwesomeFeatureEnabled = await client.getBooleanValue('isAwesomeFeatureEnabled', false, context);

Look under the hood