• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""


Getting started: Configuring an SDK and creating a flag

Read time: 8 minutes
Last edited: Jul 29, 2022


This guide explains how to set up an SDK and create a flag in LaunchDarkly. The exercise in this guide shows you how to use a flag to control how that feature is released to users.

Feature management starts with flags. Feature flags wrap your code and give you the ability to release your feature in a safe and controlled way.

In this guide, you will use Glitch to complete the process of creating a flag in LaunchDarkly, including configuring your environment and SDKs, and creating the flag itself.

You will:

  • Clone a sample web app
  • Add the LaunchDarkly SDK to the web app
  • Create and use your first LaunchDarkly feature flag
  • Turn on your first feature


To complete this guide, you must have the following prerequisites:

  • An active LaunchDarkly account with the ability to create a flag.
  • Basic knowledge of the Python and JavaScript programming languages. You need these to complete the exercises in this guide.
  • Access to Glitch.

To learn more, read How to use Glitch.


Read this section to understand the way we use some terms in this guide.

LaunchDarkly data hierarchy

The basic unit of work in LaunchDarkly is a feature flag, often referred to as a "flag." Flags exist inside environments, which are grouped into projects. A project can contain multiple environments, and flags exist across all environments within a project.

Many software projects have a test, staging, and production environment within each project. In LaunchDarkly, you can define any environment structure that suits your team's needs. For example, your mobile team might need an android test, android prod, ios test, and ios prod environment in a "Mobile apps" project.

To learn more, read Projects and Environments.

LaunchDarkly SDK basics

The LaunchDarkly SDK is the primary tool you use to manage feature flags in your application code. The SDK is responsible for making the connection to LaunchDarkly and giving your app access to the flags defined in your environment.

After you implement the client SDK in your application code, your code can fetch a flag from LaunchDarkly and evaluate it locally to get the flag’s value for a given user or context.

To learn more, read Getting started with SDKs.

LaunchDarkly SDKs come in a variety of client-side, server-side, and mobile implementations for different languages. This guide uses the Python SDK for our server-side code.

There are several differences between the server and client-side SDKs.

Server-side SDKs are designed for multi-user systems and services running on a corporate network or in the cloud. Because they are designed to exist in more secure environments, and not rely on mobile data to make connections to LaunchDarkly, server-side SDKs retrieve the entire set of flag rules and evaluate the flag values for each user or context within the SDK's runtime.

Client-side SDKs are designed to run on a single-user desktop, embedded application, client-side web application, or on mobile devices. Client-side SDKs receive the evaluated value of a flag for the user on the chosen application directly from LaunchDarkly. We assume that client-side SDKs are on devices that may be shared or less secure, so LaunchDarkly never transmits flag rules to client-side SDKs.

To learn more, read Understanding the different types of SDKs.

Using feature flags with variations

With LaunchDarkly's feature flags, you can develop a feature, wrap it in a feature flag, and prevent users from accessing it until you want them to. When you wrap a feature in a feature flag, you're gating access to the feature based on a condition you specify.

You can also specify multiple variations of a flag to produce different outcomes. For example, you could create three variations of the same flag, each of which shows a price page with a 10%, 15%, or 25% discount.

The simplest feature flags have two variations: true and false. These are called boolean flags. This is the kind of flag we'll use in the exercise below.

An example of a feature flag wrapped around a feature is depicted below:

if enableFeature(on.click.checkout, {...}) then
/* show the new feature */
/* show the old feature */

The ld_client has a few methods that allow your code to decide which variation of a feature to show.

An example of a variation method is depicted below:

# flag-key: String
# user: Python dictionary
# default-variation: boolean, string, or dictionary
ld_client.variation(<FLAG-KEY>, <USER>, <DEFAULT-VARIATION>)

The user property in the second parameter of the variation() method is an object that describes the user against which you want to evaluate that flag.

LaunchDarkly keeps track of this user and remembers what variation they received so your app can show them the same feature each time they visit. This also gives you knowledge about your users so you can target specific users with your new feature.

The third property, default-variation, is the value that the flag defaults to if ldclient encounters an error. For example, if the feature flag key doesn't exist or the user doesn't have a key specified, LaunchDarkly returns the default flag variation.

To learn more, read Targeting users and Variations.

Exercise: Using the LaunchDarkly SDK

In this exercise, we'll implement a pricing page on a mock online store. You will introduce a new pricing tier, wrap the code for it in a feature flag, and release it to users by enabling the flag.

This exercise uses a Python-based web application we provide and runs on Glitch, an online tool for creating web apps.

Here is an image of the Glitch app:

The Starlight Travel pricing page, running in Glitch.
The Starlight Travel pricing page, running in Glitch.

Remixing the Glitch app

You must remix the Glitch project embedded below to create an editable copy of the Python web app. Remixing is Glitch's term for cloning a project. This creates an editable copy of the Python web app we use for this exercise.

To remix the app, click this Glitch remix link, or follow the instructions below.

To remix the app:

  1. Click the fullscreen button in the bottom right-hand corner of the Glitch app embedded above. The app opens in a separate tab.
  2. In the new Glitch tab, click Remix to Edit.
  3. The name of your project appears in the top left corner. If you want to rename the app, click the arrow next to the name.

Learn more: How to use Glitch

Switching between the code and app views in Glitch

In the Glitch app, you have the option to view the live app in production, or the code for the app.

To switch between them:

  1. Navigate to the Glitch app you remixed.
  2. At the top of the Glitch page, click Show:
The "Show" menu in Glitch.
The "Show" menu in Glitch.
  1. Confirm that you can switch between the code and the app view.

Setting up your LaunchDarkly project and environment

You must set up a LaunchDarkly project and environment where your flags will live. Create a new project or use an existing one in your account.

To create a new project:

  1. Navigate to the Account settings page.
  2. Click the Projects tab.
  3. Click Create project. The "Create a project" panel appears.
  4. Give your project a human-readable Name. In the example below, the project is called Starlight Travel.
  5. Click Create project. The new project's Environments tab appears.
  6. Click the "Production" environment SDK key to copy it to your clipboard:
The Account Settings page in LaunchDarkly with environment SDK keys called out.
The Account Settings page in LaunchDarkly with environment SDK keys called out.

Configuring the LaunchDarkly SDK key in your app

Glitch lets you store environment variables, such as secrets or config variables, that your app can read from memory. This is a more secure practice than hard-coding these directly into your code.

To learn more about environment variables in Glitch, read Glitch's documentation.

To configure the LaunchDarkly SDK key in your app:

  1. Click the .env file in the Glitch sidebar.
  2. Assign the SDK key you copied above to an environment variable called LD_SDK_KEY:
The .env file in Glitch.
The .env file in Glitch.

Installing the Python SDK in your code

Now you must install the LaunchDarkly Python SDK in your Python code. To do this, add the LaunchDarkly Python SDK as a dependency to the Python app by adding the following line to the requirements file.

Here's how:


The requirements file defines all of the Python modules the app you're building depends on.

This app is configured to install dependencies and restart when certain of its files change. You do not need to refresh or reinstall anything.

To verify a successful installation, open the the log console in Glitch:

  1. Click Tools in the bottom left hand corner.
  2. Select Logs.

If you configured the Python SDK correctly, you will receive a message indicating that launchdarkly-server-sdk was installed.

Initializing the LaunchDarkly client

After you install the SDK, you can import the LaunchDarkly client into your code and initialize it.

To import and initialize the LaunchDarkly client:

  1. Add the following line to the top of your server.py file:
import ldclient
  1. Specify your SDK key to connect the SDK to LaunchDarkly:
# Paste your SDK key inside the `.env` file. Don't paste it
# here or you'll expose your SDK key to anyone who opens
# this app. Instead, use the environment variable to
# reference the SDK key, as shown in the example below:
# We will use this client from now on:
ld_client = ldclient.get()

You've initialized the ldclient. Next, use the client to wrap your app's new pricing tier inside a flag.

Creating a new flag

Before you add code to the pricing page, create a new flag in your LaunchDarkly project. Name the flag "Pricing tier 3."

Here's how to create the new flag:

  1. In LaunchDarkly, navigate to the Feature flags list.
  2. Click Create flag:
The "Create flag" button on the flags list.
The "Create flag" button on the flags list.
  1. Enter "Pricing tier 3" in the Name field.
  2. (Optional) Add a Description of the flag.
  3. Leave the flag variation set to "Boolean":
The "Create a feature flag" panel.
The "Create a feature flag" panel.
  1. Click Save flag.

The flag's targeting state is set to Off by default. You can turn it on after implementing the code to use it.

The new flag is pictured below:

The "Pricing tier 3" flag.
The "Pricing tier 3" flag.

Adding a new pricing tier

You're now ready to add the new pricing tier to the pricing page. To do this, you'll add the HTML for the new page to your app, but only after wrapping it in a feature flag so only intended users will receive it.

The updated pricing tier page is pictured below:

The new pricing tier.
The new pricing tier.

It uses the following HTML:

<!-- Admiral Tier -->
<div class="card h-100">
<div class="card-body">
<h5 class="card-title text-center">
Admiral Class
<h6 class="card-price text-center">
<i class="fab fa-btc"></i> 250,000
<hr />
<ul class="fa-ul">
<span class="fa-li"><i
class="fas fa-space-shuttle"></i></span>Shuttle-class
x701 space coach
<span class="fa-li"><i class="fab fa-grav"></i></span>Space walk experience
<div class="card-footer">
<a id="tier3" href="#"
class="btn btn-block btn-primary">Select class</a>

This HTML is also available in your Glitch app, in views/ > tier_3.html.

Wrapping the pricing page code in a flag

Now you wrap the HTML for the new pricing page in the flag you just created. This prevents anyone who is not targeted by the flag from receiving the new pricing page.

Use the following server.py code:

def pricing():
"""Displays the pricing page."""
# Implement the feature flag here
return render_template('pricing.html')

The code above shows the controller handler for the pricing page route to the web app. All this handler does is return the rendered pricing.html template to the user.

The following code sample shows an instruction to evaluate the flag, then pass the result of the evaluated flag into the page template:

def pricing():
"""Displays the pricing page."""
# Because this page doesn't require a logged in user,
# tell LaunchDarkly that this is an anonymous user.
user = {
"key": "anon",
"anonymous": True
is_tier_3_enabled = ld_client.variation('pricing-tier-3', user, False)
return render_template('pricing.html', is_tier_3_enabled=is_tier_3_enabled)

The controller passes the evaluated flag value to the template named is_tier_3_enabled.

Now update the pricing.html template to determine if the new pricing tier should be displayed.

The updated template should look like this:

{% extends "base.html" %}
{% block main %}
<section class="pricing py-5">
<div class="container">
<div class="row justify-content-center">
<div class="card-group">
{% include "tier_1.html" %}
{% include "tier_2.html" %}
{# Tier 3 is now controlled by a feature flag! #}
{% if is_tier_3_enabled == True %}
{% include "tier_3.html" %}
{% endif %}
{% endblock %}

After you've implemented the changes above, refresh the browser with your Glitch app in it.

Verify that the new tier is not enabled. The pricing page should not show the new pricing option yet.

Turning your feature on

Now that you've wrapped your feature in a flag, it's time to turn your feature on. This will make the new pricing option appear in your app.

To turn your feature on:

  1. In LaunchDarkly, navigate to the Feature flags list.
  2. Find the "Pricing tier 3" flag and click on the flag name. The flag's Targeting tab appears.
  3. Click the toggle to toggle the flag to On.
  4. Click Review and save.
  5. Refresh the browser with your Glitch app in it again to verify that the new pricing tier is available.

Here is a video showing how to turn on your feature:


In this tutorial, you learned to:

  • Add the LaunchDarkly SDK to your application
  • Create and use a feature flag
  • Test your feature flag to confirm that it works

You now have the basics you need to complete the next tutorial, Getting started: Flag rules and targeting.

Want to know more? Start a trial.

Your 14-day trial begins as soon as you sign up. Learn to use LaunchDarkly with the app's built-in tutorial. You'll discover how easy it is to manage the whole feature lifecycle from concept to launch to control.

Want to try it out? Start a trial.