Datadog API Client for Python

raw JSON →
2.52.0 verified Tue May 12 auth: no python install: verified

The `datadog-api-client` is the official Python client library for interacting with the Datadog API. It provides a structured, object-oriented interface to programmatically interact with all aspects of the Datadog platform, including metrics, monitors, events, and dashboards. The library is actively maintained with frequent minor releases, currently at version 2.52.0.

pip install datadog-api-client
error datadog_api_client.exceptions.UnauthorizedException
cause This error occurs when the Datadog API Key and/or Application Key are either missing, incorrect, or do not have the necessary permissions for the requested API operation.
fix
Ensure you have set the DD_API_KEY and DD_APP_KEY environment variables with valid keys, or explicitly configure them in your Configuration object:
from datadog_api_client import Configuration, ApiClient

configuration = Configuration()
configuration.api_key['apiKeyAuth'] = 'YOUR_DATADOG_API_KEY'
configuration.api_key['appKeyAuth'] = 'YOUR_DATADOG_APP_KEY'

# Or, relying on environment variables (default behavior)
# configuration = Configuration()

with ApiClient(configuration) as api_client:
    # Your API calls here
    pass
error ModuleNotFoundError: No module named 'datadog_api_client'
cause The `datadog-api-client` library is not installed in your current Python environment, or the environment where your script is running does not have access to the installed package.
fix
Install the library using pip:
pip install datadog-api-client
If you need async support, install with the async extra:
pip install datadog-api-client[async]
error datadog_api_client.exceptions.ApiTypeError
cause This exception is raised when the arguments provided to an API method do not match the expected data types or structure defined by the API specification, often due to missing required fields or incorrect parameter types for complex objects.
fix
Review the official datadog-api-client documentation for the specific API endpoint and model you are using. Ensure all required parameters are supplied and that their values adhere to the expected data types and formats (e.g., providing a string where an enum is expected, or missing a required field in a body object). For example:
from datadog_api_client.v1.model.monitor import Monitor
from datadog_api_client.v1.model.monitor_type import MonitorType

# Ensure all required fields for Monitor are present and correctly typed
body = Monitor(
    name="My Example Monitor",
    type=MonitorType("metric alert"), # Correct enum usage
    query='avg(system.cpu.user{host:my-host}) > 0.5',
    message="Alert message @pagerduty",
    tags=["env:prod", "team:backend"],
    priority=3,
    # Other optional but correctly typed fields
)
gotcha Always use `DD_API_KEY` and `DD_APP_KEY` environment variables for authentication to avoid exposing credentials in code. While in-code configuration is possible, it is less secure.
fix Store `DD_API_KEY` and `DD_APP_KEY` as environment variables. The client automatically picks them up, or you can explicitly set `configuration.api_key` from `os.environ.get()`.
deprecated Many V1 API endpoints are deprecated and have V2 equivalents. While V1 might still work, it's recommended to use V2 endpoints for new development and migrate existing code where possible to ensure access to the latest features and avoid future breaking changes.
fix Consult the official Datadog API documentation to identify the V2 equivalent for your desired functionality and update your imports and API calls accordingly (e.g., `from datadog_api_client.v2.api.metrics_api import MetricsApi`).
gotcha The default Datadog API site is `datadoghq.com` (US). If your Datadog account is hosted in a different region (e.g., EU, US1-FED), you must explicitly configure the client to use the correct regional endpoint. Otherwise, API calls will fail or target the wrong Datadog instance.
fix Set the `DD_SITE` environment variable (e.g., `DD_SITE=datadoghq.eu`) or configure it in code: `configuration.server_variables["site"] = "datadoghq.eu"`.
breaking The client includes access to 'unstable' API endpoints that are subject to breaking changes without major version bumps. These endpoints require an explicit opt-in configuration step.
fix Enable unstable operations with `configuration.unstable_operations["<OperationName>"] = True`. Be aware that code using these endpoints may break in future minor releases.
gotcha Datadog APIs are subject to rate limits. Failing to implement retry and backoff logic can lead to API call failures (e.g., HTTP 429 Too Many Requests) and degraded application performance.
fix Implement robust retry mechanisms with exponential backoff. The library can be configured with a custom retry policy using `urllib3.util.Retry`.
gotcha There are two distinct Python libraries for Datadog: `datadog` (the older client, `pip install datadog`) and `datadog-api-client` (the newer, generated client, `pip install datadog-api-client`). Ensure you are importing and using the correct library for your needs, as they have different interfaces and capabilities. `datadog-api-client` supports all public Datadog API endpoints.
fix For comprehensive API coverage and the latest features, use `datadog-api-client`. If you are migrating, be aware of the different import paths and API call patterns.
gotcha There have been reports of memory not being released after API calls, potentially leading to increased memory usage in long-running applications.
fix Ensure `ApiClient` is used as a context manager (`with ApiClient(...) as api_client:`), which handles connection closing. For persistent issues, consider explicitly calling garbage collection (`gc.collect()`) after intensive API operations or analyzing memory usage patterns.
gotcha When constructing API parameters like Datadog query strings, which often contain special characters (e.g., quotes, curly braces), ensure proper Python string literal syntax. Unescaped quotes within a string literal can lead to `SyntaxError` before the API call is even made, preventing your script from running.
fix Correct the string literal by using consistent outer quotes (single or double) and escaping any inner quotes that match the outer ones (e.g., `query='avg(system.cpu.user{host:"test-host"}) by {host} > 80'`). Alternatively, use triple quotes (single or double) to define strings that can contain both single and double quotes without explicit escaping (e.g., `query="""avg(system.cpu.user{host:"test-host"}) by {host} > 80"""`).
breaking A `SyntaxError: invalid syntax` can occur when constructing query strings that contain nested quotes (e.g., `{host:"test-host"}`). Python's string literal rules interpret the inner quote prematurely as the end of the string, leading to unparsable code.
fix To resolve `SyntaxError` with nested quotes, use different quote types for the outer string and the inner content (e.g., `query='avg(system.cpu.user{host:"test-host"}) by {host} > 80'`), or escape the inner quotes (e.g., `query="avg(system.cpu.user{host:\"test-host\"}) by {host} > 80"`). For Python 3.6+, f-strings can also be used with careful escaping of literal curly braces (e.g., `query=f"avg(system.cpu.user{{host:\"test-host\"}}) by {{host}} > 80"`).
pip install datadog-api-client[async]
python os / libc variant status wheel install import disk
3.10 alpine (musl) datadog-api-client wheel - 0.34s 82.3M
3.10 alpine (musl) datadog-api-client - - 0.34s 77.3M
3.10 alpine (musl) async wheel - 0.33s 84.9M
3.10 alpine (musl) async - - 0.35s 79.9M
3.10 slim (glibc) datadog-api-client wheel 5.6s 0.23s 83M
3.10 slim (glibc) datadog-api-client - - 0.23s 78M
3.10 slim (glibc) async wheel 6.2s 0.23s 85M
3.10 slim (glibc) async - - 0.27s 80M
3.11 alpine (musl) datadog-api-client wheel - 0.46s 86.0M
3.11 alpine (musl) datadog-api-client - - 0.52s 80.9M
3.11 alpine (musl) async wheel - 0.44s 88.6M
3.11 alpine (musl) async - - 0.60s 83.5M
3.11 slim (glibc) datadog-api-client wheel 5.6s 0.45s 87M
3.11 slim (glibc) datadog-api-client - - 0.38s 81M
3.11 slim (glibc) async wheel 6.1s 0.40s 89M
3.11 slim (glibc) async - - 0.38s 84M
3.12 alpine (musl) datadog-api-client wheel - 0.42s 77.3M
3.12 alpine (musl) datadog-api-client - - 0.42s 72.2M
3.12 alpine (musl) async wheel - 0.41s 79.8M
3.12 alpine (musl) async - - 0.42s 74.9M
3.12 slim (glibc) datadog-api-client wheel 5.7s 0.44s 78M
3.12 slim (glibc) datadog-api-client - - 0.40s 73M
3.12 slim (glibc) async wheel 6.2s 0.42s 80M
3.12 slim (glibc) async - - 0.40s 75M
3.13 alpine (musl) datadog-api-client wheel - 0.40s 76.9M
3.13 alpine (musl) datadog-api-client - - 0.40s 71.7M
3.13 alpine (musl) async wheel - 0.41s 79.4M
3.13 alpine (musl) async - - 0.40s 74.3M
3.13 slim (glibc) datadog-api-client wheel 6.1s 0.40s 77M
3.13 slim (glibc) datadog-api-client - - 0.38s 72M
3.13 slim (glibc) async wheel 6.1s 0.40s 80M
3.13 slim (glibc) async - - 0.39s 75M
3.9 alpine (musl) datadog-api-client wheel - 0.31s 81.7M
3.9 alpine (musl) datadog-api-client - - 0.31s 76.7M
3.9 alpine (musl) async wheel - 0.29s 84.4M
3.9 alpine (musl) async - - 0.31s 79.4M
3.9 slim (glibc) datadog-api-client wheel 6.3s 0.30s 82M
3.9 slim (glibc) datadog-api-client - - 0.26s 77M
3.9 slim (glibc) async wheel 7.1s 0.29s 85M
3.9 slim (glibc) async - - 0.26s 80M