No results for ""
EXPAND ALL
  • Home
  • API docs

GIVE DOCS FEEDBACK

Evaluating flags

Read time: 19 minutes
Last edited: Jun 14, 2024

Overview

This topic explains how to use the flag evaluation feature to serve different feature flag variations to contexts and users. This feature is available for all of our SDKs.

Newer versions of LaunchDarkly SDKs replace users with contexts

A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more, read Contexts.

Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.

Configuring variations

Feature flag variations determine what a context or user receives when they encounter a feature flag. Every flag has at least two variations: one for when targeting is off, and one for when it's on. To learn more, read Creating flag variations.

This is an example of a flag with three variations:

The "Variations" definition for multivariate flag.
The "Variations" definition for multivariate flag.

Flags also have fallback values. The fallback value is defined in your code, is one of the flag's variations, and is only returned if an error occurs. For example, the SDK serves the fallback value if LaunchDarkly is unreachable, the feature flag key doesn't exist, or the context or user doesn't have a key specified.

The flag evaluation feature adds a context to the Contexts list, if a context with the same key does not already exist. However, each SDK evaluates flags based only on the object you provide in the evaluation call.

Contexts without a context kind are interpreted as users

If you are working with an older version of the SDK, you may provide a user object in the evaluation call. If you are using an SDK that supports contexts, and don't supply the context kind, then that object is automatically interpreted as a context with a kind of "user." To learn more, read The foundations of contexts.

The SDK does not automatically use the attributes shown on the Contexts list, and attributes are not synchronized across SDK instances. You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly. To learn more, read Context configuration.

You do not need to create contexts or users manually, but if you want to, you can with the identify feature. To learn more, read Identifying and changing contexts.

Details about each SDK's configuration are available in the SDK-specific sections below:

  • Client-side SDKs
  • Server-side SDKs
  • Edge SDKs

Client-side SDKs

This feature is available for all of our client-side SDKs:

.NET (client-side)

Expand .NET (client-side) code sample

The Variation method determines which variation of a flag LaunchDarkly serves to the current context. It requires the flag key and a fallback value. After the context is evaluated, you can view it on the Contexts list.

In the client-side .NET SDK, there is a variation method for each type, such as BoolVariation or StringVariation.

Here is an example:

client.BoolVariation("flag-key-123abc", false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Android

Expand Android code sample

The variation method determines which variation of a flag LaunchDarkly serves to the current context. It requires the flag key and a fallback value. After the context is evaluated, you can view it on the Contexts list. When LDClient is initialized for the first time at app launch, contexts receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

In Android, there is a variation method for each type, such as boolVariation or stringVariation.

Here is an example:

boolean variationResult = client.boolVariation(flagKey, false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

C++ (client-side)

Expand C++ (client-side) code sample

The variation methods determine which variation of a flag is served to the current context. The variation method signatures take the feature flag key and a fallback value. When the client is initialized for the first time, contexts receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

There are variation methods for each type, such as BoolVariation or StringVariation.

Here is an example:

bool show_feature = client.BoolVariation("flag-key-123abc", false);
if (show_feature) {
// Application code to show the feature
} else {
// The code to run if the feature is off
}

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

To learn more, read the Client documentation.

Electron

Expand Electron code sample

The variation method determines which variation of a flag LaunchDarkly serves to a specific user. It requires the flag key and a fallback value. After the user is evaluated, you can view it on the Contexts list.

To evaluate any feature flag for the current user, call variation:

const flagValue = client.variation('flag-key-123abc', false);
// proceed based on flag value, for example:
if (flagValue) {
// feature flag targeting is on
} else {
// feature flag targeting is off
}

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

The return value of variation is always either one of the variations you defined for your flag in the flag's Targeting tab, or the fallback value. The fallback value is the second parameter to variation.

You can also fetch all flags for the current user.

Here is an example:

const flags = client.allFlags();
const flagValue = flags['flag-key-123abc'];

This returns a key-value map of all your feature flags. It contains null values for any flags that could not be evaluated.

Both of these methods are synchronous. The client always has the last known flag values in memory, so retrieving them does not involve any input/output (I/O).

Flutter

Expand Flutter code sample

The variation methods determine which variation of a flag LaunchDarkly serves to the current context. variation calls take the feature flag key and a fallback value. After the context is evaluated, you can view it on the Contexts list. When the client is initialized for the first time at app launch, end users receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

In Flutter, there is a variation method for each type, such as boolVariation or stringVariation.

Here is an example:

bool variationResult = client.boolVariation(flagKey, false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

To learn more, read boolVariation.

iOS

Expand iOS code sample

The variation functions determine which variation of a flag LaunchDarkly serves the current context. Variation functions take the feature flag key and a fallback value as parameters. After the context is evaluated, you can view it on the Contexts list. If the flag does not exist, cannot be cast to the correct return type, or the LDClient is not started, the function returns the fallback value.

In the iOS SDK, there is a variation method for each type, such as boolVariation or jsonVariation.

Here is an example:

let boolFlagValue = LDClient.get()!.boolVariation(forKey: "bool-flag-key-123abc", defaultValue: false)
let jsonFlagValue = LDClient.get()!.jsonVariation(forKey: "json-flag-key-456def", defaultValue: ["enabled": false, "special": "none"])

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

JavaScript

Expand JavaScript code sample

The variation method determines which variation of a flag LaunchDarkly serves to the current context. variation calls take the feature flag key and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example of the variation method:

client.variation('flag-key-123abc', false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Node.js (client-side)

Expand Node.js (client-side) code sample

The variation method determines which variation of a flag LaunchDarkly serves to the current context. It requires the flag key and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

const value = client.variation('flag-key-123abc', false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

React Native

Expand React Native code sample

The variation method determines the flag variation to serve for a specific context.

In React Native, there is a variation method for each type, such as boolVariation or stringVariation. In the React Native SDK version 10, there is also a hook for each type, such as useBoolVariation or useStringVariation.

Variation calls take the feature flag key and a fallback value. When ReactNativeLDClient is initialized for the first time at app launch, end users receive feature flag fallback values until an initial connection to LaunchDarkly is completed. You are not required to specify a context when you initialize ReactNativeLDClient. If you do not specify a context initially, end users will also receive fallback values until you do specify a context by calling identify(). To learn more, read Identifying and changing contexts.

Here is an example:

const boolResult = useBoolVariation('bool-flag-key-123abc', false);
const numResult = useNumberVariation('numeric-flag-key-123abc', 2);
const stringResult = useStringVariation('string-flag-key-123abc', '');
const jsonResult = useJsonVariation('json-flag-key-123abc', {});

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

To learn more, read boolVariation.

The SDK also includes an untyped method to determine the variation of a feature flag. To learn more, read variation. We recommend using the strongly typed variation methods, such as boolVariation, which perform type checks and handle type errors.

React Web

The React Web SDK relies on the JavaScript SDK for this functionality.

Fallback values in the React Web SDK

The fallback variation for each flag is returned if an error occurs. For example, the SDK serves the fallback variation if LaunchDarkly is unreachable, the feature flag key doesn't exist, or the context or user doesn't have a key specified.

In the LaunchDarkly user interface, on the flag's Targeting tab, a value for the "Fallback variation" information appears. However, if you are using the React Web SDK this displayed value may not match the value actually being served for the fallback variation:

  • A value for the fallback variation may appear in the UI even though you have not defined a fallback variation in your code. Specifically, the React Web SDK automatically uses the results of getting all flags to determine the value of the fallback variation. In some cases, this means that the fallback value is undefined. For example, it may be undefined if the SDK has not finished initializing, or if LaunchDarkly is unreachable.
  • If you use the useLDClient() hook and evaluate flags with the underlying JavaScript SDK's variation() method, then you can define a fallback variation. However, the value displayed in the UI may not match the value of the fallback variation that you set in the variation() call.

Rather than relying on the "Fallback variation" display in the UI, review your application code to determine the fallback variation that the SDK will serve if LaunchDarkly is unreachable.

Roku

Expand Roku code sample

The *variation methods determines which variation of a flag LaunchDarkly serves to the current context. The calls require the flag key and a fallback value. After the context is evaluated, you can view it on the Contexts list.

In the Roku SDK, there is a variation method for each type, such as boolVariation and intVariation.

Here is an example:

' typed variations
myInt = launchDarkly.intVariation("flag-key-123abc", 123)
myBool = launchDarkly.boolVariation("flag-key-123abc", false)
myString = launchDarkly.stringVariation("flag-key-123abc", "hello world!")
myObjectOrArray = launchDarkly.jsonVariation("flag-key-123abc", {"value": 123})
' generic variation
myValue = launchDarkly.variation("flag-key-123abc", false)

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Server-side SDKs

This feature is available for all of our server-side SDKs:

.NET (server-side)

Expand .NET (server-side) code sample

The Variation methods determine which variation of a flag LaunchDarkly serves to a specific context. The methods require the flag key, the Context to evaluate, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

In .NET, there is a Variation method for each type:

Here is an example:

var value = client.BoolVariation("your.feature.key", context, false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Apex

Expand Apex code sample

The variation methods determine which variation of a flag LaunchDarkly serves to a specific user. In Apex, there is a variation method for each type, such as boolVariation or stringVariation. The methods take an LDUser, a feature flag key, and a fallback value. After the user is evaluated, you can view it on the Contexts list.

Here is an example:

Boolean value = client.boolVariation(user, 'your.feature.key', false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

C++ (server-side)

Expand C++ (server-side) code sample

The variation family of functions determine which variation of a flag LaunchDarkly serves to a specific context. The functions require a context, feature flag key, and fallback value. After the context is evaluated, you can view it on the Contexts list.

In C++, there is a variation function for each type, such as BoolVariation or StringVariation.

Here is an example:

bool value = client.BoolVariation(context, "flag-key-123abc", false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Erlang

Expand Erlang code sample

The variation function determines which variation of a flag LaunchDarkly serves to a specific context. The functions take a flag key, context, fallback value, and an instance tag. In the example below, the fallback value is false. The instance tag is optional. After the context is evaluated, you can view it on the Contexts list.

Flag = ldclient:variation(<<"flag-key-123abc">>, #{key => <<"context-key-123abc">>,}, false, your_instance)

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Go

Expand Go code sample

The Variation methods determine which variation of a flag LaunchDarkly serves to a specific context. Variation methods take the feature flag key, a context, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

In Go, there is a Variation method for each type:

Here is an example:

result, _ := client.BoolVariation("your.feature.key", context, false)
// result is now true or false depending on the setting of this boolean feature flag

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

If you are using OpenTelemetry, then instead of using the Variation method for each type, you must use the VariationCtx method for each type. For example, use BoolVariationCtx rather than BoolVariation. The methods are the same except that the VariationCtx methods also require a Go context parameter. This Go context is used in the hook implementation that provides OpenTelemetry support. To learn more, read OpenTelemetry.

Haskell

Expand Haskell code sample

The variation family of functions determine which variation of a flag LaunchDarkly serves to a specific context. The functions take a Client, Context, feature flag key, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

In Haskell, there is a variation function for each type:

Here is an example:

myBoolVariation <- boolVariation client "flag-key-123abc" context False

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Java

Expand Java code sample

The variation methods determine which variation of a flag LaunchDarkly serves to a specific context. The variation methods take the feature flag key, an LDContext, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

In Java, there is a variation method for each type:

Here is an example:

boolean value = client.boolVariation("flag-key-123abc", context, false);

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Lua

Expand Lua code sample

The variation family of functions determine which variation of a flag LaunchDarkly serves to a specific context. The functions take a context, feature flag key, and fallback value. After the context is evaluated, you can view it on the Contexts list.

In Lua, there is a variation function for each type, such as boolVariation or stringVariation.

Here is an example:

local value = client:boolVariation(context, "flag-key-123abc", false)

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly. To learn more, read boolVariation.

Node.js (server-side)

Expand Node.js (server-side) code sample

The variation method determines which variation of a flag LaunchDarkly serves to a specific context. variation calls take the feature flag key, an LDContext, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

client.variation('flag-key-123abc', context, false,
(err, value) => {
// check value and proceed accordingly
});

You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

PHP

Expand PHP code sample

The variation method determines which variation of a flag LaunchDarkly serves to a specific context. variation calls take the feature flag key, an LDContext, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

$value = $client->variation($key, $context, false);

You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

Python

Expand Python code sample

The variation method determines which variation of a flag LaunchDarkly serves to a specific context. variation calls take the feature flag key, a Context, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

show_feature = ldclient.get().variation("your.feature.key", context, False)

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Ruby

Expand Ruby code sample

The variation method determines which variation of a flag LaunchDarkly serves to a specific context. variation calls take the feature flag key, an LDContext or user hash, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

value = client.variation("your.feature.key", context, false)

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

To learn more about constructing the hash, read Context configuration.

Rust

Expand Rust code sample

The variation methods determine which variation of a flag LaunchDarkly serves to a specific context. variation methods take a Context, the feature flag key, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

In Rust, there is a variation method for each type:

Here is an example:

let result = client.bool_variation(&context, "your.feature.key", false);
// result is now true or false depending on the setting of this boolean feature flag

You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

Edge SDKs

This feature is available for all of our edge SDKs:

Akamai

Expand Akamai code sample

The variation method determines which variation of a feature flag a context receives. variation calls take the feature flag key, an LDContext, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

const flagValue = await client.variation('flag-key-123abc', context, false);

You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

Cloudflare

Expand Cloudflare code sample

The variation method determines which variation of a feature flag a context receives. variation calls take the feature flag key, an LDContext, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

const flagValue = await client.variation('flag-key-123abc', context, false);

You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

Vercel

Expand Vercel code sample

The variation method determines which variation of a feature flag a context receives. variation calls take the feature flag key, an LDContext, and a fallback value. After the context is evaluated, you can view it on the Contexts list.

Here is an example:

const flagValue = await client.variation('flag-key-123abc', context, false);

You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.