LaunchDarkly API Client for Python
The `launchdarkly-api` library is an auto-generated Python client for the LaunchDarkly REST API. It enables programmatic interaction for custom integrations, data export, or automating feature flag workflows. It is currently at version 22.0.0 and undergoes frequent major version releases to align with updates to the underlying REST API, often introducing breaking changes.
Warnings
- breaking The `launchdarkly-api` client library undergoes frequent major version increments, typically indicating breaking changes in the underlying LaunchDarkly REST API. Always pin your dependency version (`launchdarkly-api==X.Y.Z`) and thoroughly review the changelog for migration instructions between versions.
- gotcha This `launchdarkly-api` client library is designed for programmatic interaction with the LaunchDarkly REST API (e.g., automation, data export, managing flag configurations). It is *not* for evaluating feature flags within your application. For feature flag evaluation, use the appropriate LaunchDarkly SDK (e.g., `launchdarkly-server-sdk` for Python applications).
- gotcha Only LaunchDarkly API access tokens (personal or service tokens) are valid for authentication with this client. Using SDK keys, mobile keys, or client-side IDs (which are designed for SDKs) will result in authentication failures when using the `launchdarkly-api` client.
- breaking Frequent API updates lead to changes in data models and endpoint behaviors. For example, in v19.0.0, the `status` field in `IterationRep` objects changed from a string to an enum, and in v22.0.0, the `guardrail` type was removed for `POST /api/v2/projects/{projectKey}/metric-groups`. Code relying on specific string values or previously available fields/types may break.
Install
-
pip install launchdarkly-api
Imports
- Configuration
import launchdarkly_api config = launchdarkly_api.Configuration(...)
- ApiClient
import launchdarkly_api api_client = launchdarkly_api.ApiClient(config)
- ProjectsApi
import launchdarkly_api projects_api = launchdarkly_api.ProjectsApi(api_client)
- ApiException
from launchdarkly_api.rest import ApiException
Quickstart
import os
import launchdarkly_api
from launchdarkly_api.rest import ApiException
# Configure API key authorization: ApiKey
configuration = launchdarkly_api.Configuration(
host="https://app.launchdarkly.com"
)
# Your LaunchDarkly API Key (NOT an SDK key)
configuration.api_key['ApiKey'] = os.environ.get('LAUNCHDARKLY_API_KEY', '')
# Create an instance of the API client
try:
with launchdarkly_api.ApiClient(configuration) as api_client:
# Create an instance of the Projects API class
api_instance = launchdarkly_api.ProjectsApi(api_client)
try:
# List all projects
projects = api_instance.get_projects()
if projects and projects.items:
print(f"Successfully retrieved {len(projects.items)} project(s):")
for project in projects.items:
print(f"- {project.name} (Key: {project.key})")
else:
print("No projects found or API key may be invalid/lack permissions.")
except ApiException as e:
print(f"Exception when calling ProjectsApi->get_projects: {e}")
if e.status == 401:
print("Error 401: Unauthorized. Check your LAUNCHDARKLY_API_KEY and its permissions.")
elif e.status == 403:
print("Error 403: Forbidden. Your API key might not have permissions to list projects.")
except ValueError as e:
print(f"Error initializing API client: {e}. Ensure LAUNCHDARKLY_API_KEY is set.")