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.

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:
      LDContext context = Context.Builder("example-context-key")
          .Set("firstName", "Sandy")
          .Set("lastName", "Smith")
          .Set("email", "sandy@example.com")
          .Set("groups", LdValue.ArrayOf(LdValue.Of("Acme"), LdValue.Of("Global Health Services")))
          .Build();
Here’s how to construct a context with a context kind of something other than “user”:
      var context2 = 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-context-key");

      var deviceContext = Context.Builder("example-device-key")
          .Kind("device")
          .Build();

      var multiContext = Context.NewMulti(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 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

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:
      LDUser user = new LDUser.Builder('example-user-key')
          .setFirstName('Sandy')
          .setLastName('Smith')
          .setEmail('sandy@example.com')
          .setCustom(new LDValueObject.Builder()
              .set('groups', new LDValueArray.Builder()
                  .add(LDValue.of('Acme'))
                  .add(LDValue.of('Global Health Services'))
                  .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 "example-user-key".To learn how to configure private attributes in the Apex SDK, read Private attributes.To learn how to configure anonymous users in the Apex SDK, read Anonymous contexts and users.

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:
      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 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();
If you are working in C, when you are done with the context ensure that you free the structure:
      LDContext_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:
          Context = ldclient_context:set(<<"name">>, <<"Sandy Smith">>,
              ldclient_context:set(<<"email">>, <<"sandy@example.com">>,
              ldclient_context:set(<<"group">>, [<<"microsoft">>, <<"google">>],
              ldclient_context:new(<<"example-user-key">>, <<"user">>)))),
Here’s how to construct a context with a context kind of something other than “user”:
      Context = ldclient_context:new(<<"example-organization-key">>, <<"organization">>)
      %% Or as a map
      Context = #{kind => <<"organization">>, key => <<"example-organization-key">>}
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(<<"example-user-key">>),
        %% Using `new/2` creates a context of the specified kind (<<"device">>).
        ldclient_context:new(<<"example-device-key">>, <<"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

In the Go SDK, you have two options for how you want to manage contexts:
  • Create a context and pass it in when you initialize an LDScopedClient. This is a wrapper around LDClient that lets you specify the evaluation context to use for all operations. The scoped client’s context is a multi-context, and you can update the multi-context with additional associated contexts at any time.
  • Create a context and pass it in to each method call, such as when you evaluate a flag. To use this option, create a single, shared instance of LDClient.
LDScopedClient is in beta. It is still undergoing testing and active development. Its functionality may change without notice, including becoming backwards incompatible.
The Go SDK defines a Context struct and a Builder. The context key is the only mandatory context attribute. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.Here’s an example:
      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("example-context-key")

      // Context with a key plus other attributes
      context2 := 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("Acme"), ldvalue.String("Global Health Services"))).
          Build()
Here’s how to construct a context with a context kind of something other than “user”:
      context1 := ldcontext.NewWithKind("organization", "example-organization-key")
Here’s how to construct a multi-context, which includes multiple context kinds:
      multiContext := ldcontext.NewMulti(
          ldcontext.New("example-user-key"),
          ldcontext.NewWithKind("device", "example-device-key")
      )
You can also use the context builder to create each of the individual contexts:
      multiContext := ldcontext.NewMulti(
          ldcontext.NewBuilder("example-user-key").Name("Sandy").Build(),
          ldcontext.NewBuilder("example-device-key").Kind("device").Name("iPad").Build(),
      )
Each individual context within a multi-context can have the same attributes. The only restriction is that each context has to have a different context kind from the others within the multi-context.If you’re working with LDScopedClient, you don’t need to manually create the multi-context. Instead, you can add new contexts to the scoped client’s multi-context as they become available, or update existing contexts:
      userContext := ldcontext.New("example-user-key")

      scopedClient := ld.NewScopedClient(client, userContext)
      scopedClient.CurrentContext() // returns the single "user" context

      scopedClient.AddContext(ldcontext.NewWithKind("device", "example-device-key"))
      scopedClient.CurrentContext() // returns a multi-context with "user" and "device" contexts

      scopedClient.BoolVariation("example-flag-key", false) // evaluates the flag using a multi-context with "user" and "device" contexts
After you create a scoped client, we recommend adding it your Go context. Another advantage of using LDScopedClient is that you can pass the scoped client to any logic that already takes a Go context (context.Context), using utility methods provided in the SDK. This means the scoped client is implicitly passed around through all of your code that uses context.Context, and you can access the scoped client anywhere in your application logic. To learn more, read Use Go contexts with LDScopedClient.To learn more about the available LaunchDarkly context attributes, read Context and Builder.
The kind and name attributes expect string values. You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. These types are all represented by the ldvalue.Value type. The Go SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.To learn how to configure private attributes in the Go SDK, read Private attributes.To learn how to configure anonymous contexts in the Go SDK, read Anonymous contexts and users.

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:
      {-# LANGUAGE OverloadedStrings #-}

      import LaunchDarkly.Server.Context

      import Data.Function ((&))

      -- Context with key and kind
      context1 :: Context
      context1 = makeContext "example-context-key" "user"

      -- Context with a key plus other attributes
      context2 :: Context
      context2 = 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")]
Here’s how to construct a context with a context kind of something other than “user”:
      makeContext "example-context-key" "organization"
Here’s how to construct a multi-context, which includes multiple context kinds:
      makeMultiContext [ makeContext "example-user-key" "user"
                       , makeContext "example-device-key" "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

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:
      LDContext context = LDContext.builder("example-context-key")
        .set("firstName", "Sandy")
        .set("lastName", "Smith")
        .set("email", "sandy@example.com")
        .set("groups",
          LDValue.buildArray().add("Acme").add("Global Health Services").build())
        .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 multiContext = LDContext.createMulti(
          LDContext.create("example-user-key"),
          LDContext.create(ContextKind.of("device"), "example-device-key")
      );
The documentation for ContextBuilder shows you all the attributes that LaunchDarkly supports by default.
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or objects. If you pass a value that looks like a number or a boolean, the SDK interprets it that way. The Java SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.To learn how to configure private attributes in the Java SDK, read Private attributes.To learn how to configure anonymous contexts in the Java SDK, read Anonymous contexts and users.

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:
      -- using makeContext
      local user1 = ld.makeContext({
          user = {
              key = "example-user-key-1",
              attributes = {
                  firstName = "Sandy",
                  lastName  = "Smith",
                  email     = "sandy@example.com",
                  groups    = { "Acme", "Global Health Services" }
              }
          }
      })

      -- using makeUser, which is deprecated,
      -- to create an identical context (with unique key)
      local user2 = ld.makeUser({
          key       = "example-user-key-2",
          firstName = "Sandy",
          lastName  = "Smith",
          email     = "sandy@example.com",
          custom    = {
              groups = { "Acme", "Global Health Services" }
          }
      })

      -- using makeContext to create a different kind of context
      local orgContext = ld.makeContext({
          organization = {
            key = "example-organization-key",
            name = "Global Health Services"
          }
      })
Here’s how to construct a context with a context kind of something other than “user”:
      -- using makeContext to create a different kind of context
      local orgContext = ld.makeContext({
          organization = {
            key = "example-organization-key",
            name = "Global Health Services"
          }
      })
Here’s how to construct a multi-context, which includes multiple context kinds:
      -- using makeContext to create a multi-context
      local context = ld.makeContext({
          user = {
            key = "example-user-key"
          },
          org = {
            key = "example-organization-key"
          }
      })
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:
      const ld = require('@launchdarkly/node-server-sdk');

      const context: ld.LDContext = {
        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: 'device',
         key: 'example-device-key'
      }
Here’s how to construct a multi-context, which includes multiple context kinds:
      const context = {
        kind: 'multi',
        user: { key: 'example-user-key' },
        device: { key: 'example-device-key' }
      }
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:
      $context = LDContext::builder("example-context-key")
          ->set("firstName", "Sandy")
          ->set("lastName", "Smith")
          ->set("email", "sandy@example.com")
          ->set("groups", ["Acme", "Global Health Services"])
          ->build();
Here’s how to construct a context with a context kind of something other than “user”:
      $context = LDContext::create("example-context-key", "organization");
Here’s how to construct a multi-context, which includes multiple context kinds:
      $deviceContext = LDContext::create("example-device-key", "device");
      $orgContext = LDContext::create("example-organization-key", "org");
      $multiContext = LDContext::createMulti($deviceContext, $orgContext);
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:
      context = Context.builder("example-context-key") \
          .set("firstName", "Sandy") \
          .set("lastName", "Smith") \
          .set("email", "sandy@example.com") \
          .set("groups", ["Acme", "Global Health Services"]) \
          .build()
Here’s how to construct a context with a context kind of something other than “user”:
      context1 = Context.create("example-organization-key", "organization")
Here’s how to construct a multi-context, which includes multiple context kinds:
      multi_context = Context.create_multi(
          Context.create("example-user-key"),
          Context.create("example-device-key", "device")
      )
If you have many attributes to set, you can also create a context from a dictionary:
      pre_existing_dict = {
          'key': 'example-context-key',
          'kind': 'user',
          'firstName': 'Sandy',
          'lastName': 'Smith',
          'email': 'sandy@example.com',
          'groups': ['Acme', 'Global Health Services'],
      }

      context = Context.from_dict(pre_existing_dict)
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:
      context = LaunchDarkly::LDContext.create({
          key: "example-user-key",
          kind: "user",
          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”:
      context = LaunchDarkly::LDContext.with_key("example-context-key", "organization")
Here’s how to construct a multi-context, which includes multiple context kinds:
      multi_context = LaunchDarkly::LDContext.create_multi([
          LaunchDarkly::LDContext.with_key("example-user-key"),
          LaunchDarkly::LDContext.with_key("example-device-key", "device"),
      ])
All context attribute keys, for both built-in and custom attributes, must be symbols and not strings.
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user” and the hash is assumed to be in the legacy user format. Other attribute values can be booleans, numbers, strings, arrays, or objects. If you enter a custom value on the Contexts list that looks like a number or a boolean, the SDK interprets it that way.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.To learn how to configure private attributes in the Ruby SDK, read Private attributes.To learn how to configure anonymous contexts in the Ruby SDK, read Anonymous contexts and users.

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:
      // Context with only a key
      let context = ContextBuilder::new("example-context-key").build()?;

      // Context with a key plus other attributes
      let context = ContextBuilder::new("example-context-key")
          .set_value("first_name", "Sandy".into())
          .set_value("last_name", "Smith".into())
          .set_value("email", "sandy@example.com".into())
          .set_value("groups", vec!["Acme", "Global Health Services"].into())
          .build();
Here’s how to construct a context with a context kind of something other than “user”:
      let context = ContextBuilder::new("example-context-key")
          .kind("organization")
          .build()?;
Here’s how to construct a multi-context, which includes multiple context kinds:
      let user_context = ContextBuilder::new("example-user-key").build()?;
      client.identify(user_context.clone());

      let device_context = ContextBuilder::new("example-device-key").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 more about the available attributes, read Context and ContextBuilder.
The optional name and kind attributes, which you can set with .name() and .kind(), expect string values. Other attribute values can be any JSON type, including booleans, numbers, strings, arrays, or objects. These types are all represented by the AttributeValue type. The Rust SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.To learn how to configure private attributes in the Rust SDK, read Private attributes.To learn how to configure anonymous contexts in the Rust SDK, read Anonymous contexts and users.