New Relic NerdGraph API Client for Node.js
Tags: newrelic, nerdgraph, npm, api
Published: 2023-05-15 15:05:00

As software applications become more complex and distributed, monitoring and debugging them can become a significant challenge. [New Relic](https://newrelic.com/) is a well-known SaaS platform that offers monitoring, observability, and optimization solutions for modern software applications. [NerdGraph](https://docs.newrelic.com/docs/apis/nerdgraph/get-started/introduction-new-relic-nerdgraph/) is the GraphQL API of New Relic, and it provides developers with a more flexible and powerful way to interact with the New Relic platform.

To help you **leverage the power of NerdGraph in your Node.js applications**, I have recently created a Node.js API client for NerdGraph, called [newrelic-nerdgraph-client](https://www.npmjs.com/package/newrelic-nerdgraph-client). In this blog, I will walk you through the installation process and show you how to use the client to make **synchronous** and **asynchronous** NRQL queries.

## 📥 Installation

To install the `newrelic-nerdgraph-client`, simply run the following command:

```
npm i newrelic-nerdgraph-client
```

## 🔧 Usage

First, you need to obtain a [User API key](https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/) from New Relic. You can create one by logging into your New Relic account and navigating to **Account settings > API keys**.

After obtaining the API key, you can create an instance of `NerdGraph` by passing it as a parameter to the constructor:

```javascript
const NerdGraph = require('newrelic-nerdgraph-client');

const apiKey = '<YOUR_API_KEY_HERE>';
const client = new NerdGraph(apiKey);
```

### ⚙️ Sync Query

You can make a synchronous NRQL query using the `query` method:

```javascript
const options = {
  account: '<YOUR_ACCOUNT_ID_HERE>',
  query: 'SELECT * FROM Transaction SINCE 1 day ago'
};

client.query(options)
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.error(error);
  });
```

### ⚡ Async Query

NerdGraph also supports [asynchronous NRQL queries](https://docs.newrelic.com/docs/apis/nerdgraph/examples/async-queries-nrql-tutorial/). **Asynchronous queries run in the background, and you can make follow-up requests to retrieve query results or the query status**. This type of query avoids a query being interrupted by issues like browser timeouts or HTTP connection timeouts. It's especially **useful for running queries that may take a long time to complete**.

You can make an asynchronous NRQL query using the query method with the `async` option set to **true**:

```javascript
const options = {
  account: '<YOUR_ACCOUNT_ID_HERE>',
  query: 'SELECT * FROM Transaction SINCE 1 day ago',
  async: true
};

client.query(options)
  .then((data) => {
    const queryId = data?.queryId;

    if (!queryId) {
      // Poll the results using this queryId
    } else {
      console.log(data);
    }
  })
  .catch((error) => {
    console.error(error);
  });
```

#### 🔄 Polling Async Query

You can poll for the results of an asynchronous NRQL query using the `poll` method:

```javascript
const options = {
  account: '<YOUR_ACCOUNT_ID_HERE>',
  queryId: '<YOUR_QUERY_ID_HERE>'
};

client.poll(options)
  .then((data) => {
    if (data.queryId) {
      // Poll it again
    } else {
      console.log(data);
    } 
  })
  .catch((error) => {
    console.error(error);
  });
```

## 🚀 Seamlessly Interact with NerdGraph in Node.js

In conclusion, the [New Relic NerdGraph API Client](https://github.com/vinitshahdeo/newrelic-nerdgraph-client) provides a flexible way to interact with the NerdGraph GraphQL API of New Relic in your Node.js applications. Whether you need to **perform synchronous or asynchronous NRQL queries**, retrieve query results, or poll for the status of async queries, this client has you covered. With its easy-to-use interface, **support for promises and callbacks** integrating the New Relic NerdGraph API Client into your projects is a seamless experience.

For **detailed API documentation, please refer to the [GitHub repository](https://github.com/vinitshahdeo/newrelic-nerdgraph-client)**, and don't forget to check out the [package on NPM](https://www.npmjs.com/package/newrelic-nerdgraph-client) to get started.

Thank you.