• HOME
  • INTEGRATIONS
  • SDK
  • API DOCS
No results for ""
EXPAND ALL
CLOSE
launchdarkly.com

EDIT ON GITHUB

Python SDK reference

Read time: 6 minutes
Last edited: Feb 3, 2020

This reference guide documents all of the methods available in our Python SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source. To learn more, read Python SDK GitHub repository. The online Python API docs contain the programmatic definitions of every class and method. Additionally you can clone and run a sample application using this SDK.

Getting started

Building on top of our Getting Started guide guide, the following steps will get you started with using the LaunchDarkly SDK in your Python application.

The first step is to install the LaunchDarkly SDK as a dependency in your application using your application's dependency manager. Refer to the SDK releases page to identify the latest version if you want to depend on a specific version.

1pip install launchdarkly-server-sdk

Next you should import the LaunchDarkly client in your application code.

1import ldclient

Once the SDK is installed and imported, you'll want to create a single, shared instance of ldclient. The get() function enforces the singleton pattern; you should only have one instance of the client in your application. You should specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

1ldclient.set_sdk_key("YOUR_SDK_KEY")
2
3ld_client = ldclient.get()

Using ld_client, you can check which variation a particular user should receive for a given feature flag.

1show_feature = ld_client.variation("your.flag.key", {"key": "user@test.com"}, False)
2
3if show_feature:
4 # application code to show the feature
5else:
6 # the code to run if the feature is off

Lastly, when your application is about to terminate, shut down ld_client. This 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. This is something you only need to do once.

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

Customizing your client

You can also pass custom parameters to the client by creating a custom configuration object:

1ldclient.set_config(Config(sdk_key='YOUR_SDK_KEY', connect_timeout=5))

The Config allows you to specify a variety of options. Most likely the defaults will work just fine. The complete list of customizable parameters is as follows:

ParameterTypeDescriptionDefault value
base_uristringSets the base URL of the LaunchDarkly server for this configuration.https://app.launchdarkly.com
events_uristringSet the events URL of the LaunchDarkly server for this configuration.https://events.launchdarkly.com
stream_uristringSets the stream URL of the LaunchDarkly server for this configuration.https://stream.launchdarkly.com/
connect_timeoutintegerSet the connection timeout for the configuration.10 seconds
read_timeoutintegerThe timeout when reading data from feature flag requests.15 seconds
events_upload_max_batch_sizeintegerThe maximum amount of events to be sent in a single request.10000
flush_intervalintegerThe number of seconds in between flushes of the events buffer.
Decreasing the flush interval means that the event buffer is less likely to reach capacity.
events_max_pendingintegerSets the capacity of the events buffer.10000
streambooleanBoolean value which enables streaming.True
offlinebooleanSet whether this client is offline.False
poll_intervalintegerSet the polling interval (when streaming is disabled).1
use_lddbooleanIf true, the SDK will rely on LDD for feature updates. See Deployment options for more details.False
feature_storeInMemoryFeatureStore or RedisFeatureStoreSets the feature flag store to be used by the client. By default, flags are stored in memory, with an option to use a redis feature store.An InMemoryFeatureStore
feature_requester_class(str, Config, FeatureStore) -> FeatureRequesterA factory for a FeatureRequester implementation taking the sdk key and configNone
event_consumer_class(queue.Queue, str, Config) -> EventConsumerA factory for an EventConsumer implementation taking the event queue, sdk key, and configNone
update_processor_class(str, Config, FeatureStore) -> UpdateProcessorA factory for an UpdateProcessor implementation taking the sdk key, config, and FeatureStore implementationNone
all_attributes_privateboolWhether all user attributes (except the user key) should be marked as Private user attributes, and not sent to LaunchDarkly.False
all_attribute_namesstringMust be a list of strings. The names of user attributes that should be marked as Private user attributes, and not sent to LaunchDarkly.[]
user_keys_capacityintegerThe number of user keys that the event processor can remember at any one time, so that duplicate user details will not be sent in analytics events.1000
user_keys_flush_intervalintegerThe interval (in seconds) at which the event processor will reset its set of known user keys.300
inline_users_in_eventsboolWhether to include full user details in every analytics event. By default, events will only include the user key, except for one "index" event that provides the full details for the user.False

Users

Feature flag targeting and rollouts are all determined by the user you pass to your Variation calls. In our Python SDK, users are simply dictionaries. Here's an example:

1user = {
2 "key": "aa0ceb",
3 "firstName": "Ernestina",
4 "lastName": "Evans",
5 "email": "ernestina@example.com",
6 "custom": {
7 "groups": ["Google", "Microsoft"]
8 }
9}

Let's walk through this snippet. The most important attribute is the user key-- in this case we've used the hash "aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an e-mail address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like firstName, email, and the custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

Besides the key, LaunchDarkly supports the following attributes at the "top level". Remember, all of these are optional:

  • ip: Must be an IP address.
  • firstName: Must be a string. If you provide a first name, you can search for users on the Users page by name.
  • lastName: Must be a string. If you provide a last name, you can search for users on the Users page by name.
  • country: Must be a string representing the country associated with the user.
  • email: Must be a string representing the user's e-mail address. If an avatar URL is not provided, we'll use Gravatar to try to display an avatar for the user on the Users page.
  • avatar: Must be an absolute URL to an avatar image for the user.
  • name: Must be a string. You can search for users on the User page by name
  • anonymous: Must be a boolean. See the section below on anonymous users for more details.

In addition to built-in attributes, you can pass us any of your own user data by passing custom attributes, like the groups attribute in the example above.

A note on types
Most of our built-in attributes (like names and e-mail addresses) expect string values. Custom attribute values can be strings, booleans (like True or False), numbers, or lists of strings, booleans or numbers. If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us-- organizations, groups, account plans-- anything you pass to us becomes available instantly on our dashboard.

Private user attributes

You can optionally configure the Python SDK to treat some or all user attributes as Private user attribute. Private user attributes can be used for targeting purposes, but are removed from the user data sent back to LaunchDarkly.

In the Python SDK there are two ways to define private attributes for the entire LaunchDarkly client:

  • In the LaunchDarkly config, you can set all_attributes_private to true. If this is enabled, all user attributes (except the key) for all users are removed before the user is sent to LaunchDarkly.
  • In the LaunchDarkly config object, you can define a list of private_attribute_names. If any user has a custom or built-in attribute named in this list, it will be removed before the user is sent to LaunchDarkly.

You can also define a set of privateAttributeNames on the user object itself. For example:

1user = {
2 "key": "aa0ceb",
3 "email": "test@example.com",
4 "privateAttributeNames": ["email"]
5}

When this user is sent back to LaunchDarkly, the email attribute will be removed.

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

1user = {"key":"aa0ceb", "anonymous": True}

You will still need to generate a unique key for anonymous users-- session IDs or UUIDs work best for this. Anonymous users still count toward your plan's MAU limit, so make sure you capture and reuse your anonymous user's key between requests.

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. You also can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. This is actually a good thing-- it keeps anonymous users from polluting your Users page!

Variation

The variation method determines which variation of a feature flag a user receives.

1ldclient.get().variation("your.feature.key", user, False)

variation calls take the feature flag key, an LDUser, and a default value.

The default value will only be returned if an error is encountered-- for example, if the feature flag key doesn't exist or the user doesn't have a key specified.

The variation call will automatically create a user in LaunchDarkly if a user with that user key doesn't exist already. There's no need to create users ahead of time (but if you do need to, take a look at Identify).

Track

The track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. Here's a simple example:

1ldclient.get().track("your-goal-key", user)

You can also attach an extra dictionary containing arbitrary data to your event:

1ldclient.get().track("Completed purchase", user, {"price": 320})

Identify

The identify method creates or updates users on LaunchDarkly, making them available for targeting and autocomplete on the dashboard. In most cases, you won't need to call identify-- the variation call will automatically create users on the dashboard for you. identify can be useful if you want to pre-populate your dashboard before launching any features.

1ldclient.get().identify(user)

All Flags

Creating users

Unlike variation and identify calls, all_flags_state does not send events to LaunchDarkly. Thus, users are not created or updated in the LaunchDarkly dashboard.

The all_flags_state method captures the state of all feature flag keys with regard to a specific user. This includes their values, as well as other metadata.

This method can be useful for passing feature flags to your front-end. In particular, it can be used to provide bootstrap flag settings for our JavaScript SDK.

1state = ldclient.get().all_flags_state(user)

Secure mode hash

The SecureModeHash method computes an HMAC signature of a user signed with the client's SDK key. If you're using our JavaScript SDK for client-side flags, this method generates the signature you need for secure mode.

1ldclient.get().secure_mode_hash(user)

Offline mode

In some situations, you might want avoid remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. You can do this by setting offline mode in the client's Config.

1# Initialization:
2ldclient.config = Config(offline = True)
3
4ldclient.sdk_key = "YOUR_SDK_KEY"
5
6ldclient.get().variation("any.feature.flag", user, False) # will always return the default value (false)

Flush

Internally, the LaunchDarkly SDK keeps an event buffer for variation, track, and identify calls. These are flushed periodically in a background thread. In some situations (for example, if you're testing out the SDK in a REPL), you may want to manually call flush to process events immediately. Otherwise Python may close before flushing the event buffer and your user changes and tracks will not be lost.

1ldclient.get().flush()

Note that the flush interval is configurable-- if you need to change the interval, you can do so when configuring your client instance.

Close

Close safely shuts down the client instance and releases all resources associated with the client. In most long-running applications, you should not have to call close.

1ldclient.get().close()

Configuring uWSGI

The LaunchDarkly SDK is compatible with uWSGI. However, in uWSGI environments, the SDK requires the enable-threads option to be set.

Logging

The Python SDK uses Python's built-in logging library. All loggers are namespaced under ldclient.util. For an example configuration check out the hello-python project.

Be aware of two considerations when enabling the DEBUG log level:

  1. Debug-level logs can be very verbose. It is not recommended that you turn on debug logging in high-volume environments.
  2. Potentially sensitive information is logged including LaunchDarkly users created by you in your usage of this SDK.

HTTPS proxy

Python's standard HTTP library provides a built-in HTTPS proxy. If the HTTPS_PROXY environment variable is present then the SDK will proxy all network requests through the URL provided.

How to set the HTTPS_PROXY environment variable on Mac/Linux systems:

1export HTTPS_PROXY=https://web-proxy.domain.com:8080

How to set the HTTPS_PROXY environment variable on Windows systems:

1set HTTPS_PROXY=https://web-proxy.domain.com:8080

Or it can be set from within python:

1os.environ["https_proxy"] = "https://web-proxy.domain.com:8080"

Using a database as a persistent store

The Python SDK supports Redis, Consul, or DynamoDB as a persistent store of feature flag configurations. To learn more, read Using a persistent feature store.