Package: datadog-api-client
datadog-api-client-pythonThis repository contains a Python API client for theDatadog API.RequirementsBuilding and using the API client library requiresPython 3.7+.InstallationTo install the API client library, simply execute:pipinstalldatadog-api-clientGetting StartedPlease follow theinstallationinstruction and execute the following Python code:fromdatadog_api_clientimportApiClient,Configurationfromdatadog_api_client.v1.api.monitors_apiimportMonitorsApifromdatadog_api_client.v1.model.monitorimportMonitorfromdatadog_api_client.v1.model.monitor_typeimportMonitorTypebody=Monitor(name="example",type=MonitorType("log alert"),query='logs("service:foo AND type:error").index("main").rollup("count").by("source").last("5m") > 2',message="some message Notify: @hipchat-channel",tags=["test:example","env:ci"],priority=3,)configuration=Configuration()withApiClient(configuration)asapi_client:api_instance=MonitorsApi(api_client)response=api_instance.create_monitor(body=body)print(response)AuthenticationBy default the library will use theDD_API_KEYandDD_APP_KEYenvironment variables to authenticate against the Datadog API.
To provide your own set of credentials, you need to set some keys on the configuration:configuration.api_key["apiKeyAuth"]="<API KEY>"configuration.api_key["appKeyAuth"]="<APPLICATION KEY>"Unstable EndpointsThis client includes access to Datadog API endpoints while they are in an unstable state and may undergo breaking changes. An extra configuration step is required to enable these endpoints:configuration.unstable_operations["<OperationName>"]=Truewhere<OperationName>is the name of the method used to interact with that endpoint. For example:list_log_indexes, orget_logs_indexChanging ServerWhen talking to a different server, like theeuinstance, change theserver_variableson your configuration object:configuration.server_variables["site"]="datadoghq.eu"Disable compressed payloadsIf you want to disable GZIP compressed responses, set thecompressflag
on your configuration object:configuration.compress=FalseEnable requests loggingIf you want to enable requests logging, set thedebugflag on your configuration object:configuration.debug=TrueEnable retryIf you want to enable retry when getting status code429rate-limited, setenable_retrytoTrueconfiguration.enable_retry=TrueThe default max retry is3, you can change it withmax_retriesconfiguration.max_retries=5Configure proxyYou can configure the client to use proxy by setting theproxykey on configuration object:configuration.proxy="http://127.0.0.1:80"Threads supportYou can run API calls in a thread by usingThreadedApiClientin place ofApiClient. API calls will then
return aAsyncResultinstance on which you can call get to retrieve the result:fromdatadog_api_clientimportConfiguration,ThreadedApiClientfromdatadog_api_client.v1.api.dashboards_apiimportDashboardsApiconfiguration=Configuration()withThreadedApiClient(configuration)asapi_client:api_instance=DashboardsApi(api_client)result=api_instance.list_dashboards()dashboards=result.get()print(dashboards)Asyncio supportThe library supports asynchronous operations when usingAsyncApiClientfor the transport. When that client is used,
the API methods will then return coroutines that you can wait for.To make async support available, you need to install the extraasyncqualifiers during installation:pip install datadog-api-client[async].importasynciofromdatadog_api_clientimportConfiguration,AsyncApiClientfromdatadog_api_client.v1.api.dashboards_apiimportDashboardsApiasyncdefmain():configuration=Configuration()asyncwithAsyncApiClient(configuration)asapi_client:api_instance=DashboardsApi(api_client)dashboards=awaitapi_instance.list_dashboards()print(dashboards)asyncio.run(main())PaginationSeveral listing operations have a pagination method to help consume all the items available.
For example, to retrieve all your incidents:fromdatadog_api_clientimportApiClient,Configurationfromdatadog_api_client.v2.api.incidents_apiimportIncidentsApiconfiguration=Configuration()configuration.unstable_operations["list_incidents"]=TruewithApiClient(configuration)asapi_client:api_instance=IncidentsApi(api_client)forincidentinapi_instance.list_incidents_with_pagination():print(incident.id)Documentation for API Endpoints and ModelsDocumentation for API endpoints and models are available onreadthedocs.Documentation for AuthorizationAuthenticate with the API by providing your API and Application keys in the configuration:configuration.api_key["apiKeyAuth"]="YOUR_API_KEY"configuration.api_key["appKeyAuth"]="YOUR_APPLICATION_KEY"Authorsupport@datadoghq.com