Skip to main content

Evaluation API

The Evaluation API is the primary component of OpenFeature that application authors interact with. The Evaluation API allows developers to evaluate feature flags to alter control flow and application characteristics.

Setting a Provider

Before you can start evaluating flags, you must set a provider. The provider is the translation layer between the evaluation API and the flag system you use.

import { OpenFeature } from '@openfeature/server-sdk';

OpenFeature.setProvider(new YourProviderOfChoice());

Some providers need to establish connections, load flag data, or validate configuration before they can resolve flags accurately. If your SDK exposes a blocking or awaitable registration method, use it during startup when your application needs to avoid default values while the provider initializes. If you register the provider asynchronously, attach event handlers for PROVIDER_READY and PROVIDER_ERROR so your application can react when initialization succeeds or fails. See Events for language-specific handler examples.

Creating a client

The OpenFeature client is a lightweight abstraction used to evaluate feature flags. If your application is small, you may use a single client for your whole application. In larger applications, it may be helpful to create multiple clients, each with different configuration to fit the needs of different sub-modules. Clients may also be created dynamically, per each HTTP request, for instance.

const client = OpenFeature.getClient('my-app');

Flag Evaluation

Basic Evaluation

The client can be used to do basic flag evaluation, which simply returns flag values of a particular type. The default value must also be specified. In the case of any error during flag evaluation, the default value will be returned, so give consideration to your default values!

// get a bool value
const boolValue = await client.getBooleanValue('boolFlag', false);

// get a string value
const stringValue = await client.getStringValue('stringFlag', 'default');

// get an numeric value
const numberValue = await client.getNumberValue('intFlag', 1);

// get an object value
const object = await client.getObjectValue<MyObject>('objectFlag', {});

Detailed Evaluation

In addition to basic evaluation, detailed evaluation methods are available. These return the value, as well as additional metadata about the flag evaluation in the Evaluation Details structure.

Evaluation Details Structure Fields

FieldDescription
flag keythe unique identifier for a feature flag
valuethe value returned from a flag evaluation
reason (optional)a string explaining why the flag value was returned
variant (optional)the variant associated with the return flag value
flag metadata (optional)a key-value structure which supports definition of arbitrary properties
error code (optional)an error code that categorizes flag evaluation errors
error message (optional)a string detailing the error
// get details of boolean evaluation
const boolDetails = await client.getBooleanDetails('boolFlag', false);

// get details of string evaluation
const stringDetails = await client.getStringDetails('stringFlag', 'default');

// get details of numeric evaluation
const numberDetails = await client.getNumberDetails('intFlag', 1);

// get details of object evaluation
const objectDetails = await client.getObjectDetails<MyObject>('objectFlag', {});

API Shutdown

The OpenFeature API can shut down all registered providers so they can release connections, background workers, file watchers, or other resources. Call this from your application shutdown path after you stop evaluating flags. The method name varies by SDK; use the shutdown or close function documented by the SDK you are using.

import { OpenFeature } from '@openfeature/server-sdk';

await OpenFeature.close();