Context configuration

Overview

This topic explains how to configure contexts in LaunchDarkly SDKs. This feature is available for all SDKs.

Newer versions of LaunchDarkly SDKs replace users with contexts

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

Configuration for 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 an AI config, the customer receives.

Here is an image of the Contexts list:

The "Contexts" list.

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.

Only the context attributes you provide are available for targeting

The SDK only evaluates flags or customizes AI 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.

About 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.

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:

  • the key of an existing context kind that is already defined
  • the key for a new context kind, which will be automatically created when this context is evaluated or identified
  • the string multi, if this is a multi-context
  • omitted, in which case, it defaults to user

The 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.

Custom and built-in attributes cannot share names

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.

Details about each SDK’s configuration are available in the SDK-specific sections below.

Client-side SDKs

Here are the configuration options for contexts in client-side SDKs:

.NET (client-side)

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:

1Context context = Context.Builder("context-key-123abc")
2 .Set("firstName", "Sandy")
3 .Set("lastName", "Smith")
4 .Set("email", "sandy@example.com")
5 .Set("group", "microsoft")
6 .Build();

Here’s how to construct a context with a context kind of something other than “user”:

.NET SDK v3.0+ (C#)
1var context = Context.New(ContextKind.Of("organization"), "org-key-123abc");

Here’s how to construct a multi-context, which includes multiple context kinds:

.NET SDK v3.0+ (C#)
1var userContext = Context.New("user-key-123abc");
2
3var orgContext = Context.New(ContextKind.Of("organization"), "org-key-123abc");
4
5var multiContext = Context.NewMulti(userContext, orgContext);
Interpreting custom attribute types

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.

Android

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:

1LDContext context = LDContext.builder("context-key-123abc")
2 .set("email", "sandy@example.com")
3 .set("firstName", "Sandy")
4 .set("lastName", "Smith")
5 .set("group", "Microsoft")
6 .build();

Here’s how to construct a context with a context kind of something other than “user”:

Android SDK v4.0+
1LDContext context1 = LDContext.create(ContextKind.of("organization"), "org-key-123abc");

Here’s how to construct a multi-context, which includes multiple context kinds:

Android SDK v4.0+
1LDContext multiContext = LDContext.createMulti(
2 LDContext.create("user-key-123abc"),
3 LDContext.create(ContextKind.of("device"), "device-key-123abc")
4);
Interpreting custom attribute types

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.

C++ (client-side)

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:

1auto context = ContextBuilder()
2 .Kind("user", "user-key-123abc")
3 .Set("firstName", "Sandy")
4 .Set("lastName", "Smith")
5 .Set("groups", {"Google", "Microsoft"})
6 .Build();

Here’s how to construct a context with a context kind of something other than “user”:

1auto context = ContextBuilder()
2 .Kind("organization", "org-key-123abc")
3 .Build();

Here’s how to construct a multi-context, which includes multiple context kinds:

C++ SDK v3.0 (native)
1auto context = ContextBuilder()
2 .Kind("user", "user-key-123abc")
3 .Name("Sandy")
4 .Kind("organization", "org-key-123abc")
5 .Name("Global Health Services")
6 .Build();
Interpreting custom attribute types

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.

Electron

Electron SDK does not support contexts

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:

1const user = {
2 key: 'user-key-123abc',
3 firstName: 'Sandy',
4 lastName: 'Smith',
5 email: 'sandy@example.com',
6 custom: {
7 groups: ['Google', 'Microsoft']
8 }
9};

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".

Personally-identifying user keys

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.

Interpreting custom attribute types

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

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:

1final context = LDContextBuilder()
2 .kind('user', 'user-key-123abc')
3 .setString('email', 'sandy@example.com')
4 .setString('firstName', 'Sandy')
5 .setString('lastName', 'Smith')
6 .setString('group', 'microsoft')
7 .build();

Here’s how to construct a context with a context kind of something other than “user”:

1final context = LDContextBuilder()
2 .kind('device', 'device-key-123abc')
3 .build();

Here’s how to construct a multi-context, which includes multiple context kinds:

1LDContextBuilder builder = LDContextBuilder();
2builder.kind('user', 'user-key-123abc')
3 .name('Sandy');
4builder.kind('organization', 'org-key-123abc')
5 .name('Global Health Services');
6LDContext context = builder.build();
Interpreting custom attribute types

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.

iOS

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:

1var contextBuilder = LDContextBuilder(key: "user-key-123abc")
2contextBuilder.trySetValue("name", .string("Sandy"))
3contextBuilder.trySetValue("email", .string("sandy@example.com"))
4
5let context = try? contextBuilder.build().get()

Here’s how to construct a context with a context kind of something other than “user”:

1var contextBuilder = LDContextBuilder(key: "org-key-123abc")
2contextBuilder.kind("organization")
3
4let context = try? contextBuilder.build().get()

Here’s how to construct a multi-context, which includes multiple context kinds:

1var userBuilder = LDContextBuilder(key: "user-key-123abc")
2var deviceBuilder = LDContextBuilder(key: "device-key-123abc")
3deviceBuilder.kind("device")
4
5var multiBuilder = LDMultiContextBuilder()
6multiBuilder.addContext(try userBuilder.build().get())
7multiBuilder.addContext(try deviceBuilder.build().get())
8
9let context = try multiBuilder.build().get()

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.

JavaScript

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:

1const context = {
2 kind: 'user',
3 key: 'user-key-123abc',
4 firstName: 'Sandy',
5 lastName: 'Smith',
6 email: 'sandy@example.com',
7 groups: ['Google', 'Microsoft']
8};

Here’s how to construct a context with a context kind of something other than “user”:

JavaScript SDK v3.0
1const context = {
2 kind: 'organization',
3 key: 'org-key-123abc'
4};
5const client = LDClient.initialize('client-side-id-123abc', context);

Here’s how to construct a multi-context, which includes multiple context kinds:

JavaScript SDK v3.0
1const deviceContext = {
2 kind: 'device',
3 type: 'iPad',
4 key: 'device-key-123abc'
5}
6
7const userContext = {
8 kind: 'user',
9 key: 'user-key-123abc',
10 name: 'Sandy',
11 role: 'doctor'
12}
13
14const multiContext = {
15 kind: 'multi',
16 user: userContext,
17 device: deviceContext
18}
19
20const client = LDClient.initialize('client-side-id-123abc', multiContext)
Interpreting custom attribute types

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.

Personally-identifying context keys

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.

Node.js (client-side)

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:

1const context = {
2 kind: 'user',
3 key: 'user-key-123abc',
4 firstName: 'Sandy',
5 lastName: 'Smith',
6 email: 'sandy@example.com',
7 groups: ['Google', 'Microsoft']
8};

Here’s how to construct a context with a context kind of something other than “user”:

Node.js SDK v3.0 (JavaScript)
1const context = {
2 kind: 'organization',
3 key: 'org-key-123abc'
4};

Here’s how to construct a multi-context, which includes multiple context kinds:

Node.js SDK v3.0 (JavaScript)
1const deviceContext = {
2 kind: 'device',
3 type: 'iPad',
4 key: 'device-key-123abc'
5}
6
7const userContext = {
8 kind: 'user',
9 key: 'user-key-123abc',
10 name: 'Sandy',
11 role: 'doctor'
12}
13
14const multiContext = {
15 kind: 'multi',
16 user: userContext,
17 device: deviceContext
18}

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.

Personally-identifying keys

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.

React Native

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:

1import { type LDContext } '@launchdarkly/react-native-client-sdk';
2
3// key and kind are the only required attributes
4
5let context: LDContext = {
6 key: 'user-key-123abc',
7 kind: 'user',
8 firstName: 'Sandy',
9 lastName: 'Smith',
10 email: 'sandy@example.com',
11 address: {
12 street: '123 Main St',
13 city: 'Springfield'
14 }
15};

Here’s how to construct a context with a context kind of something other than “user”:

1const context = {
2 kind: 'organization',
3 key: 'org-key-123abc'
4};

Here’s how to construct a multi-context, which includes multiple context kinds:

1const deviceContext = {
2 kind: 'device',
3 key: 'device-key-123abc'
4};
5
6const userContext = {
7 kind: 'user',
8 key: 'user-key-123abc',
9 name: 'Sandy',
10 role: 'doctor'
11};
12
13const multiContext = {
14 kind: 'multi',
15 user: userContext,
16 device: deviceContext
17}

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.

React Web

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.

Roku

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:

1context = LaunchDarklyCreateContext({"key": "user-key-123abc", "kind": "user"})

Here’s how to construct a context with a context kind of something other than “user”:

Roku SDK v2.0 (BrightScript)
1context = LaunchDarklyCreateContext({"key": "org-key-123abc", "kind": "organization"})

Here’s how to construct a multi-context, which includes multiple context kinds:

Roku SDK v2.0 (BrightScript)
1context = LaunchDarklyCreateContext({
2 "kind": "multi",
3 "user": { "key": "user-key-123abc", "name": "Sandy" },
4 "org": { "key": "org-key-789xyz", "name": "LaunchDarkly" }
5})
Interpreting custom attribute types

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.

Server-side SDKs

Here are the configuration options for contexts in server-side SDKs:

.NET (server-side)

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:

1LDContext context = Context.Builder("context-key-123abc")
2 .Set("firstName", "Sandy")
3 .Set("lastName", "Smith")
4 .Set("email", "sandy@example.com")
5 .Set("groups", LdValue.ArrayOf(LdValue.Of("Google"), LdValue.Of("Microsoft")))
6 .Build();

Here’s how to construct a context with a context kind of something other than “user”:

.NET SDK v7.0 (C#)
1var context2 = Context.New(ContextKind.Of("organization"), "org-key-123abc");

Here’s how to construct a multi-context, which includes multiple context kinds:

.NET SDK v7.0 (C#)
1var userContext = Context.New("context-key-123abc");
2
3var deviceContext = Context.Builder("device-key-123abc")
4 .Kind("device")
5 .Build();
6
7var multiContext = Context.NewMulti(userContext, deviceContext);
Interpreting custom attribute types

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.

Apex

Apex SDK does not support contexts

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:

Java
1LDUser user = new LDUser.Builder('user-key-123abc')
2 .setFirstName('Sandy')
3 .setLastName('Smith')
4 .setEmail('sandy@example.com')
5 .setCustom(new LDValueObject.Builder()
6 .set('groups', new LDValueArray.Builder()
7 .add(LDValue.of('Google'))
8 .add(LDValue.of('Microsoft'))
9 .build()
10 )
11 .build()
12 )
13 .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++ (server-side)

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:

1auto context = ContextBuilder()
2 .Kind("user", "user-key-123abc")
3 .Set("firstName", "Sandy")
4 .Set("lastName", "Smith")
5 .Set("groups", {"Google", "Microsoft"})
6 .Build();

Here’s how to construct a context with context kind of something other than “user”:

1auto context = ContextBuilder()
2 .Kind("organization", "org-key-123abc")
3 .Build();

Here’s how to construct a multi-context, which includes multiple context kinds:

1auto context = ContextBuilder()
2 .Kind("user", "user-key-123abc")
3 .Name("Sandy")
4 .Kind("organization", "org-key-123abc")
5 .Name("Global Health Services")
6 .Build();

If you are working in C, when you are done with the context ensure that you free the structure:

1LDContext_Free(context);

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.

Erlang

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:

1 Context = ldclient_context:set(<<"name">>, <<"Sandy Smith">>,
2 ldclient_context:set(<<"email">>, <<"sandy@example.com">>,
3 ldclient_context:set(<<"group">>, [<<"microsoft">>, <<"google">>],
4 ldclient_context:new(<<"user-key-abc123">>, <<"user">>)))),

Here’s how to construct a context with a context kind of something other than “user”:

Erlang SDK v2.0+
1Context = ldclient_context:new(<<"org-key-123abc">>, <<"organization">>)
2%% Or as a map
3Context = #{kind => <<"organization">>, key => <<"org-key-123abc">>}

Here’s how to construct a multi-context, which includes multiple context kinds:

Erlang SDK v2.0+
1Context = ldclient_context:new_multi_from([
2 %% Using `new/1` creates a context with a kind of <<"user">>.
3 ldclient_context:new(<<"user-key-123abc">>),
4 %% Using `new/2` creates a context of the specified kind (<<"device">>).
5 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

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:

1import (
2 "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
3 "github.com/launchdarkly/go-sdk-common/v3/ldvalue"
4)
5
6// Context with only a key
7// by default, the context kind is "user"
8context1 := ldcontext.New("context-key-123abc")
9
10// Context with a key plus other attributes
11context2 := ldcontext.NewBuilder("context-key-456def").
12 Kind("organization").
13 Name("Global Health Services").
14 SetString("email", "info@globalhealthexample.com").
15 SetValue("address", ldvalue.ObjectBuild().
16 SetString("street", "123 Main Street").
17 SetString("city", "Springfield")).
18 SetValue("groups", ldvalue.ArrayOf(
19 ldvalue.String("Google"), ldvalue.String("Microsoft"))).
20 Build()

Here’s how to construct a context with a context kind of something other than “user”:

Go SDK v6.0
1context1 := ldcontext.NewWithKind("organization", "org-key-123abc")

Here’s how to construct a multi-context, which includes multiple context kinds:

Go SDK v6.0
1multiContext := ldcontext.NewMulti(
2 ldcontext.New("user-key-123abc"),
3 ldcontext.NewWithKind("device", "device-key-123abc")
4)

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:

Go SDK v6.0
1multiContext := ldcontext.NewMulti(
2 ldcontext.NewBuilder("user-key-123abc").Name("Sandy").Build(),
3 ldcontext.NewBuilder("device-key-123abc").Kind("device").Name("iPad").Build(),
4)

To learn more about the available attributes, read Context and Builder.

Interpreting attribute types

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.

Haskell

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:

1{-# LANGUAGE OverloadedStrings #-}
2
3import LaunchDarkly.Server.Context
4
5import Data.Function ((&))
6
7-- Context with key and kind
8context1 :: Context
9context1 = makeContext "context-key-123abc" "user"
10
11-- Context with a key plus other attributes
12context2 :: Context
13context2 = makeContext "context-key-456def" "organization"
14 & withAttribute "name" "Global Health Services"
15 & withAttribute "email" "info@globalhealthexample.com"
16 & withAttribute "address" $ Object $ fromList [("street", "123 Main St"), ("city", "Springfield")]

Here’s how to construct a context with a context kind of something other than “user”:

Haskell SDK v4.0
1makeContext "context-key-123abc" "organization"

Here’s how to construct a multi-context, which includes multiple context kinds:

Haskell SDK v4.0
1makeMultiContext [ makeContext "user-key-123abc" "user"
2 , makeContext "device-key-123abc" "device"
3 ]

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

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:

1LDContext context = LDContext.builder("context-key-123abc")
2 .set("firstName", "Sandy")
3 .set("lastName", "Smith")
4 .set("email", "sandy@example.com")
5 .set("groups",
6 LDValue.buildArray().add("Google").add("Microsoft").build())
7 .build();

Here’s how to construct a context with a context kind of something other than “user”:

Java SDK v6.0
1LDContext context1 = LDContext.create(ContextKind.of("organization"), "org-key-123abc");

Here’s how to construct a multi-context, which includes multiple context kinds:

Java SDK v6.0
1LDContext multiContext = LDContext.createMulti(
2 LDContext.create("user-key-123abc"),
3 LDContext.create(ContextKind.of("device"), "device-key-123abc")
4);

The documentation for ContextBuilder shows you all the attributes that LaunchDarkly supports by default.

Interpreting custom attribute types

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.

Lua

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:

1-- using makeContext
2local user1 = ld.makeContext({
3 user = {
4 key = "user1-key-123abc",
5 attributes = {
6 firstName = "Sandy",
7 lastName = "Smith",
8 email = "sandy@example.com",
9 groups = { "Google", "Microsoft" }
10 }
11 }
12})
13
14-- using makeUser, which is deprecated,
15-- to create an identical context (with unique key)
16local user2 = ld.makeUser({
17 key = "user2-key-123abc",
18 firstName = "Sandy",
19 lastName = "Smith",
20 email = "sandy@example.com",
21 custom = {
22 groups = { "Google", "Microsoft" }
23 }
24})
25
26-- using makeContext to create a different kind of context
27local orgContext = ld.makeContext({
28 organization = {
29 key = "org-key-123abc",
30 name = "Global Health Services"
31 }
32})

Here’s how to construct a context with a context kind of something other than “user”:

Lua SDK v2
1-- using makeContext to create a different kind of context
2local orgContext = ld.makeContext({
3 organization = {
4 key = "org-key-123abc",
5 name = "Global Health Services"
6 }
7})

Here’s how to construct a multi-context, which includes multiple context kinds:

Lua SDK v2
1-- using makeContext to create a multi-context
2local context = ld.makeContext({
3 user = {
4 key = "user-key-123abc"
5 },
6 org = {
7 key = "org-key-123abc"
8 }
9})

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.

Node.js (server-side)

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:

1const ld = require('@launchdarkly/node-server-sdk');
2
3const context: ld.LDContext = {
4 kind: 'user',
5 key: 'user-key-123abc',
6 firstName: 'Sandy',
7 lastName: 'Smith',
8 email: 'sandy@example.com',
9 groups: ['Google', 'Microsoft'],
10};

Here’s how to construct a context with a context kind of something other than “user”:

1const context = {
2 kind: 'device',
3 key: 'device-key-123abc'
4}

Here’s how to construct a multi-context, which includes multiple context kinds:

1const context = {
2 kind: 'multi',
3 user: { key: 'user-key-123abc' },
4 device: { key: 'device-key-123abc' }
5}
Interpreting custom attribute types

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.

PHP

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:

1$context = LDContext::builder("context-key-123abc")
2 ->set("firstName", "Sandy")
3 ->set("lastName", "Smith")
4 ->set("email", "sandy@example.com")
5 ->set("groups", ["Google", "Microsoft"])
6 ->build();

Here’s how to construct a context with a context kind of something other than “user”:

PHP SDK v5.0
1$context = LDContext::create("context-key-123abc", "organization");

Here’s how to construct a multi-context, which includes multiple context kinds:

PHP SDK v5.0
1$deviceContext = LDContext::create("device-key-123abc", "device");
2$orgContext = LDContext::create("org-key-123abc", "org");
3$multiContext = LDContext::createMulti($deviceContext, $orgContext);
Interpreting custom attribute types

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.

Python

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:

1context = Context.builder("context-key-123abc") \
2 .set("firstName", "Sandy") \
3 .set("lastName", "Smith") \
4 .set("email", "sandy@example.com") \
5 .set("groups", ["Google", "Microsoft"]) \
6 .build()

Here’s how to construct a context with a context kind of something other than “user”:

Python SDK v8.0
1context1 = Context.create("org-key-123abc", "organization")

Here’s how to construct a multi-context, which includes multiple context kinds:

Python SDK v8.0
1multi_context = Context.create_multi(
2 Context.create("user-key-123abc"),
3 Context.create("device-key-123abc", "device")
4)

If you have many attributes to set, you can also create a context from a dictionary:

Python SDK v8.0+
1pre_existing_dict = {
2 'key': 'context-key-123abc',
3 'kind': 'user',
4 'firstName': 'Sandy',
5 'lastName': 'Smith',
6 'email': 'sandy@example.com',
7 'groups': ['Google', 'Microsoft'],
8}
9
10context = Context.from_dict(pre_existing_dict)
Interpreting custom attribute types

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.

Ruby

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:

1context = LaunchDarkly::LDContext.create({
2 key: "user-key-123abc",
3 kind: "user",
4 firstName: "Sandy",
5 lastName: "Smith",
6 email: "sandy@example.com",
7 groups: ["Google", "Microsoft"]
8})

Here’s how to construct a context with a context kind of something other than “user”:

Ruby SDK v7.0
1context = LaunchDarkly::LDContext.with_key("context-key-123abc", "organization")

Here’s how to construct a multi-context, which includes multiple context kinds:

Ruby SDK v7.0
1multi_context = LaunchDarkly::LDContext.create_multi([
2 LaunchDarkly::LDContext.with_key("user-key-123abc"),
3 LaunchDarkly::LDContext.with_key("device-key-123abc", "device"),
4])
Context attribute keys must be symbols

All context attribute keys, for both built-in and custom attributes, must be symbols and not strings.

Interpreting custom attribute types

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.

Rust

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:

1// Context with only a key
2let context = ContextBuilder::new("context-key-123abc").build()?;
3
4// Context with a key plus other attributes
5let custom = hashmap! {
6 "groups".into() => vec!["Google", "Microsoft"].into(),
7};
8let context = ContextBuilder::new("context-key-123abc")
9 .set_value("first_name", "Sandy".into())
10 .set_value("last_name", "Smith".into())
11 .set_value("email", "sandy@example.com".into())
12 .set_value("Google", "groups".into())
13 .set_value("Microsoft", "groups".into())
14 .build();

Here’s how to construct a context with a context kind of something other than “user”:

Rust SDK v1
1let context = ContextBuilder::new("context-key-123abc")
2 .kind("organization")
3 .build()?;

Here’s how to construct a multi-context, which includes multiple context kinds:

Rust SDK v1
1let user_context = ContextBuilder::new("user-key-123abc").build()?;
2client.identify(user_context.clone());
3
4let device_context = ContextBuilder::new("device-key-123abc").kind("device").build()?;
5client.identify(device_context.clone());
6
7let multi_context = MultiContextBuilder::new()
8.add_context(user_context)
9.add_context(device_context)
10.build()?;
11
12client.identify(multi_context);

To learn more about the available attributes, read Context and ContextBuilder.

Interpreting custom attribute types

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.

Edge SDKs

Here are the configuration options for contexts in edge SDKs.

Akamai

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.

Cloudflare

To configure contexts, the Cloudflare SDK uses the same code as the Node.js server-side SDK.

Vercel

To configure contexts, the Vercel SDK uses the same code as the Node.js server-side SDK.

AI SDKs

Here are the configuration options for contexts in AI SDKs:

.NET AI

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:

.NET AI SDK
1LDContext context = Context.Builder("context-key-123abc")
2 .Set("firstName", "Sandy")
3 .Set("lastName", "Smith")
4 .Set("email", "sandy@example.com")
5 .Set("groups", LdValue.ArrayOf(LdValue.Of("Google"), LdValue.Of("Microsoft")))
6 .Build();

Here’s how to construct a context with a context kind of something other than “user”:

.NET AI SDK
1var context2 = Context.New(ContextKind.Of("organization"), "org-key-123abc");

Here’s how to construct a multi-context, which includes multiple context kinds:

.NET AI SDK
1var userContext = Context.New("context-key-123abc");
2
3var deviceContext = Context.Builder("device-key-123abc")
4 .Kind("device")
5 .Build();
6
7var multiContext = Context.NewMulti(userContext, deviceContext);
Interpreting custom attribute types

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 AI 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 AI 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 AI config. To learn more, read Target with flags and Customizing AI 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.

Go AI

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:

Go AI SDK
1import (
2 "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
3 "github.com/launchdarkly/go-sdk-common/v3/ldvalue"
4)
5
6// Context with only a key
7// by default, the context kind is "user"
8context1 := ldcontext.New("context-key-123abc")
9
10// Context with a key plus other attributes
11context2 := ldcontext.NewBuilder("context-key-456def").
12 Kind("organization").
13 Name("Global Health Services").
14 SetString("email", "info@globalhealthexample.com").
15 SetValue("address", ldvalue.ObjectBuild().
16 SetString("street", "123 Main Street").
17 SetString("city", "Springfield")).
18 SetValue("groups", ldvalue.ArrayOf(
19 ldvalue.String("Google"), ldvalue.String("Microsoft"))).
20 Build()

Here’s how to construct a context with a context kind of something other than “user”:

Go AI SDK
1context1 := ldcontext.NewWithKind("organization", "org-key-123abc")

Here’s how to construct a multi-context, which includes multiple context kinds:

Go AI SDK
1multiContext := ldcontext.NewMulti(
2 ldcontext.New("user-key-123abc"),
3 ldcontext.NewWithKind("device", "device-key-123abc")
4)

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:

Go AI SDK
1multiContext := ldcontext.NewMulti(
2 ldcontext.NewBuilder("user-key-123abc").Name("Sandy").Build(),
3 ldcontext.NewBuilder("device-key-123abc").Kind("device").Name("iPad").Build(),
4)
Interpreting custom attribute types

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 AI 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 AI 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 AI config. To learn more, read Target with flags and Customizing AI 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.

Node.js (server-side) AI

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:

1const context: LDContext = {
2 kind: 'user',
3 key: 'user-key-123abc',
4 firstName: 'Sandy',
5 lastName: 'Smith',
6 email: 'sandy@example.com',
7 groups: ['Google', 'Microsoft'],
8};

Here’s how to construct a context with a context kind of something other than “user”:

Node.js (server-side) AI SDK (TypeScript)
1const context: LDContext = {
2 kind: 'device',
3 key: 'device-key-123abc'
4}

Here’s how to construct a multi-context, which includes multiple context kinds:

Node.js (server-side) AI SDK (TypeScript)
1const context: LDContext = {
2 kind: 'multi',
3 user: { key: 'user-key-123abc' },
4 device: { key: 'device-key-123abc' }
5}
Interpreting custom attribute types

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 AI 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 AI 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 AI config. To learn more, read Target with AI configs and Customizing AI 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.

Python AI

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:

Python AI SDK
1context = Context.builder("context-key-123abc") \
2 .set("firstName", "Sandy") \
3 .set("lastName", "Smith") \
4 .set("email", "sandy@example.com") \
5 .set("groups", ["Google", "Microsoft"]) \
6 .build()

Here’s how to construct a context with a context kind of something other than “user”:

Python AI SDK
1context1 = Context.create("org-key-123abc", "organization")

Here’s how to construct a multi-context, which includes multiple context kinds:

Python AI SDK
1multi_context = Context.create_multi(
2 Context.create("user-key-123abc"),
3 Context.create("device-key-123abc", "device")
4)

If you have many attributes to set, you can also create a context from a dictionary:

Python AI SDK
1pre_existing_dict = {
2 'key': 'context-key-123abc',
3 'kind': 'user',
4 'firstName': 'Sandy',
5 'lastName': 'Smith',
6 'email': 'sandy@example.com',
7 'groups': ['Google', 'Microsoft'],
8}
9
10context = Context.from_dict(pre_existing_dict)
Interpreting custom attribute types

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 AI 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 AI config message. Continuing the sam example, you could use {{LDCTX.address.city}} in your message, and the value of the “city” field will be substituted when you customize the AI config. To learn more, read Target with AI configs and Customizing AI 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.

Built with