Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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 the LaunchDarkly SDKs.About flag evaluation
The flag evaluation feature requires a feature flag key and the context that encounters the flag in your application. It returns the value of the feature flag variation for that context, based on the flag targeting rules you have created, including any prerequisite flags. 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:
getFeatureFlag endpoint in LaunchDarkly’s REST API.
Details about each SDK’s configuration are available in the SDK-specific sections below:
Client-side SDKs
This feature is available for all of our client-side SDKs:- .NET (client-side)
- Android
- C++ (client-side)
- Electron
- Flutter
- iOS
- JavaScript
- Node.js (client-side)
- React Native
- React Web: The React Web SDK relies on the JavaScript SDK for this functionality.
- Roku
.NET (client-side)
Expand .NET (client-side) code sample
Expand .NET (client-side) code sample
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:Android
Expand Android code sample
Expand Android code sample
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:C++ (client-side)
Expand C++ (client-side) code sample
Expand C++ (client-side) code sample
BoolVariation or StringVariation.Here is an example:Client documentation.Electron
Expand Electron code sample
Expand Electron code sample
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.To evaluate any feature flag for the current context, call variation:variation is always either one of the variations you defined for your flag in the flag’s Variations tab, or the fallback value. The fallback value is the second parameter to variation.You can also fetch all flags for the current context.Here is an example: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
Expand Flutter code sample
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:boolVariation.iOS
Expand iOS code sample
Expand iOS code sample
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:Java
ScriptExpand JavaScript code sample
Expand JavaScript code sample
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:Node.js (client-side)
Expand Node.js (client-side) code sample
Expand Node.js (client-side) code sample
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:React Native
Expand React Native code sample
Expand React Native code sample
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: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.- 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’svariation()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 thevariation()call.
Roku
Expand Roku code sample
Expand Roku code sample
*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:Server-side SDKs
This feature is available for all of our server-side SDKs:- .NET (server-side)
- Apex
- C++ (server-side)
- Erlang
- Go
- Haskell
- Java
- Lua
- Node.js (server-side)
- PHP
- Python
- Ruby
- Rust
.NET (server-side)
Expand .NET (server-side) code sample
Expand .NET (server-side) code sample
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:Apex
Expand Apex code sample
Expand Apex code sample
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:C++ (server-side)
Expand C++ (server-side) code sample
Expand C++ (server-side) code sample
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:Erlang
Expand Erlang code sample
Expand Erlang code sample
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.Go
Expand Go code sample
Expand Go code sample
Variation methods determine which variation of a flag LaunchDarkly serves to a specific context:-
If you are working with an
LDScopedClient, the context is the multi-context comprised of all contexts currently in the scoped client. -
If you are working with the
LDClient, the context is the one you pass to theVariationmethods.
Variation methods also take the feature flag key 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:BoolVariationIntVariationFloat64VariationStringVariationJSONVariation, which can be any JSON type.
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
Expand Haskell code sample
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:Java
Expand Java code sample
Expand Java code sample
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:boolVariationintVariationdoubleVariationstringVariationjsonValueVariation, which can be of any JSON type.
Lua
Expand Lua code sample
Expand Lua code sample
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:boolVariation.Node.js (server-side)
Expand Node.js (server-side) code sample
Expand Node.js (server-side) code sample
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:variation method for each type, such as boolVariation or stringVariation. These typed methods return a Promise and are recommended over the generic variation method.You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.PHP
Expand PHP code sample
Expand PHP code sample
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:Python
Expand Python code sample
Expand Python code sample
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:Ruby
Expand Ruby code sample
Expand Ruby code sample
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:Rust
Expand Rust code sample
Expand Rust code sample
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:bool_variation,int_variation,float_variation,str_variationfor strings, andjson_variation, which can be any JSON type.
Edge SDKs
This feature is available for all of our edge SDKs:Akamai
Expand Akamai code sample
Expand Akamai code sample
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:variation. The LDClient also provides typed variation methods for type-safe usage in TypeScript: boolVariation, stringVariation, numberVariation, jsonVariation.Every time you evaluate a flag, the SDK fetches the flag data from the EdgeKV store. Your Akamai resource tier may limit how many of these queries you can make while a single worker handler is being executed. To learn more, read Understand resource limits and caching options.Cloudflare
Expand Cloudflare code sample
Expand Cloudflare code sample
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:variation. The LDClient also provides typed variation methods for type-safe usage in TypeScript: boolVariation, stringVariation, numberVariation, jsonVariation.Fastly
Expand Fastly code sample
Expand Fastly code sample
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:variation.The LDClient also provides typed variation methods for type-safe usage in TypeScript: boolVariation, stringVariation, numberVariation, jsonVariation.Vercel
Expand Vercel code sample
Expand Vercel code sample
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:variation. The LDClient also provides typed variation methods for type-safe usage in TypeScript: boolVariation, stringVariation, numberVariation, jsonVariation.