https://github.com/vinitshahdeo/newrelic-nerdgraph-client
An API client for NerdGraph—the GraphQL API of New Relic. Supports both synchronous and asynchronous NRQL queries.
https://github.com/vinitshahdeo/newrelic-nerdgraph-client
graphql nerdgraph newrelic newrelic-api nrql
Last synced: 18 days ago
JSON representation
An API client for NerdGraph—the GraphQL API of New Relic. Supports both synchronous and asynchronous NRQL queries.
- Host: GitHub
- URL: https://github.com/vinitshahdeo/newrelic-nerdgraph-client
- Owner: vinitshahdeo
- License: mit
- Created: 2023-05-12T10:55:41.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-14T20:12:18.000Z (about 3 years ago)
- Last Synced: 2025-10-04T21:32:18.329Z (8 months ago)
- Topics: graphql, nerdgraph, newrelic, newrelic-api, nrql
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/newrelic-nerdgraph-client
- Size: 43 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# New Relic NerdGraph API Client
A Node.js API Client for [NerdGraph](https://docs.newrelic.com/docs/apis/nerdgraph/examples/nerdgraph-nrql-tutorial/), the GraphQL API of New Relic that supports both synchronous and asynchronous NRQL queries. [Learn more about NerdGraph](https://docs.newrelic.com/docs/apis/nerdgraph/get-started/introduction-new-relic-nerdgraph/) or try it out — [api.newrelic.com/graphiql](https://api.newrelic.com/graphiql)
[](https://www.npmjs.com/package/newrelic-nerdgraph-client) [](https://docs.newrelic.com/docs/apis/nerdgraph/get-started/introduction-new-relic-nerdgraph/) 
## Installation
Install using npm:
```bash
npm i newrelic-nerdgraph-client
```
## Usage
First, you need to obtain a [User API key from New Relic](https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/). 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:
```js
const NerdGraph = require('newrelic-nerdgraph-api');
const apiKey = '';
const client = new NerdGraph(apiKey);
```
### Sync Query
You can make a synchronous NRQL query using the `query` method:
```js
const options = {
account: '',
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 query](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`:
```js
const options = {
account: '',
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:
```js
const options = {
account: '',
queryId: ''
};
client.poll(options)
.then((data) => {
if (data.queryId) {
// Poll it again
} else {
console.log(data);
}
})
.catch((error) => {
console.error(error);
});
```
### Callback
You can use a callback function instead of a promise by passing it as a second parameter:
```js
const options = {
account: '',
query: 'SELECT * FROM Transaction SINCE 1 day ago'
};
client.query(options, (error, data) => {
if (error) {
console.error(error);
} else {
console.log(data);
}
});
```
### Complete NRQL Response
You can get complete NRQL response with `completeResponse` option set to `true`:
```javascript
const options = {
account: '',
query: 'SELECT * FROM Transaction SINCE 1 day ago',
completeResponse: true
};
client.query(options)
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
```
Below is a sample NRQL response:
```json
{
"data": {
"actor": {
"account": {
"nrql": {
"results": []
}
}
}
}
}
```
## API Reference
> The API Reference is also available [here](https://vinitshahdeo.github.io/newrelic-nerdgraph-client/).
### `NerdGraph(apiKey: string)`
Creates a new instance of the NewRelicNerdGraphAPI class.
**Parameters:**
- `apiKey` The API key to use when making requests to New Relic.
### `query(options: Object, callback?: Function): Promise`
Calls the NerdGraph API with the provided options and either invokes the provided callback or returns a promise.
**Parameters:**
- `options` The options to use when calling the API.
- `options.account` The account ID to use when calling the API.
- `options.query` The NRQL query to execute.
- `options.async` Whether or not to make an asynchronous query.
- `callback` The optional callback function to invoke with the results of the query.
**Returns:**
- A promise that resolves with the results of the query if no callback is provided.
### `poll(options: Object, callback?: Function): Promise`
Polls a NerdGraph query using the specified options.
**Parameters:**
- `options` The options to use when calling the API.
- `options.account` The account ID to use when calling the API.
- `options.queryId` The query ID to poll.
- `callback` The optional callback function to invoke with the results of the query.
**Returns:**
- A promise that resolves with the results of the query if no callback is provided.
## Contributing
Contributions to [newrelic-nerdgraph-client](https://github.com/vinitshahdeo/newrelic-nerdgraph-client) are most welcome!
If you find a bug or want to suggest a new feature, please [open an issue](https://github.com/vinitshahdeo/newrelic-nerdgraph-client/issues/new) on the GitHub repository. If you want to contribute code, please [fork the repository](https://github.com/vinitshahdeo/newrelic-nerdgraph-client/fork), make your changes, and **submit a pull request**. Your contributions and feedback are most welcome!
## License
This library is authored by @[vinitshahdeo](https://github.com/vinitshahdeo) and released under the [MIT License](./LICENSE).
[](./LICENSE) [](https://twitter.com/Vinit_Shahdeo)