User and context configuration
Read time: 20 minutes
Last edited: May 30, 2023
Overview
This topic explains how to configure user and context objects in LaunchDarkly SDKs. These features are available for both client-side and server-side SDKs.
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 and segments.
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 context objects
Every LaunchDarkly SDK lets you configure context or user objects to return specific data to LaunchDarkly. Any attributes you pass to LaunchDarkly become available on the Contexts list. The attribute values determine which variation of a feature flag the customer receives.
Every object is required to have a key
. Keys are always transmitted to LaunchDarkly. Typically, you supply the key when you create the context. If you are using a client-side SDK and mark the context as anonymous, the 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 and accurate monthly context instances (MCI). 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 based on the context you configure and then provide in the evaluation 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.
Using built-in and custom attributes
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.
When you work with an SDK that supports contexts, the only built-in attributes are kind
, key
, name
, and anonymous
.
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 during feature flag evaluation.
When you work with an SDK that supports contexts, the only built-in attributes are kind
, key
, name
, and anonymous
. If you work with an 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.
Details about each SDK's configuration are available in the SDK-specific sections below.
Client-side SDKs
Here are the configuration options for context and user entities in client-side SDKs.
- .NET (client-side)
- Android
- C/C++ (client-side)
- Electron
- Flutter
- iOS
- JavaScript
- Node.js (client-side)
- React Native
- React Web: The React SDK relies on the JavaScript SDK for context-related functionality.
- Roku
.NET (client-side)
Expand .NET (client-side) code sample
In the client-side .NET SDK, you can construct a simple Context
, with a context kind of "user," that only has a key by calling Context.New
. Alternatively, you can use Context.Builder
, which allows setting all properties.
Here's an example:
Context context = Context.Builder("context-key-123abc").Set("firstName", "Sandy").Set("lastName", "Smith").Set("email", "sandy@example.com").Set("group", "microsoft").Build();
The argument to Builder
is the context's key. The key should uniquely identify each context. 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. In this example, the hash is "context-key-123abc"
.
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 Targeting with flags.
Here's how to construct a context with a context kind of something other than "user":
var context = Context.New(ContextKind.Of("organization"), "org-key-123abc");
Here's how to construct a multi-context, which includes multiple context kinds:
var userContext = Context.New("user-key-123abc");var orgContext = Context.New(ContextKind.Of("organization"), "org-key-123abc");var multiContext = Context.NewMulti(userContext, orgContext);
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.
Android
Expand Android code sample
In the Android SDK, use a builder pattern to construct contexts.
Here's an example:
LDContext context = LDContext.builder("context-key-123abc").set("email", "sandy@example.com").set("firstName", "Sandy").set("lastName", "Smith").set("group", "Microsoft").build();
The argument to Builder
is the context's key. The key should uniquely identify each context. 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. In this example, the hash is "context-key-123abc"
.
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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
LDContext context1 = LDContext.create(ContextKind.of("organization"), "org-key-123abc");
Here's how to construct a multi-context, which includes multiple context kinds:
LDContext multiContext = LDContext.createMulti(LDContext.create("user-key-123abc"),LDContext.create(ContextKind.of("device"), "device-key-123abc"));
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.
C/C++ (client-side)
Expand C/C++ (client-side) code sample
In the C/C++ (client-side) SDK, you can construct a user using LDUserNew
.
Here's an example:
struct LDJSON *attributes, *groups;groups = LDNewArray();LDArrayPush(groups, LDNewText("Google"));LDArrayPush(groups, LDNewText("Microsoft"));attributes = LDNewObject();LDObjectSetKey("groups", groups);struct LDUser *user = LDUserNew("user-key-123abc");LDUserSetFirstName(user, "Sandy");LDUserSetLastName(user, "Smith");LDUserSetCustomAttributesJSON(user, attributes);
The argument to LDUserNew
is the user's key. The key should uniquely identify each user. You can use a primary key, an email address, 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 "user-key-123abc"
.
Most of the built-in attributes, like names and email addresses, expect string values. Custom attributes 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 C/C++ (client-side) SDK, read Private attributes.
To learn how to configure anonymous users in the C/C++ (client-side) SDK, read Anonymous contexts and users.
Electron
Expand Electron code sample
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.
Here's an example of a user:
const user = {key: 'user-key-123abc',firstName: 'Sandy',lastName: 'Smith',email: 'sandy@example.com',custom: {groups: ['Google', 'Microsoft']}};
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 "user-key-123abc"
.
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.
Flutter
Expand Flutter code sample
In the Flutter SDK, use a builder pattern to construct contexts.
Here's an example:
LDContextBuilder builder = LDContextBuilder();builder.kind('user', 'user-key-123abc').set('email', LDValue.ofString('sandy@example.com')).set('firstName', LDValue.ofString('Sandy')).set('lastName', LDValue.ofString('Smith')).set('group', LDValue.ofString('microsoft'));LDContext context = builder.build();
The arguments to LDContextBuilder
are the context's kind and key. This combination must uniquely identify each context. 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. In this example, the hash is "user-key-123abc"
.
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.
To learn how to configure private attributes in the Flutter SDK, read Private attributes.
To learn how to configure anonymous users in the Flutter SDK, read Anonymous contexts and users.
iOS
Expand iOS code sample
You can pass optional properties to the context object in iOS:
var contextBuilder = LDContextBuilder(key: "context-key-123abc")contextBuilder.trySetValue("name", .string("Sandy"))contextBuilder.trySetValue("email", .string("sandy@example.com"))let context = try contextBuilder.build().get()
This example sets a key and adds the end user's full name and email address.
The key
property should uniquely identify each context. 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. In this example, the hash is "context-key-123abc"
.
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 Targeting with flags.
To learn more about the specific context properties that are available in this SDK, read LDContextBuilder
.
Here's how to construct a multi-context, which includes multiple context kinds:
var userBuilder = LDContextBuilder(key: "user-key-123abc")var deviceBuilder = LDContextBuilder(key: "device-key-123abc")deviceBuilder.kind("device")var multiBuilder = LDMultiContextBuilder()multiBuilder.addContext(try userBuilder.build().get())multiBuilder.addContext(try deviceBuilder.build().get())let context = try multiBuilder.build().get()
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.
JavaScript
Expand JavaScript code sample
Here's an example of a context:
const context = {kind: 'user',key: 'user-key-123abc',firstName: 'Sandy',lastName: 'Smith',email: 'sandy@example.com',groups: ['Google', 'Microsoft']};
The key
property should uniquely identify each context. 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. In this example, the hash is user-key-123abc
.
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 Targeting 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.
Here's how to construct a basic context with a context kind of something other than "user":
const context = {kind: 'organization',key: 'org-key-123abc'};const client = LDClient.initialize('client-side-id-123abc', context);
Here's how to construct a multi-context, which includes multiple context kinds:
const deviceContext = {kind: 'device',type: 'iPad',key: 'device-key-123abc'}const userContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',role: 'doctor'}const multiContext = {kind: 'multi',user: userContext,device: deviceContext}const client = LDClient.initialize('client-side-id-123abc', multiContext)
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.
Node.js (client-side)
Expand Node.js (client-side) code sample
Here's an example of a context:
const context = {kind: 'user',key: 'user-key-123abc',firstName: 'Sandy',lastName: 'Smith',email: 'sandy@example.com',groups: ['Google', 'Microsoft']};
The key should uniquely identify each context. 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. In this example, the hash is "user-key-123abc"
.
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 Targeting 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 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.
Here's how to construct a basic context with a context kind of something other than "user":
const context = {kind: 'organization',key: 'org-key-123abc'};const client = LaunchDarkly.initialize('client-side-id-123abc', context);
Here's how to construct a multi-context, which includes multiple context kinds:
const deviceContext = {kind: 'device',type: 'iPad',key: 'device-key-123abc'}const userContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',role: 'doctor'}const multiContext = {kind: 'multi',user: userContext,device: deviceContext}const client = LaunchDarkly.initialize('client-side-id-123abc', multiContext)
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.
React Native
Expand React Native code sample
Here is a context configuration object:
// key and kind are the only required attributeslet context = {key: 'user-key-123abc',kind: 'user',firstName: 'Sandy',lastName: 'Smith',email: 'sandy@example.com',address: {street: '123 Main St',city: 'Springfield'}};
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. If you mark the context as anonymous, the SDK can generate the key for you. The combination of key
and kind
should 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. In this example, the value is user-key-123abc
.
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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
const context = {kind: 'organization',key: 'org-key-123abc'};await ldClient.configure(config, context);
Here's how to construct a multi-context, which includes multiple context kinds:
const deviceContext = {kind: 'device',type: 'iPad',key: 'device-key-123abc'}const userContext = {kind: 'user',key: 'user-key-123abc',name: 'Sandy',role: 'doctor'}const multiContext = {kind: 'multi',user: userContext,device: deviceContext}await ldClient.configure(config, multiContext);
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.
React Web
All context-related functionality provided by the JavaScript SDK is also available in the React SDK.
Unlike the JavaScript SDK, the React SDK does not require a context object for initialization. If you do not specify one, the React SDK uses an anonymous context by default.
Roku
Expand Roku code sample
You can create a context object with:
context = LaunchDarklyCreateContext({"key": "user-key-123abc", "kind": "user"})
The key should uniquely identify each context. 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. In this example, the hash is "user-key-123abc"
.
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 Targeting 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.
Server-side SDKs
Here are the configuration options for context and user entities in server-side SDKs:
- .NET (server-side)
- Apex
- C/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
In the .NET SDK, the Context
class has a New
method for creating a simple context, with a context kind of "user" and with only a key. It has a Builder
method for building a context with other properties.
Here's an example:
LDContext context = Context.Builder("context-key-123abc").Set("firstName", "Sandy").Set("lastName", "Smith").Set("email", "sandy@example.com").Set("groups", LdValue.ArrayOf(LdValue.Of("Google"), LdValue.Of("Microsoft"))).Build();
The argument to Builder
is the context's key. The key should uniquely identify each context. 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. In this example, the hash is context-key-123abc
.
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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
var context2 = Context.New(ContextKind.Of("organization"), "org-key-123abc");
Here's how to construct a multi-context, which includes multiple context kinds:
var userContext = Context.New("context-key-123abc");var deviceContext = Context.Builder("device-key-123abc").Kind("device").Build();var multiContext = Context.NewMulti(userContext, deviceContext);
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.
Apex
Expand Apex code sample
Here's an example of a user:
LDUser user = new LDUser.Builder('user-key-123abc').setFirstName('Sandy').setLastName('Smith').setEmail('sandy@example.com').setCustom(new LDValueObject.Builder().set('groups', new LDValueArray.Builder().add(LDValue.of('Google')).add(LDValue.of('Microsoft')).build()).build()).build();
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 "user-key-123abc"
.
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.
C/C++ (server-side)
Expand C/C++ (server-side) code sample
Here's an example of a user:
struct LDUser *user = LDUserNew("user-key-123abc");LDUserSetFirstName(user, "Sandy");LDUserSetLastName(user, "Smith");LDUserSetEmail(user, "sandy@example.com");struct LDJSON *tmp;struct LDJSON *custom = LDNewObject();struct LDJSON *groups = LDNewArray();tmp = LDNewText("Google");LDArrayPush(groups, tmp);tmp = LDNewText("Microsoft");LDArrayPush(groups, tmp);LDObjectSetKey(custom, "groups", groups);LDUserSetCustom(user, custom);
The argument to LDUserNew
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 "user-key-123abc"
.
LDUserFree(user);
When you are done with an LDUser
ensure that you free the structure.
To learn how to configure private attributes in the C/C++ (server-side) SDK, read Private attributes.
To learn how to configure anonymous users in the C/C++ (server-side) SDK, read Anonymous contexts and users.
Erlang
Expand Erlang code sample
Here's an example of a context:
Context = ldclient_context:set(<<"name">>, <<"Sandy Smith">>,ldclient_context:set(<<"email">>, <<"sandy@example.com">>,ldclient_context:set(<<"group">>, [<<"microsoft">>, <<"google">>],ldclient_context:new(<<"user-key-abc123">>, <<"user">>)))),
The key
property is the context's key. The key is the only mandatory context attribute. The combination of key and kind should uniquely identify each context. 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. In this example, the hash is "user-key-123abc"
.
You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is "user."
Here's how to construct a basic context with a context kind of something other than "user":
Context = ldclient_context:new(<<"org-key-123abc">>, <<"organization">>)%% Or as a mapContext = #{kind => <<"organization">>, key => <<"org-key-123abc">>}
Here's how to construct a multi-context, which includes multiple context kinds:
Context = ldclient_context:new_multi_from([%% Using `new/1` creates a context with a kind of <<"user">>.ldclient_context:new(<<"user-key-123abc">>),%% Using `new/2` creates a context of the specified kind (<<"device">>).ldclient_context:new(<<"device-key-123abc">>, <<"device">>)]). %% kind = device
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.
Go
Expand Go code sample
The Go SDK defines a Context
struct and a Builder
.
Here's an example:
import ("github.com/launchdarkly/go-sdk-common/v3/ldcontext""github.com/launchdarkly/go-sdk-common/v3/ldvalue")// Context with only a key// by default, the context kind is "user"context1 := ldcontext.New("context-key-123abc")// Context with a key plus other attributescontext2 := ldcontext.NewBuilder("context-key-456def").Kind("organization").Name("Global Health Services").SetString("email", "info@globalhealthexample.com").SetValue("address", ldvalue.ObjectBuild().SetString("street", "123 Main Street").SetString("city", "Springfield")).SetValue("groups", ldvalue.ArrayOf(ldvalue.String("Google"), ldvalue.String("Microsoft"))).Build()
The most common attribute is the context's key. The key is the only mandatory context attribute. The key should uniquely identify each context. 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. In this case, the key and hash are "context-key-123abc"
and "context-key-456def"
.
To learn more about the available 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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
context1 := ldcontext.NewWithKind("organization", "org-key-123abc")
Here's how to construct a multi-context, which includes multiple context kinds:
multiContext := ldcontext.NewMulti(ldcontext.New("user-key-123abc"),ldcontext.NewWithKind("device", "device-key-123abc"))
Each individual context within a multi-context can have the same attributes as a basic context. 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:
multiContext := ldcontext.NewMulti(ldcontext.NewBuilder("user-key-123abc").Name("Sandy").Build(),ldcontext.NewBuilder("device-key-123abc").Kind("device").Name("iPad").Build(),)
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.
Haskell
Expand Haskell code sample
Here's an example of a context:
{-# LANGUAGE OverloadedStrings #-}import LaunchDarkly.Server.Contextimport Data.Function ((&))-- Context with key and kindcontext1 :: Contextcontext1 = makeContext "context-key-123abc" "user"-- Context with a key plus other attributescontext2 :: Contextcontext2 = makeContext "context-key-456def" "organization"& withAttribute "name" "Global Health Services"& withAttribute "email" "info@globalhealthexample.com"& withAttribute "address" $ Object $ fromList [("street", "123 Main St"), ("city", "Springfield")]
The argument to makeUser
is the context's key. The key is the only mandatory attribute. The combination of the key and kind should uniquely identify each context. 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. In this example, the hash is "user-key-123abc"
.
Here's how to construct a basic context with a context kind of something other than "user":
makeContext "context-key-123abc" "organization"
Here's how to construct a multi-context, which includes multiple context kinds:
makeMultiContext [ makeContext "user-key-123abc" "user", makeContext "device-key-123abc" "device"]
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.
Java
Expand Java code sample
In the Java SDK, use a builder pattern to construct contexts.
Here's an example:
LDContext context = LDContext.builder("context-key-123abc").set("firstName", "Sandy").set("lastName", "Smith").set("email", "sandy@example.com").set("groups",LDValue.buildArray().add("Google").add("Microsoft").build()).build();
The argument to Builder
is the context's key. The key should uniquely identify each context. 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. In this example, the hash is "context-key-123abc"
.
Our Javadoc 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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
LDContext context1 = LDContext.create(ContextKind.of("organization"), "org-key-123abc");
Here's how to construct a multi-context, which includes multiple context kinds:
LDContext multiContext = LDContext.createMulti(LDContext.create("user-key-123abc"),LDContext.create(ContextKind.of("device"), "device-key-123abc"));
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.
Lua
Expand Lua code sample
Here's an example of a user:
local user = ld.makeUser({key = "user-key-123abc",firstName = "Sandy",lastName = "Smith",email = "sandy@example.com",custom = {groups = { "Google", "Microsoft" }}})
The argument to ld.makeUser
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 "user-key-123abc"
.
To learn more about configuration options, read the API docs.
To learn how to configure private attributes in the Lua SDK, read Private attributes.
To learn how to configure anonymous users in the Lua SDK, read Anonymous contexts and users.
Node.js (server-side)
Expand Node.js (server-side) code sample
In the Node.js SDK, contexts are JSON objects.
Here's an example:
const context = {kind: 'user',key: 'user-key-123abc',firstName: 'Sandy',lastName: 'Smith',email: 'sandy@example.com',groups: ['Google', 'Microsoft'],};
The key
property is the context key. The key should uniquely identify each context. 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. In this example, the hash is "user-key-123abc"
.
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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
const context = {kind: 'device',key: 'device-key-123abc'}
Here's how to construct a multi-context, which includes multiple context kinds:
const context = {kind: 'multi',user: { key: 'user-key-123abc' },device: { key: 'device-key-123abc' }}
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.
PHP
Expand PHP code sample
In the PHP SDK, use a builder pattern to construct contexts.
Here's an example:
$context = LDContext::builder("context-key-123abc")->set("firstName", "Sandy")->set("lastName", "Smith")->set("email", "sandy@example.com")->set("groups", ["Google", "Microsoft"])->build();
The first argument to LDContextBuilder
is the context's key. The key should uniquely identify each context. 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. In this example, the hash is "context-key-123abc"
.
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.
Here's how to construct a basic context with a context kind of something other than "user":
$context = LDContext::create("context-key-123abc", "organization");
Here's how to construct a multi-context, which includes multiple context kinds:
$deviceContext = LDContext::create("device-key-123abc", "device");$orgContext = LDContext::create("org-key-123abc", "org");$multiContext = LDContext::createMulti($deviceContext, $orgContext);
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.
Python
Expand Python code sample
In version 8.0 and higher of the Python SDK, the Context
class has a create
method for creating a simple context, with a context kind of "user" and with only a key. It has a builder
method for building a context with other properties.
Here's an example:
context = Context.builder("context-key-123abc") \.set("firstName", "Sandy") \.set("lastName", "Smith") \.set("email", "sandy@example.com") \.set("groups", ["Google", "Microsoft"]) \.build()
The argument to Context.builder
is the context's key. The key should uniquely identify each context. 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. In this example, the hash is "context-key-123abc"
.
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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
context1 = Context.create("org-key-123abc", "organization")
Here's how to construct a multi-context, which includes multiple context kinds:
multi_context = Context.create_multi(Context.create("user-key-123abc"),Context.create("device-key-123abc", "device"))
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.
Ruby
Expand Ruby code sample
In the Ruby SDK, contexts are instances of LaunchDarkly::LDContext
. Legacy users can continue to be provided as simple hashes.
Here's an example:
context = LaunchDarkly::LDContext.create({key: "user-key-123abc",kind: "user",firstName: "Sandy",lastName: "Smith",email: "sandy@example.com",groups: ["Google", "Microsoft"]})
The key
property is the context's key. The key should uniquely identify each context. 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. In this example, the hash is "user-key-123abc"
.
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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
context = LaunchDarkly::LDContext.with_key("context-key-123abc", "organization")
Here's how to construct a multi-context, which includes multiple context kinds:
multi_context = LaunchDarkly::LDContext.create_multi([LaunchDarkly::LDContext.with_key("user-key-123abc"),LaunchDarkly::LDContext.with_key("device-key-123abc", "device"),])
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.
Rust
Expand Rust code sample
The Rust SDK defines a Context
struct and a ContextBuilder
.
Here's an example:
// Context with only a keylet context = ContextBuilder::new("context-key-123abc").build()?;// Context with a key plus other attributeslet custom = hashmap! {"groups".into() => vec!["Google", "Microsoft"].into(),};let context = ContextBuilder::new("context-key-123abc").set_value("first_name", "Sandy".into()).set_value("last_name", "Smith".into()).set_value("email", "sandy@example.com".into()).set_value("Google", "groups".into()).set_value("Microsoft", "groups".into()).build();
The most common attribute is the context's key. The key is the only mandatory context attribute. The combination of the key and kind should also uniquely identify each context. For the context 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. In this example, the hash is "context-key-123abc"
.
You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is "user."
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 Targeting with flags.
Here's how to construct a basic context with a context kind of something other than "user":
let context = ContextBuilder::new("context-key-123abc").kind("organization").build()?;
Here's how to construct a multi-context, which includes multiple context kinds:
let user_context = ContextBuilder::new("user-key-123abc").build()?;client.identify(user_context.clone());let device_context = ContextBuilder::new("device-key-123abc").kind("device").build()?;client.identify(device_context.clone());let multi_context = MultiContextBuilder::new().add_context(user_context).add_context(device_context).build()?;client.identify(multi_context);
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.
Edge SDKs
Here are the configuration options for context and user entities in edge SDKs.
Cloudflare
Expand Cloudflare code sample
To configure contexts, the Cloudflare SDK uses the same code as the Node.js server-side SDK.
The Cloudflare SDK does not support sending events, so private attributes are not supported.
Vercel
Expand Vercel code sample
To configure contexts, the Vercel SDK uses the same code as the Node.js server-side SDK.
The Vercel SDK does not support sending events, so private attributes are not supported.