Skip to main content

Documentation Index

Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

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:
      Context context = Context.Builder("example-context-key")
          .Set("firstName", "Sandy")
          .Set("lastName", "Smith")
          .Set("email", "sandy@example.com")
          .Set("group", "microsoft")
          .Build();
Here’s how to construct a context with a context kind of something other than “user”:
      var context = Context.New(ContextKind.Of("organization"), "example-organization-key");
Here’s how to construct a multi-context, which includes multiple context kinds:
      var userContext = Context.New("example-user-key");

      var orgContext = Context.New(ContextKind.Of("organization"), "example-organization-key");

      var multiContext = Context.NewMulti(userContext, orgContext);
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:
      LDContext context = LDContext.builder("example-context-key")
          .set("email", "sandy@example.com")
          .set("firstName", "Sandy")
          .set("lastName", "Smith")
          .set("group", "Global Health Services")
          .build();
Here’s how to construct a context with a context kind of something other than “user”:
      LDContext context1 = LDContext.create(ContextKind.of("organization"), "example-organization-key");
Here’s how to construct a multi-context, which includes multiple context kinds:
      LDContext userContext = LDContext.create("example-user-key");
      LDContext deviceContext = LDContext.create(ContextKind.of("device"), "example-device-key");

      LDContext multiContext = LDContext.createMulti(
          userContext,
          deviceContext
      );
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.
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](/sdk/client-side/android/migration-3-to-
  1. 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:
      auto context = ContextBuilder()
        .Kind("user", "example-user-key")
        .Set("firstName", "Sandy")
        .Set("lastName", "Smith")
        .Set("groups", {"Acme", "Global Health Services"})
        .Build();
Here’s how to construct a context with a context kind of something other than “user”:
      auto context = ContextBuilder()
        .Kind("organization", "example-organization-key")
        .Build();
Here’s how to construct a multi-context, which includes multiple context kinds:
      auto context = ContextBuilder()
        .Kind("user", "example-user-key")
        .Name("Sandy")
        .Kind("organization", "example-organization-key")
        .Name("Global Health Services")
        .Build();
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

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:
      const user = {
        key: 'example-user-key',
        firstName: 'Sandy',
        lastName: 'Smith',
        email: 'sandy@example.com',
        custom: {
          groups: ['Acme', 'Global Health Services']
        }
      };
The key property is the user’s key. The key should uniquely identify each user. You can use a primary key or a hash, as long as the same user always has the same key. We recommend using a hash if possible. In this example, the hash is "example-user-key".
By default, when the SDK requests feature flags from LaunchDarkly, it makes an HTTP GET request with the user properties encoded in the URL. If you do not want user keys or other properties to be in request URLs, enable the useReport option in your client configuration. The SDK sends user data in the body of an HTTP REPORT request instead.
Most of the built-in attributes, like names and email addresses, expect string values. Custom attribute values can be booleans, numbers, strings, or arrays. If you enter a custom value on the Users list that looks like a number or a boolean, the SDK interprets it that way.
To learn how to configure private attributes in the Electron SDK, read Private attributes.To learn how to configure anonymous users in the Electron SDK, read Anonymous contexts and users.

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:
      final context = LDContextBuilder()
        .kind('user', 'example-user-key')
        .setString('email', 'sandy@example.com')
        .setString('firstName', 'Sandy')
        .setString('lastName', 'Smith')
        .setString('group', 'microsoft')
        .build();
Here’s how to construct a context with a context kind of something other than “user”:
      final context = LDContextBuilder()
        .kind('device', 'example-device-key')
        .build();
Here’s how to construct a multi-context, which includes multiple context kinds:
      LDContextBuilder builder = LDContextBuilder();
      builder.kind('user', 'example-user-key')
        .name('Sandy');
      builder.kind('organization', 'example-organization-key')
        .name('Global Health Services');
      LDContext context = builder.build();
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:
      var contextBuilder = LDContextBuilder(key: "example-user-key")
      contextBuilder.trySetValue("name", .string("Sandy"))
      contextBuilder.trySetValue("email", .string("sandy@example.com"))

      let context = try? contextBuilder.build().get()
Here’s how to construct a context with a context kind of something other than “user”:
      var contextBuilder = LDContextBuilder(key: "example-organization-key")
      contextBuilder.kind("organization")

      let context = try? contextBuilder.build().get()
Here’s how to construct a multi-context, which includes multiple context kinds:
      var userBuilder = LDContextBuilder(key: "example-user-key")
      var deviceBuilder = LDContextBuilder(key: "example-device-key")
      deviceBuilder.kind("device")

      var multiBuilder = LDMultiContextBuilder()
      multiBuilder.addContext(try userBuilder.build().get())
      multiBuilder.addContext(try deviceBuilder.build().get())

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

Java

Script
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:
      const context = {
        kind: 'user',
        key: 'example-user-key',
        firstName: 'Sandy',
        lastName: 'Smith',
        email: 'sandy@example.com',
        groups: ['Acme', 'Global Health Services']
      };
Here’s how to construct a context with a context kind of something other than “user”:
      const context = {
        kind: 'organization',
        key: 'example-organization-key'
      };
      const client = LDClient.initialize('example-client-side-id', context);

      try {
        await client.waitForInitialization(5);
        proceedWithSuccessfullyInitializedClient();
      } catch(err) {
        // Client failed to initialized or timed out
        // variation() calls return fallback values until initialization completes
      }
Here’s how to construct a multi-context, which includes multiple context kinds:
      const deviceContext = {
        kind: 'device',
        type: 'iPad',
        key: 'example-device-key'
      }

      const userContext = {
        kind: 'user',
        key: 'example-user-key',
        name: 'Sandy',
        role: 'doctor'
      }

      const multiContext = {
        kind: 'multi',
        user: userContext,
        device: deviceContext
      }

      const client = LDClient.initialize('example-client-side-id', multiContext)

      try {
        await client.waitForInitialization(5);
        proceedWithSuccessfullyInitializedClient();
      } catch(err) {
        // Client failed to initialized or timed out
        // variation() calls return fallback values until initialization completes
      }
The optional name and kind attributes expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attributes can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
We recommend against using personally identifiable information (PII) in context keys. If the key attribute you rely on in your context JSON does contain PII, you should enable the useReport option by sending the evaluation context as a JSON base64 URL-encoded path parameter. When you enable useReport, the SDK fetches flag settings by sending the context JSON in the body of a REPORT request instead, hiding that information from request logs.
To learn how to configure private attributes in the JavaScript SDK, read Private attributes.To learn how to configure anonymous contexts in the JavaScript SDK, read Anonymous contexts and users.

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:
      const context = {
        kind: 'user',
        key: 'example-user-key',
        firstName: 'Sandy',
        lastName: 'Smith',
        email: 'sandy@example.com',
        groups: ['Acme', 'Global Health Services']
      };
Here’s how to construct a context with a context kind of something other than “user”:
      const context = {
        kind: 'organization',
        key: 'example-organization-key'
      };
Here’s how to construct a multi-context, which includes multiple context kinds:
      const deviceContext = {
        kind: 'device',
        type: 'iPad',
        key: 'example-device-key'
      }

      const userContext = {
        kind: 'user',
        key: 'example-user-key',
        name: 'Sandy',
        role: 'doctor'
      }

      const multiContext = {
        kind: 'multi',
        user: userContext,
        device: deviceContext
      }
The kind and name attributes expect string values. Other attribute values can be booleans, numbers, strings, arrays, or JSON objects.If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.
By default, when the SDK requests feature flags from LaunchDarkly, it makes an HTTP GET request with the user properties encoded in the URL. If you do not want keys or other properties to be in request URLs, enable the useReport option in your client configuration. The SDK sends data in the body of an HTTP REPORT request instead.
To learn how to configure private attributes in the Node.js (client-side) SDK, read Private attributes.To learn how to configure anonymous contexts in the Node.js (client-side) SDK, read Anonymous contexts and users.

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:
      import { type LDContext } '@launchdarkly/react-native-client-sdk';

      // key and kind are the only required attributes

      let context: LDContext = {
        key: 'example-user-key',
        kind: 'user',
        firstName: 'Sandy',
        lastName: 'Smith',
        email: 'sandy@example.com',
        address: {
          street: '123 Main St',
          city: 'Springfield'
        }
      };
Here’s how to construct a context with a context kind of something other than “user”:
      const context = {
        kind: 'organization',
        key: 'example-organization-key'
      };
Here’s how to construct a multi-context, which includes multiple context kinds:
      const deviceContext = {
        kind: 'device',
        key: 'example-device-key'
      };

      const userContext = {
        kind: 'user',
        key: 'example-user-key',
        name: 'Sandy',
        role: 'doctor'
      };

      const multiContext = {
        kind: 'multi',
        user: userContext,
        device: deviceContext
      }
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:
      context = LaunchDarklyCreateContext({"key": "example-user-key", "kind": "user"})
Here’s how to construct a context with a context kind of something other than “user”:
      context = LaunchDarklyCreateContext({"key": "example-organization-key", "kind": "organization"})
Here’s how to construct a multi-context, which includes multiple context kinds:
      context = LaunchDarklyCreateContext({
          "kind": "multi",
          "user": { "key": "example-user-key", "name": "Sandy" },
          "org": { "key": "org-key-789xyz", "name": "LaunchDarkly" }
      })
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.