We've built a simple console application that demonstrates how LaunchDarkly's SDK works.
Below, you'll find the build procedure. For more comprehensive instructions, you can visit your Quickstart page or the Python reference guide.
This demo requires Python 3.9 or higher.
Before running the application, you need to set up a feature flag in your LaunchDarkly dashboard.
- Flag Type: Boolean flag
- Flag Key:
sample-feature
(default) or any custom key you set inLAUNCHDARKLY_FLAG_KEY
- Flag Name: You can name it anything descriptive like "Sample Feature" or "Demo Feature"
Since this is a boolean flag, you need two variations:
- Variation 1:
true
(Boolean) - Variation 2:
false
(Boolean)
The program creates a specific user context that will appear in your LaunchDarkly dashboard:
- User Key:
example-user-key
- User Kind:
user
- User Name:
Sandy
-
Go to your LaunchDarkly dashboard and navigate to your project
-
Create a new flag:
- Click "Create Flag"
- Choose "Boolean" as the flag type
- Set the flag key to
sample-feature
(or your custom key) - Set the flag name to something like "Sample Feature"
-
Configure the flag variations:
- Variation 1:
true
(Boolean) - Variation 2:
false
(Boolean) - Set the default variation to
false
- Variation 1:
-
Set up targeting rules:
- For the user "Sandy" (key: example-user-key):
- Add a targeting rule that serves
true
to this specific user
- Add a targeting rule that serves
- For all other users:
- Serve
false
(or whatever you want as the default)
- Serve
- For the user "Sandy" (key: example-user-key):
-
Save and turn on the flag
Once you've created the flag, you can test it by running:
# Activate virtual environment
source venv/bin/activate
# Set your SDK key (replace with your actual key)
export LAUNCHDARKLY_SDK_KEY="your-actual-sdk-key-here"
# Optionally set a custom flag key (or leave it to use 'sample-feature')
export LAUNCHDARKLY_FLAG_KEY="sample-feature"
# Run the program
python main.py
- If the flag evaluates to
true
: You'll see the LaunchDarkly banner displayed - If the flag evaluates to
false
: You'll see the evaluation result but no banner - The program will wait for real-time changes: You can toggle the flag in LaunchDarkly and see the changes immediately
While the program is running:
- Go to your LaunchDarkly dashboard
- Find the flag you created
- Toggle it between
true
andfalse
- Watch the program output change in real-time
-
Create a
.env
file in the project root with your LaunchDarkly credentials:# Create .env file touch .env
-
Add your environment variables to the
.env
file:# LaunchDarkly Configuration LAUNCHDARKLY_SDK_KEY=your-sdk-key-here LAUNCHDARKLY_FLAG_KEY=sample-feature # Optional: Set to 'true' to run in CI mode (single evaluation) # CI=false
-
Install dependencies:
# Using pip (recommended) python3 -m venv venv source venv/bin/activate pip install -r requirements.txt # Or using Poetry poetry install
-
Run the application:
# Using pip source venv/bin/activate python main.py # Or using Poetry poetry run python main.py
-
Set the environment variable
LAUNCHDARKLY_SDK_KEY
to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, setLAUNCHDARKLY_FLAG_KEY
to the flag key; otherwise, a boolean flag ofsample-feature
will be assumed.export LAUNCHDARKLY_SDK_KEY="1234567890abcdef" export LAUNCHDARKLY_FLAG_KEY="my-boolean-flag"
-
Ensure you have Poetry installed.
-
Install the required dependencies with
poetry install
. -
On the command line, run
poetry run python main.py
You should receive the message "The feature flag evaluates to .". The application will run continuously and react to the flag changes in LaunchDarkly.
The .env
file is included in .gitignore
to prevent accidentally committing sensitive credentials to version control. Always keep your LaunchDarkly SDK keys secure and never commit them to public repositories.