Google provides a [Node.js](https://nodejs.org/) client library for interacting with
the Ad Manager API.

## Prerequisite

To use the Node.js client library, you must have Node.js installed. For more
information, see [Download Node.js®](https://nodejs.org/en/download).

## Install the client library

To get started, create a new project in the IDE of your choice or add the
dependency to an existing project. Google publishes client library artifacts to
npm as
[`@google-ads/admanager`](https://www.npmjs.com/package/@google-ads/admanager).

### package.json

```javascript
"dependencies": {
  "@google-ads/admanager": "^0.15.0"
}
```

### Command line

```bash
npm install @google-ads/admanager
```

## Configure credentials

To authenticate, the Node.js client library uses OAuth2 and Application Default
Credentials (ADC). For creating and configuring your ADC credentials, see
[Authentication](https://developers.google.com/ad-manager/api/beta/authentication.md.txt).

## Make your first request

Each service has a `ServiceClient` object with methods for each REST method.
For examples of each method, see the GitHub repository
[`googleapis/google-cloud-node`](https://github.com/googleapis/google-cloud-node/tree/main/packages/google-ads-admanager/samples/generated/v1).
The following example reads a
[`Network`](https://developers.google.com/ad-manager/api/beta/reference/rest/v1/networks.md.txt) object:

```javascript
// Resource name of the Network
const name = 'networks/NETWORK_CODE'

// Imports the Admanager library
const {NetworkServiceClient} = require('@google-ads/admanager').v1;

// Instantiates a client
const admanagerClient = new NetworkServiceClient();

async function callGetNetwork() {
  // Construct request
  const request = {
    name,
  };

  // Run request
  const response = await admanagerClient.getNetwork(request);
  console.log(response);
}

callGetNetwork();
```

## Log HTTP requests and responses

The Node.js client library supports logging HTTP requests and responses.
By default, the client library disables logging.

To enable default logging to standard output, set the environment variable
`GOOGLE_SDK_NODE_LOGGING` to a comma-separated list of Google API package names.
You can enable logging for all Google APIs by setting the variable value to
`all`. For all available client library package names, see
[Google Cloud Node.js Client Libraries](https://github.com/googleapis/google-cloud-node#google-cloud-nodejs-client-libraries)

### Node.js

```javascript
// Enable logging for the Google Ad Manager API
process.env.GOOGLE_SDK_NODE_LOGGING = 'admanager';

// Enable logging for the Google Ad Manager and pubsub APIs.
process.env.GOOGLE_SDK_NODE_LOGGING = 'admanager,pubsub';

// Enable logging for all Google APIs
process.env.GOOGLE_SDK_NODE_LOGGING = 'all';
```

### Linux or macOS

```bash
# Enable logging for the Google Ad Manager API.
export GOOGLE_SDK_NODE_LOGGING=admanager

# Enable logging for the Google Ad Manager and pubsub APIs.
export GOOGLE_SDK_NODE_LOGGING=admanager,pubsub

# Enable logging for all Google APIs
export GOOGLE_SDK_NODE_LOGGING=all
```

### Windows

```powershell
# Enable logging for the Google Ad Manager API.
set GOOGLE_SDK_NODE_LOGGING=admanager

# Enable logging for the Google Ad Manager and pubsub APIs.
set GOOGLE_SDK_NODE_LOGGING=admanager,pubsub

# Enable logging for all Google APIs
set GOOGLE_SDK_NODE_LOGGING=all
```

Alternatively, you can change the logging backend or hook log events.
For more information, see [Google Logging Tools](https://www.npmjs.com/package/google-logging-utils).

## Handle errors

In the Node.js client library, all Ad Manager API errors throw an exception of
type
[GaxiosError](https://cloud.google.com/nodejs/docs/reference/gaxios/latest/gaxios/gaxioserror).

Ad Manager API errors include an error message and a unique `requestId` value
you can provide to the API support team. For assistance with
troubleshooting, see [Contact API support](https://developers.google.com/ad-manager/api/beta/support). The
following example extracts the `requestId` value and error message:

```javascript
const admanagerClient = new NetworkServiceClient();
try {
  const network = admanagerClient.getNetwork(
    { name: 'networks/NETWORK_CODE' }
  );
  console.log(network);
} catch(e) {
  if (e instanceof GaxiosError) {
    // Load the error
    const apiError = JSON.parse(e.message).error;
    const requestInfoType = 'type.googleapis.com/google.rpc.RequestInfo';
    const requestInfo = apiError.details.find(detail => detail['@type'] === requestInfoType);
    console.error(apiError.status + ' - ' + apiError.message);
    console.error('RequestId: ' + requestInfo.requestId);
  } else {
    throw e;
  }
}
```