This topic explains how to configure contexts in LaunchDarkly SDKs. This feature is available for all SDKs.
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Feature flags use contexts during evaluation to determine which variation to use, based on your flag targeting rules.
Each context contains attributes that describe what you know about that context. Additionally, each context has a kind, so that you can group attributes together conceptually. For example, user contexts often include context attributes like name, email address, location, and so on. However, you can create other context kinds like organization or device. An organization context kind might include attributes like “name” or “address,” and a device context kind might include attributes like “type” or “operating system.” You can also create a context with a kind of “multi” and include several associated contexts together in a multi-context. To learn more, read Contexts.
Every LaunchDarkly SDK lets you configure contexts to return specific data to LaunchDarkly. Any attributes you pass to LaunchDarkly as part of a context become available on the Contexts list. The attribute values determine which variation of a feature flag, or which version and prompt from a config, the customer receives.
Here is an image of the Contexts list:

Every context is required to have a key. Keys are always transmitted to LaunchDarkly. Typically, you supply the key when you create the context. For some client-side SDKs, if you mark the context as anonymous then SDK can generate the key for you. To learn more, read Anonymous contexts and users.
Keys must be a string type. Keys must be unique, deterministic, and should not contain personally identifiable information (PII). Keys must be consistent, which means the same person must correspond to the same key across different services to contribute to consistent flag evaluations. You can use a primary key or a hash, as long as the same person always has the same key. We recommend using a hash if possible.
The SDK only evaluates flags or customizes configs based on the context you define and then provide in the call. The SDK does not use the attributes shown on the Contexts list, and context attributes are not synchronized across SDK instances. You must provide all applicable attributes for each evaluation in the SDK for your targeting rules to apply correctly.
Attributes other than the key are optional. There are two types of attributes: built-in attributes, which are LaunchDarkly names, and custom attributes, which you can name anything you choose.
The only built-in attributes for contexts are key, kind, name, and anonymous.
Only the key is required. It must be a string.
The value of kind can be:
multi, if this is a multi-contextuserThe value of name can be any string.
The value of anonymous can be true or false. If not specified, it defaults to false.
You can define additional attributes for a context by passing in a name and value for each. These additional attributes let you add targeting rules for your flags based on any data that you want to send to LaunchDarkly. Attribute values can be any JSON type, including boolean, number, string, array, or object. To learn more, read Context attributes.
If you create an attribute with a name already in use by a built-in attribute, the SDK will behave unpredictably.
The only built-in attributes for contexts are kind, key, name, and anonymous. If you work with an older SDK that only supports users, there are several additional built-in attributes.
To learn how to configure private attributes in your SDK, read Private attributes.
The SDK only evaluates flags or customizes configs based on the context you define and then provide in the call. You must provide all applicable attributes for each evaluation in the SDK for your targeting rules to apply correctly.
The context can include as many attributes as you like. However, we recommend that you take the following considerations into account:
useReport option. URLs in some browsers cannot exceed 2k characters in length.Details about each SDK’s configuration are available in the SDK-specific sections below.
Here are the configuration options for contexts in client-side SDKs:
In the client-side .NET SDK, you can construct a Context that only has a key by calling Context.New. The context kind defaults to “user,” or you can supply a different context kind. Alternatively, you can use Context.Builder, which allows setting all properties.
The argument to Builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes, which you can set with .Name() and .Kind(), expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be any JSON type, including booleans, numbers, strings, arrays, or objects. The SDK uses the LdValue type to represent arrays and objects. The client-side .NET SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the .NET (client-side) SDK, read Private attributes.
To learn how to configure anonymous contexts in the .NET (client-side) SDK, read Anonymous contexts and users.
In the Android SDK, use a builder pattern to construct contexts. The argument to builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes, which you can set with .name() and .kind(), expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be any JSON type, including boolean, number, string, array, or object. The Android SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the Android SDK, read Private attributes.
To learn how to configure anonymous contexts in the Android SDK, read Anonymous contexts and users.
LDUser removal
Version 4 of the Android SDK replaced users with contexts. Starting in version 5, the deprecated LDUser is removed. To learn more about replacing users with contexts, read the Android SDK 3.x to 4.0 migration guide and Best practices for upgrading users to contexts.
In the C++ (client-side) SDK, you can construct a context using the ContextBuilder. The arguments to .Kind() are the context kind and key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key, an email address, or a hash for the key, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The name and kind attributes, which you can set with .Name() and .Kind(), expect string values. Other attribute values can be any JSON type, including boolean, number, string, array, or object.
To learn how to configure private attributes in the C++ (client-side) SDK, read Private attributes.
To learn how to configure anonymous contexts in the C++ (client-side) SDK, read Anonymous contexts and users.
The Electron SDK does not support contexts. Instead, it supports users. You can think of these as contexts with a context kind of “user.” Other context kinds are not supported.
Here’s an example of a user:
The key property is the user’s key. The key should uniquely identify each user. You can use a primary key or a hash, as long as the same user always has the same key. We recommend using a hash if possible. In this example, the hash is "example-user-key".
By default, when the SDK requests feature flags from LaunchDarkly, it makes an HTTP GET request with the user properties encoded in the URL. If you do not want user keys or other properties to be in request URLs, enable the useReport option in your client configuration. The SDK sends user data in the body of an HTTP REPORT request instead.
Most of the built-in attributes, like names and email addresses, expect string values. Custom attribute values can be booleans, numbers, strings, or arrays. If you enter a custom value on the Users list that looks like a number or a boolean, the SDK interprets it that way.
To learn how to configure private attributes in the Electron SDK, read Private attributes.
To learn how to configure anonymous users in the Electron SDK, read Anonymous contexts and users.
In the Flutter SDK, use a builder pattern to construct contexts. The arguments to LDContextBuilder are the context’s kind and key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The required kind and optional name attributes expect string values. Other attribute values can be any JSON type, including boolean, number, string, array, or object. Attribute values in the Flutter SDK use the LDValue class to support the various underlying types for the values. The Flutter SDK is strongly-typed, so be aware of this distinction.
Starting in version 4, the Flutter SDK provides setters so that you do not have to create an LDValue yourself. Instead, you can use setBool, setNum, and setString when adding attributes to a context.
To learn how to configure private attributes in the Flutter SDK, read Private attributes.
To learn how to configure anonymous contexts in the Flutter SDK, read Anonymous contexts and users.
In the iOS SDK, you can construct a context using LDContextBuilder. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
You can define additional attributes for a context by passing in a name and value for each. Additional attributes can be any JSON type, including boolean, number, string, array, or object.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn more about the specific context properties that are available in this SDK, read LDContextBuilder.
To learn how to configure private attributes in the iOS SDK, read Private attributes.
To learn how to configure anonymous contexts in the iOS SDK, read Anonymous contexts and users.
LDUser removal
Version 8 of the iOS SDK replaced users with contexts. Starting in version 9, the deprecated LDUser is removed. To learn more about replacing users with contexts, read the iOS SDK 7.x to 8.0 migration guides for Swift or Objective-C and Best practices for upgrading users to contexts.
In the JavaScript SDK, construct a context using key/value pairs for the context attributes. Contexts use the LDContext type. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example of a context:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attributes can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
We recommend against using personally identifiable information (PII) in context keys. If the key attribute you rely on in your context JSON does contain PII, you should enable the useReport option by sending the evaluation context as a JSON base64 URL-encoded path parameter. When you enable useReport, the SDK fetches flag settings by sending the context JSON in the body of a REPORT request instead, hiding that information from request logs.
To learn how to configure private attributes in the JavaScript SDK, read Private attributes.
To learn how to configure anonymous contexts in the JavaScript SDK, read Anonymous contexts and users.
In the Node.js (client-side) SDK, construct a context using key/value pairs for the context attributes. Contexts use the LDContext type. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example of a context:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The kind and name attributes expect string values. Other attribute values can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
By default, when the SDK requests feature flags from LaunchDarkly, it makes an HTTP GET request with the user properties encoded in the URL. If you do not want keys or other properties to be in request URLs, enable the useReport option in your client configuration. The SDK sends data in the body of an HTTP REPORT request instead.
To learn how to configure private attributes in the Node.js (client-side) SDK, read Private attributes.
To learn how to configure anonymous contexts in the Node.js (client-side) SDK, read Anonymous contexts and users.
In the React Native SDK, construct a context using key/value pairs for the context attributes. Contexts use the LDContext type.
The first attribute in the object is the key. In the React Native SDK, both key and kind are required. They are the only mandatory attributes. The combination of key and kind must uniquely identify each context. You can use any value for the key, such as a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
If the context is anonymous, you should set the key to an empty string. The SDK will automatically set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.
Other attributes can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the React Native SDK, read Private attributes.
To learn how to configure anonymous contexts in the React Native SDK, read Anonymous contexts and users.
All context-related functionality provided by the JavaScript SDK is also available in the React Web SDK.
Unlike the JavaScript SDK, the React Web SDK does not require a context object for initialization. If you do not specify one, the React SDK uses an anonymous context by default.
In the Roku SDK, use LaunchDarklyCreateContext to construct a context. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attributes can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the Roku SDK, read Private attributes.
To learn how to configure anonymous contexts in the Roku SDK, read Anonymous contexts and users.
Here are the configuration options for contexts in server-side SDKs:
In the server-side .NET SDK, you can construct a Context that only has a key by calling Context.New. The context kind defaults to “user,” or you can supply a different context kind. Alternatively, you can use the Context.Builder method for building a context with other properties.
The argument to Builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes, which you can set with .Name() and .Kind(), expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. The SDK uses the LdValue type to represent arrays and objects. The .NET SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the .NET (server-side) SDK, read Private attributes.
To learn how to configure anonymous contexts in the .NET (server-side) SDK, read Anonymous contexts and users.
The Apex SDK does not support contexts. Instead, it supports users. You can think of these as contexts with a context kind of “user.” Other context kinds are not supported.
Here’s an example of a user:
The argument to Builder is the user’s key. The key should uniquely identify each user. You can use a primary key or a hash, as long as the same user always has the same key. We recommend using a hash if possible. In this example, the hash is "example-user-key".
To learn how to configure private attributes in the Apex SDK, read Private attributes.
To learn how to configure anonymous users in the Apex SDK, read Anonymous contexts and users.
In the C++ (server-side) SDK, you can construct a context using the ContextBuilder. The arguments to .Kind() are the context kind and key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash for the key, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
If you are working in C, when you are done with the context ensure that you free the structure:
To learn how to configure private attributes in the C++ (server-side) SDK, read Private attributes.
To learn how to configure anonymous contexts in the C++ (server-side) SDK, read Anonymous contexts and users.
In the Erlang SDK, use ldclient_context:set and ldclient_context:new to define and construct a context.
The key property is the context’s key. The key is the only mandatory context attribute. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is “user.”
Here’s an example of a context:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
To learn how to configure private attributes in the Erlang SDK, read Private attributes.
To learn how to configure anonymous contexts in the Erlang SDK, read Anonymous contexts and users.
In the Go SDK, you have two options for how you want to manage contexts:
LDScopedClient. This is a wrapper around LDClient that lets you specify the evaluation context to use for all operations. The scoped client’s context is a multi-context, and you can update the multi-context with additional associated contexts at any time.LDClient.LDScopedClient is in beta. It is still undergoing testing and active development. Its functionality may change without notice, including becoming backwards incompatible.
The Go SDK defines a Context struct and a Builder. The context key is the only mandatory context attribute. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
You can also use the context builder to create each of the individual contexts:
Each individual context within a multi-context can have the same attributes. The only restriction is that each context has to have a different context kind from the others within the multi-context.
If you’re working with LDScopedClient, you don’t need to manually create the multi-context. Instead, you can add new contexts to the scoped client’s multi-context as they become available, or update existing contexts:
After you create a scoped client, we recommend adding it your Go context. Another advantage of using LDScopedClient is that you can pass the scoped client to any logic that already takes a Go context (context.Context), using utility methods provided in the SDK. This means the scoped client is implicitly passed around through all of your code that uses context.Context, and you can access the scoped client anywhere in your application logic. To learn more, read Use Go contexts with LDScopedClient.
To learn more about the available LaunchDarkly context attributes, read Context and Builder.
The kind and name attributes expect string values. You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. These types are all represented by the ldvalue.Value type. The Go SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the Go SDK, read Private attributes.
To learn how to configure anonymous contexts in the Go SDK, read Anonymous contexts and users.
In the Haskell SDK, use makeContext to create a new context. The argument to makeContext is the context’s key. The key is the only mandatory attribute. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example of a context:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
To learn how to configure private attributes in the Haskell SDK, read Private attributes.
To learn how to configure anonymous contexts in the Haskell SDK, read Anonymous contexts and users.
In the Java SDK, use a builder pattern to construct contexts. The argument to Builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The documentation for ContextBuilder shows you all the attributes that LaunchDarkly supports by default.
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or objects. If you pass a value that looks like a number or a boolean, the SDK interprets it that way. The Java SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the Java SDK, read Private attributes.
To learn how to configure anonymous contexts in the Java SDK, read Anonymous contexts and users.
In the Lua SDK, use makeContext to construct a context of any kind. This requires a context key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible. The context attributes are defined as part of the context kind.
To construct a user context specifically, you can use makeUser. Both makeUser and makeContext require a context key. You can omit the kind option if you construct your context with makeUser. This method is a convenience to make upgrading from the Lua SDK version 1.x to version 2.0 easier. It is deprecated and may be removed in future versions.
Here’s an example of a context:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
To learn more, read makeUser and makeContext.
To learn how to configure private attributes in the Lua SDK, read Private attributes.
To learn how to configure anonymous contexts in the Lua SDK, read Anonymous contexts and users.
In the Node.js (server-side) SDK, contexts are JSON objects. The key property is the context key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the Node.js (server-side) SDK, read Private attributes.
To learn how to configure anonymous contexts in the Node.js (server-side) SDK, read Anonymous contexts and users.
In the PHP SDK, use a builder pattern to construct contexts. The first argument to LDContextBuilder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The kind and name attributes expect string values. Other attribute values can be booleans, numbers, strings, or arrays. If you enter a custom value on the Contexts list that looks like a number or a boolean, the SDK interprets it that way. The PHP SDK is strongly-typed, so be aware of this distinction.
To learn how to configure private attributes in the PHP SDK, read Private attributes.
To learn how to configure anonymous contexts in the PHP SDK, read Anonymous contexts and users.
In version 8.0 and higher of the Python SDK, the Context class has a create method for creating a context with a context kind of “user” and with only a key. It has a builder method for building a context with other properties.
The argument to Context.builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
If you have many attributes to set, you can also create a context from a dictionary:
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn more, read from_dict.
To learn how to configure private attributes in the Python SDK, read Private attributes.
To learn how to configure anonymous contexts in the Python SDK, read Anonymous contexts and users.
In the Ruby SDK, contexts are instances of LaunchDarkly::LDContext. Legacy users can continue to be provided as simple hashes.
The key property is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key, an email address, or a hash string, as long as the same context always has the same key. We recommend using a hash string if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
All context attribute keys, for both built-in and custom attributes, must be symbols and not strings.
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user” and the hash is assumed to be in the legacy user format. Other attribute values can be booleans, numbers, strings, arrays, or objects. If you enter a custom value on the Contexts list that looks like a number or a boolean, the SDK interprets it that way.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the Ruby SDK, read Private attributes.
To learn how to configure anonymous contexts in the Ruby SDK, read Anonymous contexts and users.
The Rust SDK defines a Context struct and a ContextBuilder.
The context key is the only mandatory context attribute. You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is “user.” The combination of key and kind must uniquely identify each context. For the key, you can use a primary key, a hash string, or some other value, as long as the same context always has the same key. We recommend using a hash string if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
To learn more about the available attributes, read Context and ContextBuilder.
The optional name and kind attributes, which you can set with .name() and .kind(), expect string values. Other attribute values can be any JSON type, including booleans, numbers, strings, arrays, or objects. These types are all represented by the AttributeValue type. The Rust SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
To learn how to configure private attributes in the Rust SDK, read Private attributes.
To learn how to configure anonymous contexts in the Rust SDK, read Anonymous contexts and users.
Here are the configuration options for contexts in edge SDKs.
To configure contexts, the Akamai SDK uses the same code as the Node.js server-side SDK.
The Akamai SDK does not support sending events, so private attributes are not supported.
To configure contexts, the Cloudflare SDK uses the same code as the Node.js server-side SDK.
To configure contexts, the Fastly SDK uses the same code as the Node.js server-side SDK.
To configure contexts, the Vercel SDK uses the same code as the Node.js server-side SDK.
Here are the configuration options for contexts in AI SDKs:
In the .NET AI SDK, you can construct a Context that only has a key by calling Context.New. The context kind defaults to “user,” or you can supply a different context kind. Alternatively, you can use the Context.Builder method for building a context with other properties.
The argument to Builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes, which you can set with .Name() and .Kind(), expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. The SDK uses the LdValue type to represent arrays and objects. The .NET SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your config targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your config message. Continuing the same example, you could use {{ldctx.address.city}} in your message, and the value of the “city” field will be substituted when you customize the config. To learn more, read Target with flags and Customizing configs.
To learn how to configure private attributes in the .NET AI SDK, read Private attributes.
To learn how to configure anonymous contexts in the .NET AI SDK, read Anonymous contexts and users.
The Go AI SDK defines a Context struct and a Builder. The context key is the only mandatory context attribute. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
Each individual context within a multi-context can have the same attributes. The only restriction is that each context has to have a different context kind from the others within the multi-context.
You can also use the context builder to create each of the individual contexts:
The kind and name attributes expect string values. You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. These types are all represented by the ldvalue.Value type. The Go SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your config targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your config message. Continuing the same example, you could use {{ldctx.address.city}} in your message, and the value of the “city” field will be substituted when you customize the config. To learn more, read Target with flags and Customizing configs.
To learn how to configure private attributes in the Go AI SDK, read Private attributes.
To learn how to configure anonymous contexts in the Go AI SDK, read Anonymous contexts and users.
In the Node.js (server-side) AI SDK, contexts are JSON objects. The key property is the context key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your config targeting, you can use / as a delimiter to refer to specific fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your config message. Continuing the same example, you could use {{ldctx.address.city}} in your message, and the value of the “city” field will be substituted when you customize the config. To learn more, read Target with configs and Customizing configs.
To learn how to configure private attributes in the Node.js (server-side) AI SDK, read Private attributes.
To learn how to configure anonymous contexts in the Node.js (server-side) AI SDK, read Anonymous contexts and users.
In the Python AI SDK, the Context class has a create method for creating a context with a context kind of “user” and with only a key. It has a builder method for building a context with other properties.
The argument to Context.builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
If you have many attributes to set, you can also create a context from a dictionary:
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or objects.
If an attribute is a JSON object, then in your config targeting, you can use / as a delimiter to refer to specific fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your config message. Continuing the same example, you could use {{LDCTX.address.city}} in your message, and the value of the “city” field will be substituted when you customize the config. To learn more, read Target with configs and Customizing configs.
To learn how to configure private attributes in the Python AI SDK, read Private attributes.
To learn how to configure anonymous contexts in the Python AI SDK, read Anonymous contexts and users.
In the Ruby AI SDK, contexts are instances of LaunchDarkly::LDContext.
The key property is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key, an email address, or a hash string, as long as the same context always has the same key. We recommend using a hash string if possible.
Here’s an example:
Here’s how to construct a context with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user” and the hash is assumed to be in the legacy user format. Other attribute values can be booleans, numbers, strings, arrays, or objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
You can use . as a delimiter in your config message. Continuing the same example, you could use {{LDCTX.address.city}} in your message, and the value of the “city” field will be substituted when you customize the config. To learn more, read Target with configs and Customizing configs.
To learn how to configure private attributes in the Ruby AI SDK, read Private attributes.
To learn how to configure anonymous contexts in the Ruby AI SDK, read Anonymous contexts and users.