Configuration

Overview

This topic explains how to configure various LaunchDarkly SDKs. It gives code samples for each SDK that include different configuration examples. This feature is available for client-side, server-side, and edge SDKs.

Customize the SDK

You can use the configuration feature to configure certain aspects of your SDK, including flush intervals, timeout periods, and client connect parameters.

You can disable the publication of events for testing purposes. We strongly recommend against disabling events for any other reason because many features depend on regularly receiving analytics events, including targeting rules, flag statuses, and the Contexts or Users lists. To learn more, read Analytics events.

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

Client-side SDKs

This feature is available in the following client-side SDKs:

.NET (client-side)

The .NET (client-side) SDK uses a mobile key

The .NET (client-side) SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to create a custom configuration object to pass custom parameters to the client. With it, we’ve configured the event queue flush frequency.

You can configure Configuration.Builder:

1var config = Configuration
2 .Builder("mobile-key-123abc", ConfigurationBuilder.AutoEnvAttributes.Enabled)
3 .Events(Components.SendEvents().FlushInterval(TimeSpan.FromSeconds(2)))
4 .Build();
5LdClient client = LdClient.Init(config, context);

To learn more about the specific configuration options available in this SDK, read Configuration.

Android

The Android SDK uses a mobile key

The Android SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

In the Android SDK, most of the configuration properties are grouped into areas of functionality, each of which has its own builder class that is only available if you are using that functionality. The basic areas of functionality are data source, events, networking, and service URIs.

This code sample shows you how to configure the client connect and flush interval parameters:

1LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
2 .mobileKey("mobile-key-123abc")
3 .http(
4 Components.httpConfiguration()
5 .connectTimeoutMillis(5000)
6 )
7 .events(
8 Components.sendEvents()
9 .flushIntervalMillis(5000)
10 )
11 .build();

To learn more about the specific configuration options available in this SDK, read LDConfig.Builder.

C++ (client-side)

The C++ (client-side) SDK uses a mobile key

The C++ (client-side) SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to configure the event queue capacity and flush interval parameters:

1auto config_builder = client_side::ConfigBuilder("mobile-key-123abc");
2config_builder.Events()
3 .Capacity(1000)
4 .FlushInterval(std::chrono::seconds(30));
5auto config = config_builder.Build();
6if (!config) {
7 /* an error occurred, config is not valid */
8}

To learn more about the specific configuration options available in this SDK, read ConfigBuilder. To learn more about the event queue capacity and flush interval parameters specifically, read EventsBuilder.

Electron

The Electron SDK uses a client-side ID

The Electron SDK uses a client-side ID. Client-side IDs are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to create a custom configuration object to pass custom parameters to the client:

1const options = {
2 allAttributesPrivate: true
3};
4const client = LDElectron.initializeInMain('client-side-id-123abc', user, options);

To learn more about the specific configuration options available in this SDK, read LDOptions.

Flutter

The Flutter SDK uses a mobile key or client-side ID

The Flutter SDK version 4 uses either a mobile key or a client-side ID, depending on the platform that you build for. If you are building for Windows, Mac, Linux, Android, or iOS, you must use a mobile key. If you are building for a web browser, you must use a client-side ID.

Your environment’s mobile key and client-side ID are specific to each project and environment. They are both available from the Environments list for each project. To learn more about key types, read Keys.

The Flutter SDK version 3 and earlier requires a mobile key, as it only works on mobile platforms.

The Flutter SDK version 4 uses either a mobile key or a client-side ID, depending on the platform that you build for. You can set these in the LAUNCHDARKLY_MOBILE_KEY and LAUNCHDARKLY_CLIENT_SIDE_ID environment variables, and then use the CredentialSource helper to select your credential and provide it to your configuration. CredentialSource expects one of the two environment variables to be set, but not both.

This code sample shows you how to configure the credential, automatic environment attributes, and evaluation reasons options:

1final config = LDConfig(
2 CredentialSource.fromEnvironment,
3 AutoEnvAttributes.enabled,
4 dataSourceConfig: DataSourceConfig(
5 evaluationReasons: true
6 ),
7);

The credential and automatic environment attributes configuration options are required. All other configuration options are optional and use default values if not set.

To learn more about the specific configuration options available in this SDK, read LDConfig.

iOS

The iOS SDK uses a mobile key

The iOS SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to configure the client connection timeout and event flush interval parameters:

1var ldConfig = LDConfig(mobileKey: "mobile-key-123abc", autoEnvAttributes: .enabled)
2ldConfig.connectionTimeout = 10.0
3ldConfig.eventFlushInterval = 30.0

To learn more about the specific configuration options available in this SDK, read LDConfig.

JavaScript

The JavaScript SDK uses a client-side ID

The JavaScript SDK uses a client-side ID. Client-side IDs are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to create a custom configuration object to pass custom parameters to the client:

1const options = { allAttributesPrivate: true };
2const client = LDClient.initialize('client-side-id-123abc', context, options);

To learn more about the specific configuration options available in this SDK, read LDOptions.

Node.js (client-side)

The Node.js (client-side) SDK uses a client-side ID

The Node.js (client-side) SDK uses a client-side ID. Client-side IDs are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to create a custom configuration object to pass custom parameters to the client:

1const options = {
2 flushInterval: 10000, // milliseconds
3 allAttributesPrivate: true
4};
5
6const client = LDClient.initialize('client-side-id-123abc', context, options);
7try {
8 await client.waitForInitialization(5);
9 // initialization succeeded, flag values are now available
10} catch (err) {
11 // initialization failed or did not complete before timeout
12}

To learn more about the specific configuration options available in this SDK, read LDOptions.

React Native

The React Native SDK uses a mobile key

The React Native SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to create a configuration object to pass configuration parameters to the client.

The configuration object can include a variety of options. In version 10 of the SDK, all properties are optional.

In versions 9 and earlier, mobileKey was a required property, but all other properties were optional. Additionally, in versions 9 and earlier we recommend setting a timeout parameter when you call configure. If the client receives flag values before the timeout, the returned promise will resolve. Otherwise, it will be rejected.

Always include a timeout parameter in v9 and earlier when calling configure

In versions 9 and earlier, do not configure your SDK to initialize without a timeout parameter. Doing so will cause your app never to load if there is a connectivity problem. We recommend setting a timeout for no more than 1-5 seconds. To learn more, read React Native SDK reference.

Here is an example:

1import { type LDOptions } from '@launchdarkly/react-native-client-sdk';
2
3const options = {
4 withReasons: true,
5};
6
7const client = new ReactNativeLDClient('mobile-key-123abc', AutoEnvAttributes.Enabled, options);

To learn more about the specific configuration options available in this SDK, read LDOptions.

Roku

The Roku SDK uses a mobile key

The Roku SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to create a configuration object:

BrightScript
1' for a legacy Roku application
2config = LaunchDarklyConfig("mobile-key-123abc")
3
4' for a SceneGraph Roku Application
5config = LaunchDarklyConfig("mobile-key-123abc", CLIENT_SCENEGRAPH_NODE)

We support the following configuration options for both SceneGraph and non-SceneGraph usage:

BrightScript
1config.setAppURI(String)
2
3config.setEventsURI(String)
4
5config.setStreamURI(String)
6
7
8config.setPollingIntervalSeconds(Integer)
9
10config.setOffline(Boolean)
11
12
13config.addPrivateAttribute(String)
14
15config.setAllAttributesPrivate(Boolean)
16
17
18config.setEventsCapacity(Integer)
19
20config.setEventsFlushIntervalSeconds(Integer)
21
22
23config.setLogger(Object)
24
25config.setLoggerNode(Dynamic)
26
27config.setStoreBackend(Object)
28
29config.setStoreBackendNode(Dynamic)
30
31
32config.setStreaming(Boolean)
33
34config.setLogLevel(Integer)
35
36
37config.setUseEvaluationReasons(Boolean)
38
39config.setApplicationInfoValue(String, String)

Server-side SDKs

This feature is available for the following server-side SDKs:

.NET (server-side)

The .NET (server-side) SDK uses an SDK key

The .NET (server-side) SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object:

1var config = Configuration.Builder("sdk-key-123abc")
2 .Events(
3 Components.SendEvents().FlushInterval(TimeSpan.FromSeconds(2))
4 )
5 .StartWaitTime(TimeSpan.FromSeconds(5))
6 .Build();
7var client = new LdClient(config);

To learn more about the specific configuration that are available in this SDK, read ConfigurationBuilder.

Apex

This code sample shows you how to configure the client to redact all user attributes in events:

Apex
1LDConfig config = new LDConfig.Builder()
2 .setAllAttributesPrivate(true)
3 .build();

To learn more about the specific configuration options available for this SDK, read LDConfig.Builder.

C++ (server-side)

The C++ (server-side) SDK uses an SDK key

The C++ (server-side) SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to configure the event queue capacity and flush interval parameters:

1auto config_builder = server_side::ConfigBuilder("sdk-key-123abc");
2config_builder.Events()
3 .Capacity(1000)
4 .FlushInterval(std::chrono::seconds(30));
5auto config = config_builder.Build();
6if (!config) {
7 /* an error occurred, config is not valid */
8}

To learn more about the specific configuration options available for this SDK, read ConfigBuilder. To learn more about the event queue capacity and flush interval parameters specifically, read EventsBuilder.

Erlang

The Erlang SDK uses an SDK key

The Erlang SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters when the client starts with the Options map parameter. The code in this example turns off streaming, so the SDK connects to LaunchDarkly through polling.

To pass custom parameters:

Erlang
1% Specify options
2ldclient:start_instance("sdk-key-123abc", #{stream => false})
3
4% With a custom instance name
5ldclient:start_instance("sdk-key-123abc", your_instance, #{stream => false})

To learn more about the specific configuration options available for this SDK, read ldclient_config.

Go

The Go SDK uses an SDK key

The Go SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object. The code in this sample configures the event flush interval parameter.

To pass custom parameters:

1import (
2 "time"
3
4 ld "github.com/launchdarkly/go-server-sdk/v6"
5 "github.com/launchdarkly/go-server-sdk/v6/ldcomponents"
6)
7
8var config ld.Config
9
10config.Events = ldcomponents.SendEvents().FlushInterval(10*time.Second)
11
12client, _ := ld.MakeCustomClient("sdk-key-123abc", config, 5*time.Second)

To learn more about the specific configuration options available for this SDK, read Config.

Haskell

The Haskell SDK uses an SDK key

The Haskell SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object. This example configures the event queue capacity and flush interval parameters.

To pass custom parameters:

Haskell
1{-# LANGUAGE OverloadedStrings #-}
2
3import LaunchDarkly.Server.Config
4
5import Data.Function ((&))
6
7config :: Config
8config = (makeConfig "sdk-key-123abc")
9 & configSetEventsCapacity 1000
10 & configSetFlushIntervalSeconds 30

To learn more about the specific configuration properties that are available in this SDK, read Config.

Java

The Java SDK uses an SDK key

The Java SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object.

In this example, we’ve configured two properties for HTTP (the connect and socket timeouts), and one property for analytics events (the event flush interval).

To pass custom parameters:

Java
1LDConfig config = new LDConfig.Builder()
2 .http(
3 Components.httpConfiguration()
4 .connectTimeout(Duration.ofSeconds(3))
5 .socketTimeout(Duration.ofSeconds(3))
6 )
7 .events(
8 Components.sendEvents()
9 .flushInterval(Duration.ofSeconds(10))
10 )
11 .build();
12LDClient client = new LDClient("sdk-key-123abc", config);

To learn more about the specific configuration properties that are available in this SDK, read LDConfig.Builder.

Lua

The Lua SDK uses an SDK key

The Lua SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object.

Finish setting up your configuration client before you call LDClientInit

You must finish setting up your configuration object before you call clientInit. If you initialize the client before configuration is complete, the SDK will not use anything you provide after initialization.

Here, we’ve configured the event queue capacity and flush interval parameters:

1local config = {
2 events = {
3 capacity = 1000,
4 flushIntervalMilliseconds = 30000
5 }
6}
7
8-- This blocks for 1 second to initialize
9local client = ld.clientInit("sdk-key-123abc", 1000, config)

To learn more about the specific configuration properties that are available in this SDK, read clientInit.

Node.js (server-side)

The Node.js (server-side) SDK uses an SDK key

The Node.js (server-side) SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object:

1import * as ld from '@launchdarkly/node-server-sdk';
2
3const options: ld.LDOptions = {
4 timeout: 3,
5};
6const client = ld.init('sdk-key-123abc', options);

To learn more about the specific configuration options available in this SDK, read LDOptions.

PHP

The PHP SDK uses an SDK key

The PHP SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample uses the cache option, which passes as an array to the client constructor. There are a few additional options you can set in this array.

We’ve set the client connect timeout to three seconds in addition to providing a custom cache storage provider.

Sending events in PHP

The LaunchDarkly SDK sends data back to our server to record events from track and variation calls. On our other platforms, this data is sent asynchronously, so that it adds no latency to serving web pages. PHP’s shared-nothing architecture makes this difficult.

By default, LaunchDarkly forks an external process that executes curl to send this data. In practice, we’ve found that this is the most reliable way to send data without introducing latency to page load times. If your server does not have curl installed, or has other restrictions that make it impossible to invoke curl as an external process, you may need to implement a custom EventProcessor to send events to LaunchDarkly.

Here is an example:

PHP
1$client = new LaunchDarkly\LDClient("sdk-key-123abc", ["cache" => $cacheStorage, "connect_timeout" => 3]);

To learn more about the specific configuration options available in this SDK, read the SDK API documentation for the LDClient constructor.

Python

The Python SDK uses an SDK key

The Python SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object:

Python
1config = Config(sdk_key='sdk-key-123abc', http=HTTPConfig(connect_timeout=5))
2ldclient.set_config(config)

To learn more about the specific configuration options available in this SDK, read ldclient.config.

Ruby

The Ruby SDK uses an SDK key

The Ruby SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to configure the behavior of the client by creating a custom configuration object.

The client constructor takes a configuration object as an optional parameter. In this example, we’ve set the connection timeout to LaunchDarkly to one second, and the read timeout to two seconds.

To create a custom configuration object:

Ruby
1config = LaunchDarkly::Config.new({connect_timeout: 1, read_timeout: 2})
2client = LaunchDarkly::LDClient.new("sdk-key-123abc", config)

To learn more about the specific configuration options available in this SDK, read Config.

Rust

The Rust SDK uses an SDK key

The Rust SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object:

Rust
1let config = ConfigBuilder::new("sdk-key-123abc")
2 .offline(true)
3 .build();
4let client = Client::build(config).unwrap();

To learn more about the specific configuration options available in this SDK, read Config.

Edge SDKs

This feature is available in the following edge SDKs:

Akamai

The Akamai SDK uses a client-side ID

The Akamai SDK uses a client-side ID. Client-side IDs are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object. The Akamai SDK only supports the logger configuration option.

1import { init, LDOptions, BasicLogger } from '@launchdarkly/akamai-server-edgekv-sdk';
2
3const options: LDOptions = {
4 logger: BasicLogger.get(),
5};
6
7const ldClient = init({
8 sdkKey: 'client-side-id-123abc',
9 namespace: 'your-edgekv-namespace',
10 group: 'your-edgekv-group-id',
11 options: options,
12});

To learn more about the specific configuration options available in this SDK, read LDOptions.

Cloudflare

The Cloudflare SDK uses a client-side ID

The Cloudflare SDK uses a client-side ID. Client-side IDs are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object.

Versions 1 and 2 of the Cloudflare SDK only support the logger configuration option. Versions 2.3.0 and later also support an events configuration option.

1import { BasicLogger, init, LDOptions } from '@launchdarkly/cloudflare-server-sdk';
2
3const options: LDOptions = {
4 logger: new BasicLogger({ destination: console.log }),
5 sendEvents: true, // default is false
6};
7
8client = init('client-side-id-123abc', env.LD_KV, options);

To learn more about the specific configuration options available in this SDK, read the SDK’s API docs.

Vercel

The Vercel SDK uses a client-side ID

The Vercel SDK uses a client-side ID. Client-side IDs are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

This code sample shows you how to pass custom parameters to the client by creating a custom configuration object.

Version 1 of the Vercel SDK only supports the logger configuration option. Versions 1.2.0 and later also support an events configuration option.

1import { createClient } from '@vercel/edge-config';
2import { BasicLogger, init, LDOptions } from '@launchdarkly/vercel-server-sdk';
3
4const edgeConfigClient = createClient(process.env.EDGE_CONFIG);
5
6const options: LDOptions = {
7 logger: new BasicLogger({ destination: console.log }),
8 sendEvents: true, // default is false
9};
10
11client = init('client-side-id-123abc', edgeConfigClient, options);

To learn more about the specific configuration options available in this SDK, read LDOptions.

Built with