Datadog API Client for Python
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.
Warnings
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- gotcha There have been reports of memory not being released after API calls, potentially leading to increased memory usage in long-running applications.
Install
-
pip install datadog-api-client -
pip install datadog-api-client[async]
Imports
- ApiClient
from datadog_api_client import ApiClient
- Configuration
from datadog_api_client import Configuration
- MonitorsApi
from datadog_api_client.v1.api.monitors_api import MonitorsApi
- MetricsApi
from datadog_api_client.v2.api.metrics_api import MetricsApi
- Monitor
from datadog_api_client.v1.model.monitor import Monitor
- datadog
Quickstart
import os
from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v1.api.monitors_api import MonitorsApi
from datadog_api_client.v1.model.monitor import Monitor
from datadog_api_client.v1.model.monitor_type import MonitorType
# Configure API keys, preferably via environment variables
DD_API_KEY = os.environ.get('DD_API_KEY', '')
DD_APP_KEY = os.environ.get('DD_APP_KEY', '')
configuration = Configuration()
configuration.api_key['apiKeyAuth'] = DD_API_KEY
configuration.api_key['appKeyAuth'] = DD_APP_KEY
# For non-US regions, set the site explicitly:
# configuration.server_variables["site"] = "datadoghq.eu" # Example for EU site
try:
with ApiClient(configuration) as api_client:
api_instance = MonitorsApi(api_client)
# Create a basic monitor example
body = Monitor(
name="Example Monitor - Python Client",
type=MonitorType("metric alert"),
query="avg(system.cpu.user{host:"test-host"}) by {host} > 80",
message="CPU usage is high! @pagerduty",
tags=["env:dev", "team:backend"],
priority=3,
)
response = api_instance.create_monitor(body=body)
print(f"Monitor created successfully: {response.name} (ID: {response.id})")
except Exception as e:
print(f"Error creating monitor: {e}")