LaunchDarkly Developer Documentation

Get started in under 30 minutes.
LaunchDarkly provides feature flags as a service for Java · Python · Ruby · Go · Node.js · PHP · .NET. Control feature launches -- who sees what and when -- without multiple code deploys. Easy dashboard for phased rollouts, targeting and segmenting.
Need more help? write us at support@launchdarkly.com

Get Started    Guides

Setting up LaunchDarkly to control your first feature flag takes only a few minutes. First, add the SDK to your project:

<dependency>
  <groupId>com.launchdarkly</groupId>
  <artifactId>launchdarkly-client</artifactId>
  <version>4.4.0</version>
</dependency>
pip install ldclient-py
gem install ldclient-rb
go get gopkg.in/launchdarkly/go-client.v4
npm install ldclient-node --save
php composer.phar require launchdarkly/launchdarkly-php
Install-Package LaunchDarkly.Client
pod `LaunchDarkly`
compile 'com.launchdarkly:launchdarkly-android-client:2.5.3'

Now import the LaunchDarkly client in your application code:

import com.launchdarkly.client.*;
import ldclient
require 'ldclient-rb'
import ld "gopkg.in/launchdarkly/go-client.v4"
const LaunchDarkly = require('ldclient-node');
require 'vendor/autoload.php';
using LaunchDarkly.Client;
#import "LDClient.h"
import com.launchdarkly.android.LDClient;
import com.launchdarkly.android.LDConfig;
import com.launchdarkly.android.LDUser;

Next, configure the LaunchDarkly client with your SDK or Mobile key and create the client:

LDClient ldClient = new LDClient(YOUR_SDK_KEY);
ldclient.set_sdk_key("YOUR_SDK_KEY")
ld_client = ldclient.get()
ld_client = LaunchDarkly::LDClient.new(YOUR_SDK_KEY)
ld_client := ld.MakeClient(YOUR_SDK_KEY, 5*time.Second)
ldclient = LaunchDarkly.init(YOUR_SDK_KEY)
$client = new LaunchDarkly\LDClient(YOUR_SDK_KEY);
LdClient ldClient = new LdClient("YOUR_SDK_KEY");
LDConfig *config = [[LDConfig alloc] initWithMobileKey: @"YOUR_MOBILE_KEY"];
LDConfig ldConfig = new LDConfig.Builder().setMobileKey("YOUR_MOBILE_KEY").build();

Finally, create a feature flag on your dashboard. When you create a flag, you specify a unique feature key-- you'll use this key in your application code to check which variation of a feature flag a user will receive by calling the variation method (or boolVariation in typed languages):

LDUser user = new LDUser("user@test.com");
boolean showFeature = ldClient.boolVariation("your.feature.key", user, false);
if (showFeature) {
  // application code to show the feature 
}
else {
  // the code to run if the feature is off
}
show_feature = ld_client.variation("your.flag.key", {"key": "user@test.com"}, False)
if show_feature:
    # application code to show the feature
else:
    # the code to run if the feature is off
show_feature = ld_client.variation("your.flag.key", {:key => "user@test.com"}, false)
if show_feature
    # application code to show the feature
else
    # the code to run if the feature is off
key := "user@test.com"
show_feature := ld_client.BoolVariation("your.flag.key", ld.NewUser(key), false)
if (show_feature) {
    // application code to show the feature
} else {
    // the code to run if the feature is off 
}
ldclient.once('ready', function() {
  ldclient.variation("your.flag.key", {"key": "user@test.com"}, false,
    function(err, showFeature) {
      if (showFeature) {
        // application code to show the feature
      } else {
        // the code to run if the feature is off
      }
    });
});
$user = new LaunchDarkly\LDUser("user@test.com");
if ($client->variation("your.flag.key", $user)) {
    # application code to show the feature
} else {
    # the code to run if the feature is off
}
User user = User.WithKey(username);
bool showFeature = ldClient.BoolVariation("your.feature.key", user, false);
if (showFeature) {
  // application code to show the feature 
}
else {
  // the code to run if the feature is off
}
LDUserBuilder *user = [[LDUserBuilder alloc] init];
user.key = @"aa0ceb";

[[LDClient sharedInstance] start:config withUserBuilder:user];

BOOL showFeature = [[LDClient sharedInstance] boolVariation:@"YOUR_FLAG_KEY" fallback:NO];
if (showFeature) {
        NSLog(@"Showing feature for %@", user.key);
} else {
        NSLog(@"Not showing feature for user %@", user.key);
}
LDConfig ldConfig = new LDConfig.Builder()
        .setMobileKey("MOBILE_KEY")
        .build();

LDUser user = new LDUser.Builder("user key")
        .email("fake@example.com")
        .build();

LDClient client = LDClient.init(getApplication(), ldConfig, user, 5);

if (client.boolVariation(FLAG_KEY, false)) {
    Log.i(TAG, "Showing feature");
} else {
    Log.i(TAG, "Not Showing feature");
}

The second argument to variation is the user object. The only mandatory property is the user key-- this is what LaunchDarkly uses to identify each user. The key does need to be unique to each user-- a database key, an e-mail address (though PII is discouraged), or a hash of an email address usually works.

Identifying users

You don't have to send users to LaunchDarkly in advance. You can target users before they have been populated on LaunchDarkly. Just set up your users in variation calls and you're all set.

Finally, shut down the LaunchDarkly client. This is something you only need to do once, when your application is about to terminate. It ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events.


// shut down the client, since we're about to quit
ldClient.close();

# shut down the client, since we're about to quit
ld_client.close()

# shut down the client, since we're about to quit
ld_client.close

// shut down the client, since we're about to quit
ld_client.Close()
// This example repeats the code from the previous example because in Node, the client methods are
// asynchronous, so in order to add another step that happens after the previous steps are finished,
// we need to add code *inside* the existing block. In other words, do not type the whole thing over
// again, just modify the last part you added as shown.
//
// Again, in a real application, this step is something you would only do when the application is
// about to quit-- NOT after every call to variation().

ldclient.once('ready', function() {
  ldclient.variation("your.flag.key", {"key": "user@test.com"}, false,
    function(err, showFeature) {
      if (showFeature) {
        // application code to show the feature
      } else {
        // the code to run if the feature is off
      }
    
      // ADDED: shut down the client, since we're about to quit
      ldclient.close();
    });
});

// This step does not exist in PHP, because the PHP SDK does not
// maintain long-lived network connections nor an event queue.

// shut down the client, since we're about to quit
ldClient.Dispose();  

// In iOS, you would shut down the client like this:
[[LDClient sharedInstance] stopClient];

// However, where you would place this code depends on the
// structure of your application. In this simple example,
// as long as you do not immediately terminate the application
// after starting it, you do not really need to shut down the
// client.

// In Android, you would shut down the client like this:
client.close();

// However, where you would place this code depends on the
// structure of your application. In this simple example,
// as long as you do not immediately terminate the application
// after starting it, you do not really need to shut down the
// client.

Once you're ready to test your integration, head over to the dev console to get a real-time view of your feature flag requests as they're received.

That's it! Now you can manage the feature on your dashboard-- read on to see how to target users or do controlled rollouts and A/B tests without re-deploying your application!

To see the advanced configuration options available for your platform's SDK, see the Reference guides section.