Configuration
Read time: 19 minutes
Last edited: Nov 20, 2023
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 both client-side and server-side SDKs.
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.
Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.
Customizing 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)
- Android
- C++ (client-side)
- Electron
- Flutter
- iOS
- JavaScript
- Node.js (client-side)
- React Native
- Roku
.NET (client-side)
Expand .NET (client-side) code sample
The .NET (client-side) SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. 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
:
var config = Configuration.Builder("mobile-key-123abc", ConfigurationBuilder.AutoEnvAttributes.Enabled).Events(Components.SendEvents().FlushInterval(TimeSpan.FromSeconds(2))).Build();LdClient client = LdClient.Init(config, context);
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Android
Expand Android code sample
The Android SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.
This code sample shows you how to configure the client connect and flush interval parameters:
LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled).mobileKey("mobile-key-123abc").setConnectionTimeoutMillis(5000).setEventsFlushIntervalMillis(5000).build();
To learn more about the specific configuration options that are available in this SDK, read the Javadoc.
C++ (client-side)
Expand C++ (client-side) code sample
The C++ (client-side) SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.
This code sample shows you how to configure the event queue capacity and flush interval parameters:
auto config = client_side::ConfigBuilder("mobile-key-123abc").Events().Capacity(1000).FlushInterval(std::chrono::seconds(30)).Build();
To learn more about the configuration options that are available in this SDK, read ConfigBuilder
. To learn more about the event queue capacity and flush interval parameters specifically, read EventsBuilder
.
Electron
Expand Electron code sample
The Electron SDK uses a client-side ID. Your environment's client-side ID is available in the Projects tab of your Account settings page. 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:
const options = {allAttributesPrivate: true};const client = LDElectron.initializeInMain('client-side-id-123abc', user, options);
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Flutter
Expand Flutter code sample
The Flutter SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.
This code sample shows you how to configure the client connect and flush interval parameters:
LDConfig ldConfig = LDConfigBuilder('mobile-key-123abc', AutoEnvAttributes.Enabled).connectionTimeoutMillis(5000).eventsFlushIntervalMillis(5000).build();
To learn more about the specific configuration options that are available in this SDK, read LDConfigBuilder
.
iOS
Expand iOS code sample
The iOS SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. 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:
var ldConfig = LDConfig(mobileKey: "mobile-key-123abc", autoEnvAttributes: .enabled)ldConfig.connectionTimeout = 10.0ldConfig.eventFlushInterval = 30.0
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
JavaScript
Expand JavaScript code sample
The JavaScript SDK uses a client-side ID. Your environment's client-side ID is available in the Projects tab of your Account settings page. 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:
const options = { allAttributesPrivate: true };const client = LDClient.initialize('client-side-id-123abc', context, options);
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Node.js (client-side)
Expand Node.js (client-side) code sample
The Node.js (client-side) SDK uses a client-side ID. Your environment's client-side ID is available in the Projects tab of your Account settings page. 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:
const options = {flushInterval: 10000, // millisecondsallAttributesPrivate: true};const client = LDClient.initialize('client-side-id-123abc', context, options);
For a full guide to the available configuration options, read LDOptions
in the TypeScript reference.
React Native
Expand React Native code sample
The React Native SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. 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.
The config
object can include a variety of options. mobileKey
is a required property, but all other properties are optional.
Here is an example, in TypeScript:
import LDClient, { LDConfig } from 'launchdarkly-react-native-client-sdk';let config: LDConfig = {mobileKey: 'mobile-key-123abc',enableAutoEnvAttributes: true,connectionTimeout: 3000};await client.configure(config, context);
Here is an example, in JavaScript:
let config = {mobileKey: 'mobile-key-123abc',enableAutoEnvAttributes: true,connectionTimeout: 3000};await client.configure(config, context);
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Roku
Expand Roku code sample
The Roku SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.
This code sample shows you how to create a configuration object:
REM for a legacy Roku applicationconfig = LaunchDarklyConfig("mobile-key-123abc")REM for a SceneGraph Roku Applicationconfig = LaunchDarklyConfig("mobile-key-123abc", CLIENT_SCENEGRAPH_NODE)
We support the following configuration options for both SceneGraph and non-SceneGraph usage:
config.setAppURI(String)config.setEventsURI(String)config.setStreamURI(String)config.setPollingIntervalSeconds(Integer)config.setOffline(Boolean)config.addPrivateAttribute(String)config.setAllAttributesPrivate(Boolean)config.setEventsCapacity(Integer)config.setEventsFlushIntervalSeconds(Integer)config.setLogger(Object)config.setLoggerNode(Dynamic)config.setStoreBackend(Object)config.setStoreBackendNode(Dynamic)config.setStreaming(Boolean)config.setLogLevel(Integer)config.setUseEvaluationReasons(Boolean)config.setApplicationInfoValue(String, String)
Server-side SDKs
This feature is available for the following server-side SDKs:
- .NET (server-side)
- Apex
- C/C++ (server-side)
- Erlang
- Go
- Haskell
- Java
- Lua
- Node.js (server-side)
- PHP
- Python
- Ruby
- Rust
.NET (server-side)
Expand .NET (server-side) code sample
The .NET (server-side) SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
var config = Configuration.Builder("sdk-key-123abc").Events(Components.SendEvents().FlushInterval(TimeSpan.FromSeconds(2))).Build();var client = new LdClient(config);
For a complete list of configuration options for the client, including proxy settings, timeouts, and streaming/polling options, read the SDK's API documentation for ConfigurationBuilder
.
Apex
Expand Apex code sample
This code sample shows you how to configure the client to redact all user attributes in events:
LDConfig config = new LDConfig.Builder().setAllAttributesPrivate(true).build();
To learn more about the configuration options available for this SDK, read the SDK's API docs.
C/C++ (server-side)
Expand C/C++ (server-side) code sample
The C/C++ (server-side) SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.
LDClientInit
You must finish setting up your configuration object before you call LDClientInit
. If you initialize the client before configuration is complete, the SDK will not use anything you provide after initialization.
This code sample shows you how to configure the event queue capacity and flush interval parameters:
struct LDConfig *config = LDConfigNew("sdk-key-123abc");LDConfigSetEventsCapacity(config, 1000);LDConfigSetEventsFlushInterval(config, 30000);
For a complete list of configuration options, read the SDK's API docs.
Erlang
Expand Erlang code sample
The Erlang SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
% Specify optionsldclient:start_instance("sdk-key-123abc", #{stream => false})% With a custom instance nameldclient:start_instance("sdk-key-123abc", your_instance, #{stream => false})
Go
Expand Go code sample
The Go SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
import ("time"ld "github.com/launchdarkly/go-server-sdk/v6""github.com/launchdarkly/go-server-sdk/v6/ldcomponents")var config ld.Configconfig.Events = ldcomponents.SendEvents().FlushInterval(10*time.Second)client, _ := ld.MakeCustomClient("sdk-key-123abc", config, 5*time.Second)
To learn more about the configuration options, read Config
.
Haskell
Expand Haskell code sample
The Haskell SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
{-# LANGUAGE OverloadedStrings #-}import LaunchDarkly.Server.Configimport Data.Function ((&))config :: Configconfig = (makeConfig "sdk-key-123abc")& configSetEventsCapacity 1000& configSetFlushIntervalSeconds 30
To learn more about the specific configuration properties that are available in this SDK, read the SDK's API docs.
Java
Expand Java code sample
The Java SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
LDConfig config = new LDConfig.Builder().http(Components.httpConfiguration().connectTimeout(Duration.ofSeconds(3)).socketTimeout(Duration.ofSeconds(3))).events(Components.sendEvents().flushInterval(Duration.ofSeconds(10))).build();LDClient client = new LDClient("sdk-key-123abc", config);
For a complete list of configuration options for the client, including proxy settings, timeouts, and streaming/polling options, read the Javadoc for LDConfig.Builder
.
Lua
Expand Lua code sample
The Lua SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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.
LDClientInit
You must finish setting up your configuration object before you call LDClientInit
. 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:
local config = {key = sdk-key-123abc,eventsCapacity = 1000,eventsFlushInterval = 30000}local client = ld.clientInit(config, 1000)
To learn more about configuration options, read the SDK's API docs.
Node.js (server-side)
Expand Node.js (server-side) code sample
The Node.js (server-side) SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
import * as ld from '@launchdarkly/node-server-sdk';const options: ld.LDOptions = {timeout: 3,};const client = ld.init('sdk-key-123abc', options);
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
PHP
Expand PHP code sample
The PHP SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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.
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:
$client = new LaunchDarkly\LDClient("sdk-key-123abc", array("cache" => $cacheStorage, "connect_timeout" => 3));
For a complete list of customizable parameters, read the documentation for the LDClient constructor.
Python
Expand Python code sample
The Python SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
config = Config(sdk_key='sdk-key-123abc', http=HTTPConfig(connect_timeout=5))ldclient.set_config(config)
The Config
type lets you specify a variety of options. To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Ruby
Expand Ruby code sample
The Ruby SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
config = LaunchDarkly::Config.new({connect_timeout: 1, read_timeout: 1})client = LaunchDarkly::LDClient.new("sdk-key-123abc", config)
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Rust
Expand Rust code sample
The Rust SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. 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:
let config = ConfigBuilder::new("sdk-key-123abc").offline(true).build();let client = Client::build(config).unwrap();
The Config
type lets you specify a variety of options. To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Edge SDKs
This feature is available in the following edge SDKs:
Akamai
Expand Akamai code sample
The Akamai SDK uses a client-side ID. Your environment's client-side ID is available in the Projects tab of your Account settings page. 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.
import { init, LDOptions, BasicLogger } from '@launchdarkly/akamai-server-edgekv-sdk';const options: LDOptions = {logger: BasicLogger.get(),};const ldClient = init({sdkKey: 'client-side-id-123abc',namespace: 'your-edgekv-namespace',group: 'your-edgekv-group-id',options: options,});
To learn more about the specific configuration options that are available in this SDK, read LDOptions
.
Cloudflare
Expand Cloudflare code sample
The Cloudflare SDK uses a client-side ID. Your environment's client-side ID is available in the Projects tab of your Account settings page. 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.
import { BasicLogger, init, LDOptions } from '@launchdarkly/cloudflare-server-sdk';const options: LDOptions = {logger: new BasicLogger({ destination: console.log }),sendEvents: true, // default is false};client = init('client-side-id-123abc', env.LD_KV, options);
To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.
Vercel
Expand Vercel code sample
The Vercel SDK uses a client-side ID. Your environment's client-side ID is available in the Projects tab of your Account settings page. 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.
import { createClient } from '@vercel/edge-config';import { BasicLogger, init, LDOptions } from '@launchdarkly/vercel-server-sdk';const edgeConfigClient = createClient(process.env.EDGE_CONFIG);const options: LDOptions = {logger: new BasicLogger({ destination: console.log }),sendEvents: true, // default is false};client = init('client-side-id-123abc', edgeConfigClient, options);
To learn more about the specific configuration options that are available in this SDK, read LDOptions
.