# Configuring the Node.js SDK

```ts
import { WatchTower15 } from '@watchtower15/sdk';

const wt15 = new WatchTower15({ apiKey: process.env.MY_KEY, timeout: 10_000 });
const fromEnv = WatchTower15.fromEnvironment({ maxRetries: 4 });
```

`fromEnvironment(overrides?)` reads five variables from `process.env`, each the first non-empty value after trimming, an override winning. The constructor reads no environment, so a test never picks a credential up by accident.

| Variable | Option | Default |
| --- | --- | --- |
| `WT15_API_KEY` | `apiKey` | none |
| `WT15_METRICS_TOKEN` | `metricsToken` | none |
| `WT15_LOGS_TOKEN` | `logsToken` | none |
| `WT15_API_URL` | `apiUrl` | `https://rest.watchtower15.com/v1` |
| `WT15_INGEST_URL` | `ingestUrl` | `https://api.watchtower15.com` |

## Options

| Option | Meaning | Bounds |
| --- | --- | --- |
| `apiKey`, `metricsToken`, `logsToken` | the credentials; `metricsToken` may be a function returning a token, or a promise of one, called only when a request is about to be sent with it and kept for at most 300 s | each checked locally |
| `apiUrl`, `ingestUrl` | the base URLs | `https`, or `http` for `localhost`, `127.0.0.1` and `[::1]` only; no user-info, query or fragment; a trailing `/` is dropped |
| `timeout` | the whole request’s limit, in milliseconds (15,000 by default) | above 0, at most 300,000 |
| `maxRetries` | how many times a failed request is sent again (2 by default) | 0 to 10 |
| `fetch` | a `fetch` to send through | a function |
| `logger` | an object with `debug`, `info`, `warn` and `error` (`console` fits); each retry is logged at `info`, a deprecation at `warn` | none logged when absent |
| `appInfo` | `name/version` of your application, added to the `User-Agent` | letters, digits and `._+-` |
| `dangerouslyAllowBrowser` | lets a client be built where `window.document` exists | off |

An option out of its bounds, an unknown option or a malformed credential throws a `ConfigurationError` naming it, before anything is sent. A credential given as a string is trimmed, and an empty one is absent.

## Credential checks

An API key carries a checksum; the SDK verifies it, the prefix and the length locally, so a truncated or mistyped credential never reaches a host. A credential of another kind in a slot is refused: `This is a metric source token (wt15ms_…abcd): pass it as metricsToken (WT15_METRICS_TOKEN), not apiKey.` Nothing the SDK prints holds a credential in full: `util.inspect`, `JSON.stringify` and `structuredClone` of a client show masked forms only, and no error, log record or response object holds a request’s headers or body.

## Timeouts and retries

There is one `timeout` for the whole exchange. `fetch` takes no connect timeout, so a connection that cannot be made gives up at undici’s fixed 10 seconds, or at a shorter `timeout`. Every call can override `timeout`, `maxRetries` and `maxRetryWait` in its [`RequestOptions`](https://developer.watchtower15.com/sdks/node/api/). Reads are retried on a connection failure, a 408, a 429 (waiting for `Retry-After`) and a 502, 503 or 504; a write is sent with an `Idempotency-Key` and retried the same way and on any 5xx; a daily limit is never retried.

## The User-Agent

The only metadata the SDK sends is the `User-Agent`: `watchtower15-node/<version> node/22.12 [framework/version] [appInfo]`. A framework package adds itself (`nestjs/12.0.0`).

## A custom `fetch`, a private CA, browsers

Pass `fetch` to route through a proxy or a test double. For a private certificate authority, set `NODE_EXTRA_CA_CERTS`: the SDK uses Node’s TLS as it is. A client refuses to be built in a browser (a key in a page is a key published); `dangerouslyAllowBrowser: true` lifts it for a client that holds no real credential, such as the test fakes.

Source: https://developer.watchtower15.com/sdks/node/configuration/
