What is @datadog/datadog-api-client?
@datadog/datadog-api-client is an official Node.js client library for interacting with the Datadog API. It allows developers to programmatically manage and interact with Datadog's monitoring, logging, and tracing services. This includes creating and managing dashboards, monitors, events, and more.
What are @datadog/datadog-api-client's main functionalities?
Create a Dashboard
This code sample demonstrates how to create a new dashboard in Datadog using the @datadog/datadog-api-client package.
const { v1: datadog } = require('@datadog/datadog-api-client');
const configuration = datadog.createConfiguration();
const apiInstance = new datadog.DashboardsApi(configuration);
const params = {
body: {
title: 'Example-Dashboard',
widgets: [],
layoutType: 'ordered',
description: 'A sample dashboard',
isReadOnly: false,
notifyList: []
}
};
apiInstance.createDashboard(params).then((data) => {
console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}).catch((error) => {
console.error(error);
});
Create a Monitor
This code sample demonstrates how to create a new monitor in Datadog using the @datadog/datadog-api-client package.
const { v1: datadog } = require('@datadog/datadog-api-client');
const configuration = datadog.createConfiguration();
const apiInstance = new datadog.MonitorsApi(configuration);
const params = {
body: {
name: 'Example-Monitor',
type: 'metric alert',
query: 'avg(last_5m):avg:system.cpu.user{host:host0} > 75',
message: 'CPU usage is high on host0',
tags: ['env:production'],
options: {
thresholds: {
critical: 75
}
}
}
};
apiInstance.createMonitor(params).then((data) => {
console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}).catch((error) => {
console.error(error);
});
Post an Event
This code sample demonstrates how to post a new event to Datadog using the @datadog/datadog-api-client package.
const { v1: datadog } = require('@datadog/datadog-api-client');
const configuration = datadog.createConfiguration();
const apiInstance = new datadog.EventsApi(configuration);
const params = {
body: {
title: 'Example-Event',
text: 'An example event for Datadog',
tags: ['env:production'],
alertType: 'info'
}
};
apiInstance.createEvent(params).then((data) => {
console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}).catch((error) => {
console.error(error);
});
Other packages similar to @datadog/datadog-api-client
newrelic
The 'newrelic' package is the official Node.js client for New Relic, a competitor to Datadog. It provides similar functionalities for monitoring and managing application performance, but is specific to New Relic's ecosystem.
prom-client
The 'prom-client' package is a client for Prometheus, an open-source monitoring and alerting toolkit. While it doesn't offer the same breadth of features as Datadog, it is widely used for metrics collection and monitoring in cloud-native environments.
elastic-apm-node
The 'elastic-apm-node' package is the official Node.js agent for Elastic APM, part of the Elastic Stack. It provides performance monitoring and error tracking for Node.js applications, similar to Datadog's APM features.
Node.js Datadog API Client
This repository contains a Node.js API client for the Datadog API.
The code is generated using openapi-generator
and apigentools.
How to install
The package is under @datadog/datadog-api-client and can be installed through NPM or Yarn:
npm install @datadog/datadog-api-client
yarn add @datadog/datadog-api-client
Getting Started
Here's an example getting a monitor:
import { client, v1 } from '@datadog/datadog-api-client';
const configuration = client.createConfiguration();
const apiInstance = new v1.MonitorsApi(configuration);
let params:v1.MonitorsApiGetMonitorRequest = {
monitorId: 1,
};
apiInstance.getMonitor(params).then((data: v1.Monitor) => {
console.log('API called successfully. Returned data: ' + data);
}).catch((error:any) => console.error(error));
Authentication
By default the library will use the DD_API_KEY
and DD_APP_KEY
environment variables to authenticate against the Datadog API.
To provide your own set of credentials, you need to set the appropriate keys on the configuration:
import { client } from '@datadog/datadog-api-client';
const configurationOpts = {
authMethods: {
apiKeyAuth: "<API KEY>",
appKeyAuth: "<APPLICATION KEY>"
},
};
const configuration = client.createConfiguration(configurationOpts);
Unstable Endpoints
This 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.unstableOperations["<operationName>"] = true
where is the name of the method used to interact with that endpoint. For example: listLogIndexes
, or getLogsIndex
.
Changing Server
When talking to a different server, like the eu
instance, change the server variables:
import { client } from '@datadog/datadog-api-client';
const configuration = client.createConfiguration();
client.setServerVariables(configuration, {
site: "datadoghq.eu"
});
Disable compressed payloads
If you want to disable GZIP compressed responses, set the compress
flag
on your configuration options:
import { client } from '@datadog/datadog-api-client';
const configurationOpts = {
httpConfig: {
compress: false
},
};
const configuration = client.createConfiguration(configurationOpts);
Enable requests logging
If you want to enable requests logging, set the debug
flag on your configuration object:
import { client } from '@datadog/datadog-api-client';
const configurationOpts = {
debug: true
};
const configuration = client.createConfiguration(configurationOpts);
Adding timeout to requests
To add timeout or other mechanism to cancel requests, you need an abort
controller, for example the one implemented by
abort-controller. You can
then pass the `signal method to the HTTP configuration options:
import { client, v1 } from '@datadog/datadog-api-client';
import AbortController from 'abort-controller';
const controller = new AbortController();
const timeout = setTimeout(
() => { controller.abort(); },
1000,
);
const configurationOpts = {
httpConfig: {
signal: controller.signal
},
};
const configuration = client.createConfiguration(configurationOpts);
const apiInstance = new v1.MonitorsApi(configuration);
apiInstance.listMonitors().then((data: v1.Monitor[]) => {
console.log('API called successfully. Returned data: ' + data);
}).catch((error:any) => console.error(error)).finally(() => clearTimeout(timeout));
Several listing operations have a pagination method to help consume all the items available.
For example, to retrieve all your incidents:
import { client, v2 } from "@datadog/datadog-api-client";
async function main() {
const configuration = client.createConfiguration();
configuration.unstableOperations["listIncidents"] = true;
const apiInstance = new v2.IncidentsApi(configuration);
for await (const incident of apiInstance.listIncidentsWithPagination()) {
console.log("Got incident " + incident.id);
}
}
main();
Documentation
Documentation for API endpoints can be found in in Github pages.
Contributing
As most of the code in this repository is generated, we will only accept PRs for files
that are not modified by our code-generation machinery (changes to the generated files
would get overwritten). We happily accept contributions to files that are not autogenerated,
such as tests and development tooling.
Author
support@datadoghq.com
License
Apache License, v2.0