> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Anonymous contexts

> This topic explains how to configure contexts as anonymous in LaunchDarkly SDKs. These features are available for all SDKs.

> For the complete documentation index, see [llms.txt](/llms.txt). For the full documentation snapshot, see [llms-full.txt](/llms-full.txt).

## Overview

This topic explains how to configure contexts as anonymous in LaunchDarkly SDKs. These features are available for all SDKs.

Each SDK lets you designate anonymous contexts. Anonymous contexts don't appear on your **Contexts** list, so you can't search for them, and you can't search for or autocomplete by their keys. If you use multi-contexts, you can choose to make only some contexts anonymous. To learn more, read [Multi-contexts](/home/flags/multi-contexts).

In some client-side SDKs, if you don't provide a key or set it to null, and set `anonymous` to `true`, then the SDK generates a random key for you. If you generate keys for anonymous contexts, session IDs or UUIDs work best.

To learn more, read [Anonymous contexts](/home/flags/anonymous-contexts).

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

* [Client-side SDKs](#client-side-sdks)
* [Server-side SDKs](#server-side-sdks)
* [Edge SDKs](#edge-sdks)
* [AI SDKs](#ai-sdks)

<Note title="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](/home/flags/contexts).
</Note>

## Client-side SDKs

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

* [.NET (client-side)](#net-client-side)
* [Android](#android)
* [C++ (client-side)](#c-client-side)
* [Electron](#electron)
* [Flutter](#flutter)
* [iOS](#ios)
* [JavaScript](#javascript)
* [Node.js (client-side)](#nodejs-client-side)
* [React Native](#react-native)
* [React Web](#javascript): The React Web SDK relies on the JavaScript SDK for context-related functionality.
* [Roku](#roku)

### .NET (client-side)

<Accordion title="Expand .NET (client-side) code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```csharp title=".NET SDK v3.0+ (C#)" theme={null}
          Context context = Context.Builder("example-context-key")
              .Anonymous(true)
              .Build();
    ```
  </CodeGroup>

  To auto-generate a key for any context whose `anonymous` attribute is true:

  <CodeGroup>
    ```csharp title=".NET SDK v4.0 (C#)" theme={null}
          var config = Configuration
              .Builder("example-mobile-key", ConfigurationBuilder.AutoEnvAttributes.Enabled)
              .GenerateAnonymousKeys(true)
              .Build();
    ```

    ```csharp title=".NET SDK v3.0 (C#)" theme={null}
          var config = Configuration.Builder("example-mobile-key")
              .GenerateAnonymousKeys(true)
              .Build();
    ```
  </CodeGroup>

  If you set this option, you must still specify a non-null key as a placeholder when you construct the `Context`, because the SDK does not allow a `Context` to exist with a null key. When you pass this context to SDK methods like `Init` or `Identify`, the SDK replaces the placeholder key with a generated key.

  In this example, the placeholder key is "placeholder-key", but it could be any non-empty string:

  <CodeGroup>
    ```csharp title=".NET SDK v3.0+ (C#)" theme={null}
          Context context = Context.Builder("placeholder-key")
              .Anonymous(true)
              .Build();
    ```
  </CodeGroup>
</Accordion>

### Android

<Accordion title="Expand Android code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```java title="Android SDK v4.0+ (Java)" theme={null}
          LDContext context = LDContext.builder("example-context-key")
              .anonymous(true)
              .build();
    ```

    ```kotlin title="Android SDK v4.0+ (Kotlin)" theme={null}
          val context: LDContext = LDContext.builder("example-context-key")
              .anonymous(true)
              .build()
    ```
  </CodeGroup>

  If you set this option, you must still specify a non-null key as a placeholder when you construct the `LDContext`, because this SDK does not allow a Context to exist with a null key. When you pass this context to SDK methods like `Init` or `Identify`, the SDK replaces the placeholder key with a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.
</Accordion>

### C++ (client-side)

<Accordion title="Expand C++ (client-side) code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```cpp title="C++ SDK v3.0 (native)" theme={null}
          auto context = ContextBuilder()
            .Kind("user", "example-user-key")
            .Anonymous(true)
            .Build();
    ```

    ```c title="C++ SDK v3.0 (C binding)" theme={null}
          LDContextBuilder builder = LDContextBuilder_New();

          LDContextBuilder_AddKind(builder, "user", "example-user-key");
          LDContextBuilder_Attributes_SetAnonymous(builder, "user", true);

          LDContext context = LDContextBuilder_Build(builder);
    ```

    ```c title="C SDK v2.x (native)" theme={null}
          /* anonymous user, with key specified */
          struct LDUser *user = LDUserNew("example-user-key");
          LDUserSetAnonymous(user, true);

          /*
            In v2.x, when you mark the context as anonymous, you can
            leave the key parameter null and the client will automatically
            set it to a LaunchDarkly-specific, device-unique string
            that is consistent between app restarts and device reboots.
          */
          struct LDUser *user = LDUserNew(NULL);
          LDUserSetAnonymous(user, LDBooleanTrue);
    ```

    ```cpp title="C SDK v2.x (C++ binding)" theme={null}
          /* anonymous user, with key specified */
          struct LDUser *user = LDUserNew("example-user-key");
          LDUserSetAnonymous(user, true);

          /*
            In v2.x, when you mark the context as anonymous, you can
            leave the key parameter null and the client will automatically
            set it to a LaunchDarkly-specific, device-unique string
            that is consistent between app restarts and device reboots.
          */
          LDUser *user = LDUserNew(nullptr);
          LDUserSetAnonymous(user, true);
    ```
  </CodeGroup>

  To learn more, read [`ContextBuilder`](https://launchdarkly.github.io/cpp-sdks/libs/client-sdk/docs/html/classlaunchdarkly_1_1ContextBuilder.html).
</Accordion>

### Electron

<Accordion title="Expand Electron code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```js title="JavaScript" theme={null}
          const anonymousUser = { key: 'example-user-key', anonymous: true };
    ```

    ```ts title="TypeScript" theme={null}
          import * as LDElectron from 'launchdarkly-electron-client-sdk';

          const anonymousUser: LDElectron.LDUser = { key: 'example-user-key', anonymous: true };
    ```
  </CodeGroup>

  To create an anonymous user with an auto-generated key, specify the "anonymous" property and omit the "key" property. The LaunchDarkly client creates a unique key for this user and caches it locally:

  <CodeGroup>
    ```js title="JavaScript" theme={null}
          const anonymousUser = { anonymous: true };
    ```

    ```ts title="TypeScript" theme={null}
          import * as LDElectron from 'launchdarkly-electron-client-sdk';

          const anonymousUser: LDElectron.LDUser = { anonymous: true };
    ```
  </CodeGroup>
</Accordion>

### Flutter

<Accordion title="Expand Flutter code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```dart title="Flutter SDK v4" theme={null}
          final context = LDContextBuilder()
            .kind('user', 'example-user-key')
            .anonymous(true)
            .build();
    ```

    ```dart title="Flutter SDK v2.x+" theme={null}
          LDContextBuilder builder = LDContextBuilder();
          builder.kind('user', 'example-user-key')
              .anonymous(true);
          LDContext context = builder.build();

    ```

    ```dart title="Flutter SDK v1.x" theme={null}
          LDUser user = LDUserBuilder('example-user-key')
              .anonymous(true)
              .build();
    ```
  </CodeGroup>

  To learn more, read [`anonymous`](https://pub.dev/documentation/launchdarkly_flutter_client_sdk/latest/launchdarkly_flutter_client_sdk/LDAttributesBuilder/anonymous.html).
</Accordion>

### iOS

<Accordion title="Expand iOS code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```swift title="iOS SDK v8.0+ (Swift)" theme={null}
          var contextBuilder = LDContextBuilder(key: "example-context-key")
          contextBuilder.anonymous(true)

          let context = contextBuilder.build().get()
    ```

    ```objectivec title="iOS SDK v8.0+ (Objective-C)" theme={null}
          LDContextBuilder *builder = [[LDContextBuilder alloc] initWithKey:@"example-context-key"];
          [builder trySetValueWithName:@"anonymous" value:[LDValue ofBool:true]];

          LDContext *context = builder.build.success;
    ```
  </CodeGroup>

  Alternatively, you can omit the key parameter. The client will automatically set the `isAnonymous` property for the context, and set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.

  Here's how:

  <CodeGroup>
    ```swift title="iOS SDK v8.0+ (Swift)" theme={null}
          // Have the SDK use a device persistent key.
          // This sets `isAnonymous` by default.
          let context = try LDContextBuilder().build().get()
    ```

    ```objectivec title="iOS SDK v8.0+ (Objective-C)" theme={null}
          // Have the SDK use a device persistent key.
          // This sets `isAnonymous` by default.
          LDContextBuilder *newThing = [[LDContextBuilder alloc] init];
    ```
  </CodeGroup>
</Accordion>

### Java

Script

<Accordion title="Expand JavaScript code sample">
  To create an anonymous context, specify the `anonymous` property and omit the `key` property. The client will automatically set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.

  Here's how:

  <CodeGroup>
    ```js title="JavaScript SDK v3.0" theme={null}
          const anonymousUserContext = {
            kind: 'user',
            anonymous: true
          };

          // A multi-context can contain both anonymous and non-anonymous contexts.
          // Here, the organization is not anonymous.
          const multiContext = {
            kind: 'multi',
            user: anonymousUserContext,
            org: {
              key: 'example-organization-key',
              name: 'Acme, Inc.'
            }
          }
    ```

    ```ts title="JavaScript SDK v3.0 (TypeScript)" theme={null}
          import * as ld from 'launchdarkly-js-client-sdk';

          const anonymousContext: ld.LDContext = {
            kind: 'user',
            anonymous: true
          };

          // A multi-context can contain both anonymous and non-anonymous contexts.
          // Here, the organization is not anonymous.
          const multiContext: ld.LDContext = {
            kind: 'multi',
            user: anonymousContext,
            org: {
              key: 'example-organization-key',
              name: 'Example organization name'
            }
          }
    ```

    ```js title="JavaScript SDK v2.x" theme={null}
          const anonymousUser = { anonymous: true };
    ```

    ```ts title="JavaScript SDK v2.x (TypeScript)" theme={null}
          import * as ld from 'launchdarkly-js-client-sdk';

          const anonymousUser: ld.LDUser = { anonymous: true };
    ```
  </CodeGroup>
</Accordion>

### Node.js (client-side)

<Accordion title="Expand Node.js (client-side) code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```js title="Node.js SDK v3.0 (JavaScript)" theme={null}
          const anonymousContext = { kind: 'user', key: 'example-user-key', anonymous: true };
    ```

    ```ts title="Node.js SDK v3.0 (TypeScript)" theme={null}
          import * as ld from 'launchdarkly-node-client-sdk';

          const anonymousContext: ld.LDContext = { kind: 'user', key: 'example-user-key', anonymous: true };
    ```
  </CodeGroup>

  You can also have the SDK generate the key for you. Specify the `anonymous` property and omit the `key` property. The client will automatically set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.

  Here's how:

  <CodeGroup>
    ```js title="Node.js SDK v3.0 (JavaScript)" theme={null}
          const anonymousContext = { kind: 'user', anonymous: true };
    ```

    ```ts title="Node.js SDK v3.0 (TypeScript)" theme={null}
          import * as ld from 'launchdarkly-node-client-sdk';

          const anonymousContext: ld.LDContext = { kind: 'user', anonymous: true };
    ```
  </CodeGroup>
</Accordion>

### React Native

<Accordion title="Expand React Native code sample">
  To create an create an anonymous context, specify the `anonymous` property and set the context `key` to an empty string.

  Here's how:

  <CodeGroup>
    ```js title="React Native SDK v10" theme={null}
          // This device context is anonymous
          const deviceContext = {
            // The key attribute is required and should be empty
            // The SDK will automatically generate a unique, stable key
            key: '',
            kind: 'device',
            deviceId: '12345',
            anonymous: true
          }

          // This user context is not anonymous
          const userContext = {
            kind: 'user',
            key: 'example-user-key'
          }

          // The multi-context contains one anonymous context
          // and one non-anonymous context
          const multiContext = {
            kind: 'multi',
            user: userContext,
            device: deviceContext
          }
    ```
  </CodeGroup>

  In version 10 of the SDK, you must include the `key` attribute when building the anonymous context. If you set the key to an empty string, the client will automatically set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots. We strongly recommend having the client manage the key for anonymous contexts. If you set the key to a non-empty string, the client uses that value as the key. However, the key may not be stable across restarts or reboots.

  The SDK gives a usage error if you omit the `key` attribute. It also gives a usage error if you set the key to an empty string and do not mark the context as anonymous.

  In versions 7 through 9 of the SDK, you may omit the context key when building an anonymous context, and the client will automatically set it to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.

  #### Using a shared key between anonymous contexts in React Native for Android

  It is possible to use one, shared key between anonymous contexts. However, we do not recommend this. Using a shared key between anonymous contexts means that some features will be limited or will not work as expected. To learn more, read [Use a shared key between anonymous contexts](/home/flags/anonymous-contexts#use-a-shared-key-between-anonymous-contexts).

  If you are using an older version of the React Native SDK on Android, there is some additional configuration required if you want to use a shared key between anonymous contexts.

  <Accordion title="Expand React Native v7.x-v9.x for Android code sample">
    If you do choose to use a shared key between anonymous contexts, and you:

    * have an Android application
    * are using the React Native SDK in versions 7.x through 9.x

    then you must also explicitly configure the SDK to allow using a shared key between anonymous contexts. The `generateAnonymousKeysAndroid` configuration option defaults to `true`, which means that the SDK will automatically generate unique keys for anonymous contexts. If you need to use a shared key between anonymous contexts, then you must set this option to `false`.

    To learn more, read [`generateAnonymousKeys`](https://launchdarkly.github.io/android-client-sdk/com/launchdarkly/sdk/android/LDConfig.Builder.html#generateAnonymousKeys\(boolean\)).

    The React Native SDK for iOS generates a context key for anonymous contexts only if you do not supply one. No additional SDK configuration is required if you are using a shared key for anonymous contexts with the React Native SDK for iOS.
  </Accordion>
</Accordion>

### React Web

All context-related functionality provided by the [JavaScript SDK](#javascript) is also available in the React Web SDK.

### Roku

<Accordion title="Expand Roku code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```brightscript title="Roku SDK v2.0 (BrightScript)" theme={null}
          context = LaunchDarklyCreateContext({"key": "example-user-key", "kind": "user", "anonymous": true})
    ```

    ```brightscript title="Roku SDK v1.x (BrightScript)" theme={null}
          user.setAnonymous(Boolean)
    ```
  </CodeGroup>
</Accordion>

## Server-side SDKs

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

* [.NET (server-side)](#net-server-side)
* [Apex](#apex)
* [C++ (server-side)](#c-server-side)
* [Erlang](#erlang)
* [Go](#go)
* [Haskell](#haskell)
* [Java](#java)
* [Lua](#lua)
* [Node.js (server-side)](#nodejs-server-side)
* [PHP](#php)
* [Python](#python)
* [Ruby](#ruby)
* [Rust](#rust)

### .NET (server-side)

<Accordion title="Expand .NET (server-side) code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```csharp title=".NET SDK v7.0 (C#)" theme={null}
          var context = Context.Builder("example-context-key")
              .Anonymous(true)
              .Build();
    ```
  </CodeGroup>
</Accordion>

### Apex

<Accordion title="Expand Apex code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```java title="Java" theme={null}
          LDUser user = new LDUser.Builder('example-user-key')
              .setAnonymous(true)
              .build();
    ```
  </CodeGroup>
</Accordion>

### C++ (server-side)

<Accordion title="Expand C++ (server-side) code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```c title="C++ SDK v3.0 (native)" theme={null}
          auto context = ContextBuilder()
            .Kind("user", "example-user-key")
            .Anonymous(true)
            .Build();
    ```

    ```c title="C++ SDK v3.0 (C binding)" theme={null}
          LDContextBuilder builder = LDContextBuilder_New();

          LDContextBuilder_AddKind(builder, "user", "example-user-key");
          LDContextBuilder_Attributes_SetAnonymous(builder, "user", true);

          LDContext context = LDContextBuilder_Build(builder);
    ```

    ```c title="C SDK v2.x (native)" theme={null}
          struct LDUser *user = LDUserNew("placeholder-key");
          LDUserSetAnonymous(user, LDBooleanTrue);
    ```

    ```cpp title="C SDK v2.x (C++ binding)" theme={null}
          struct LDUser *user = LDUserNew("placeholder-key");
          LDUserSetAnonymous(user, true);
    ```
  </CodeGroup>

  To learn more, read [`ContextBuilder`](https://launchdarkly.github.io/cpp-sdks/libs/server-sdk/docs/html/classlaunchdarkly_1_1ContextBuilder.html).
</Accordion>

### Erlang

<Accordion title="Expand Erlang code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```erlang title="Erlang SDK v2.0+" theme={null}
              Context = ldclient_context:set(anonymous, true,
                  ldclient_context:new(<<"example-user-key">>))
    ```

    ```erlang title="Erlang SDK v1.x" theme={null}
          Key = <<"example-user-key">>,
          Anonymous = true,
          Map = #{
              key => Key,
              anonymous => Anonymous,
          },
          User = ldclient_user:new_from_map(Map)
    ```
  </CodeGroup>
</Accordion>

### Go

<Accordion title="Expand Go code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```go title="Go SDK v6.0" theme={null}
          import (
              "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
          )

          // Anonymous context with only a key
          context1 := ldcontext.NewBuilder("example-context-key").Anonymous(true)

          // Anonymous context with a key plus other attributes
          context2 := ldcontext.NewBuilder("context-key-456def").
              Anonymous(true).
              SetString("country", "Canada").
              Build()
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related [events](/sdk/concepts/events#omit-anonymous-contexts-from-events) to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read [`EventsConfiguration`](https://pkg.go.dev/github.com/launchdarkly/go-sdk-events/v3#EventsConfiguration).
</Accordion>

### Haskell

<Accordion title="Expand Haskell code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```haskell title="Haskell SDK v4.0" theme={null}
          makeContext "example-user-key" "user"
            & withAnonymous True
    ```

    ```haskell title="Haskell SDK v3.x" theme={null}
          user` = userSetAnonymous True user
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related [events](/sdk/concepts/events#omit-anonymous-contexts-from-events) to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read [`configSetOmitAnonymousContexts`](https://launchdarkly.github.io/haskell-server-sdk/LaunchDarkly-Server-Config.html#v:configSetOmitAnonymousContexts).
</Accordion>

### Java

<Accordion title="Expand Java code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```java title="Java SDK v6.0" theme={null}
          LDContext context = LDContext.builder("example-context-key")
            .anonymous(true)
            .build();
    ```
  </CodeGroup>
</Accordion>

### Lua

<Accordion title="Expand Lua code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```lua title="Lua SDK v2" theme={null}
          -- to create an anonymous user context
          local userContext = ld.makeContext({
              user = {
                key       = "example-user-key",
                anonymous = true
              }
          })

          -- to create an anonymous context of a different kind
          local deviceContext = ld.makeContext({
              device    = {
                key       = "example-device-key",
                anonymous = true
              }
          })

    ```

    ```lua title="Lua SDK v1.x" theme={null}
          local user = ld.makeUser({
              key       = "example-user-key",
              anonymous = true
              }
          )
    ```
  </CodeGroup>

  To learn more, read [`makeContext`](https://launchdarkly.github.io/lua-server-sdk/modules/launchdarkly-server-sdk.html#makeContext).
</Accordion>

### Node.js (server-side)

<Accordion title="Expand Node.js (server-side) code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ts title="Node.js SDK v8.x (TypeScript)" theme={null}
          import * as ld from '@launchdarkly/node-server-sdk';

          const context: ld.LDContext = {
            kind: 'user',
            key: 'example-user-key',
            anonymous: true,
          }
    ```

    ```js title="Node.js SDK v7.x (JavaScript)" theme={null}
          const context = {kind:'user', key:'example-user-key', anonymous: true};
    ```

    ```ts title="Node.js SDK v7.x (TypeScript)" theme={null}
          import * as ld from 'launchdarkly-node-server-sdk';

          const context: ld.LDContext = {
            kind: 'user',
            key: 'example-user-key',
            anonymous: true,
          }
    ```
  </CodeGroup>
</Accordion>

### PHP

<Accordion title="Expand PHP code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```php title="PHP SDK v5.0" theme={null}
          $context = LDContext::builder("example-context-key")->anonymous(true)->build();
    ```
  </CodeGroup>
</Accordion>

### Python

<Accordion title="Expand Python code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```python title="Python SDK v8.0" theme={null}
          context = Context.builder("example-context-key").anonymous(True).build()
    ```

    ```python title="Python SDK v7.x" theme={null}
          user = { "key":"example-user-key", "anonymous": True }
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related [events](/sdk/concepts/events#omit-anonymous-contexts-from-events) to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read [`omit_anonymous_contexts`](https://launchdarkly-python-sdk.readthedocs.io/en/latest/api-main.html#ldclient.config.Config.omit_anonymous_contexts).
</Accordion>

### Ruby

<Accordion title="Expand Ruby code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ruby title="Ruby SDK v7.0" theme={null}
          context = LaunchDarkly::LDContext.create({ key: "example-context-key", anonymous: true })
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related [events](/sdk/concepts/events#omit-anonymous-contexts-from-events) to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read [`omit_anonymous_contexts`](https://launchdarkly.github.io/ruby-server-sdk/LaunchDarkly/Config.html#omit_anonymous_contexts-instance_method).
</Accordion>

### Rust

<Accordion title="Expand Rust code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```rust title="Rust SDK v1" theme={null}
          // Anonymous context with only a key
          let context = ContextBuilder::new("example-context-key").anonymous(true).build();

          // Anonymous context with a key plus other attributes
          let context = ContextBuilder::new("example-context-key").
              anonymous(true).
              set_value("country", "US".into()).
              build();
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related [events](/sdk/concepts/events#omit-anonymous-contexts-from-events) to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read [`omit_anonymous_contexts`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.EventProcessorBuilder.html#method.omit_anonymous_contexts).
</Accordion>

## Edge SDKs

Here are the configuration options for anonymous contexts in edge SDKs.

* [Akamai](#akamai)
* [Cloudflare](#cloudflare)
* [Fastly](#fastly)
* [Vercel](#vercel)

### Akamai

<Accordion title="Expand Akamai code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ts title="TypeScript" theme={null}
          import { LDContext } from '@launchdarkly/akamai-edgeworker-sdk-common';

          const anonymousContext: LDContext = { kind: 'user', key: 'example-user-key', anonymous: true };
    ```
  </CodeGroup>
</Accordion>

### Cloudflare

<Accordion title="Expand Cloudflare code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ts title="TypeScript" theme={null}
          import type { LDContext } from '@launchdarkly/cloudflare-server-sdk';

          const anonymousContext: LDContext = { kind: 'user', key: 'example-user-key', anonymous: true };
    ```
  </CodeGroup>
</Accordion>

### Fastly

<Accordion title="Expand Fastly code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ts title="TypeScript" theme={null}
          import type { LDContext } from '@launchdarkly/js-server-sdk-common';

          const anonymousContext: LDContext = { kind: 'user', key: 'example-user-key', anonymous: true };
    ```
  </CodeGroup>
</Accordion>

### Vercel

<Accordion title="Expand Vercel code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ts title="TypeScript" theme={null}
          import type { LDContext } from '@launchdarkly/vercel-server-sdk';

          const anonymousContext: LDContext = { kind: 'user', key: 'example-user-key', anonymous: true };
    ```
  </CodeGroup>
</Accordion>

## AI SDKs

Here are the configuration options for anonymous contexts in AI SDKs.

* [.NET AI](#net-ai)
* [Go AI](#go-ai)
* [Node.js (server-side) AI](#nodejs-server-side-ai)
* [Python AI](#python-ai)
* [Ruby AI](#ruby-ai)

### .NET AI

<Accordion title="Expand .NET AI SDK code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```csharp title=".NET AI SDK" theme={null}
          var context = Context.Builder("example-context-key")
              .Anonymous(true)
              .Build();
    ```
  </CodeGroup>
</Accordion>

### Go AI

<Accordion title="Expand Go AI SDK code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```go title="Go AI SDK" theme={null}
          import (
              "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
          )

          // Anonymous context with only a key
          context1 := ldcontext.NewBuilder("example-context-key").Anonymous(true)

          // Anonymous context with a key plus other attributes
          context2 := ldcontext.NewBuilder("context-key-456def").
              Anonymous(true).
              SetString("country", "Canada").
              Build()
    ```
  </CodeGroup>
</Accordion>

### Node.js (server-side) AI

<Accordion title="Expand Node.js (server-side) AI SDK code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ts title="Node.js (server-side) AI SDK (TypeScript)" theme={null}
          const context: LDContext = {
            kind: 'user',
            key: 'example-user-key',
            anonymous: true,
          }
    ```

    ```js title="Node.js (server-side) AI SDK (JavaScript)" theme={null}
          const context = {
            kind: 'user',
            key: 'example-user-key',
            anonymous: true,
          }
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list.
</Accordion>

### Python AI

<Accordion title="Expand Python AI SDK code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```python title="Python AI SDK" theme={null}
          context = Context.builder("example-context-key").anonymous(True).build()
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related [events](/sdk/concepts/events#omit-anonymous-contexts-from-events) to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read [`omit_anonymous_contexts`](https://launchdarkly-python-sdk.readthedocs.io/en/latest/api-main.html#ldclient.config.Config.omit_anonymous_contexts).
</Accordion>

### Ruby AI

<Accordion title="Expand Ruby AI SDK code sample">
  To distinguish logged-in end users from anonymous end users in the SDK:

  <CodeGroup>
    ```ruby title="Ruby AI SDK" theme={null}
          context = LaunchDarkly::LDContext.create({ key: "example-context-key", anonymous: true })
    ```
  </CodeGroup>

  Anonymous contexts do not appear on the **Contexts** list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related [events](/sdk/concepts/events#omit-anonymous-contexts-from-events) to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read [`omit_anonymous_contexts`](https://launchdarkly.github.io/ruby-server-sdk/LaunchDarkly/Config.html#omit_anonymous_contexts-instance_method).
</Accordion>
