---
title: Node.js SDK
description: Send logs and heartbeats from Node.js with @zevsight/node, as a pino transport or a small client.
url: https://docs.zevsight.com/send/nodejs
---

# Node.js SDK

> Send logs and heartbeats from Node.js with @zevsight/node, as a pino transport or a small client.

`@zevsight/node` sends logs and heartbeats from Node.js 18 or newer. Logging never blocks your code and never throws into it. Lines are batched, gzip-compressed and retried with backoff. Each line gets its id when it is logged, so a retried batch is stored once.

```bash
npm install @zevsight/node
```

Set the ingest token in the environment:

```bash
ZEVSIGHT_INGEST_KEY=<INGEST_TOKEN>
```

## pino and nestjs-pino

Add ZevSight as a transport target. Keep stdout as well if you like.

```js

export const logger = pino({
  transport: {
    targets: [
      { target: 'pino/file', options: { destination: 1 } }, // keep stdout
      {
        target: '@zevsight/node/pino',
        options: { apiKey: process.env.ZEVSIGHT_INGEST_KEY, service: 'api', environment: 'production' },
      },
    ],
  },
});
```

With nestjs-pino, put the same `transport` in `LoggerModule.forRoot({ pinoHttp: { transport } })`.

Moving from `@logtail/pino`? Replace its target with `@zevsight/node/pino` and pass `apiKey`; nothing else in your logger changes.

How pino fields are mapped:

| pino | ZevSight |
|---|---|
| `level` | severity (`trace` … `fatal`) |
| `time` | event time |
| `msg` | message (the error's message if there is no `msg`) |
| `err` | `fields.err` with type, message and stack |
| `hostname`, `pid` | resource `host.name`, `process.pid` |
| `req`, `res` | attributes `req.method`, `req.url`, `res.statusCode` … (nested objects such as headers go to fields) |
| `trace_id`, `span_id` | the event's trace and span ids |
| other flat values (`context`, `responseTime`) | attributes, searchable as `attrs.context:HTTP` |
| other objects and arrays | fields |

If you renamed pino's keys, pass `messageKey` or `errorKey` in the target's options.

## Client

```js

const zevsight = new ZevSight({ service: 'billing-worker' }); // key from ZEVSIGHT_INGEST_KEY

zevsight.info('invoice sent', { invoiceId: 'inv_42', amount: 1200 });
zevsight.error('payment failed', { provider: 'card' });
zevsight.log({
  message: 'checkout completed',
  severity: 'info',
  attrs: { route: '/v1/checkout' },   // flat, searchable
  fields: { cart: { items: 3 } },     // structured
  traceId: '4bf92f3577b34da6a3ce929d0e0e4736',
});

await zevsight.close(); // flush before a short-lived script exits
```

Methods: `log(event)`, `trace`, `debug`, `info`, `warn`, `error`, `fatal` (each takes `message, attrs`), `flush()` and `close(timeoutMs?)`.

## Heartbeats

Create a [heartbeat monitor](/monitor/heartbeats) and copy its ping URL.

```js

// Wrap a job: start, then ok, or fail with the error's message.
await withHeartbeat(process.env.ZEVSIGHT_HEARTBEAT_NIGHTLY_EXPORT, () => runNightlyExport());

// Or ping by hand.
const hb = heartbeat(process.env.ZEVSIGHT_HEARTBEAT_QUEUE_WORKER);
await hb.start();
await hb.ok('processed 120 jobs');
await hb.fail('disk full');
```

Pings never throw. Each resolves to `true` when ZevSight recorded it. `heartbeat(url, { timeoutMs, retries })` defaults to a 10-second timeout and 2 retries. Notes can be up to 8 KiB.

## Configuration

| Option | Environment variable | Default |
|---|---|---|
| `apiKey` | `ZEVSIGHT_INGEST_KEY` | none: nothing is sent, and a warning is printed once |
| `service` | `ZEVSIGHT_SERVICE`, then `OTEL_SERVICE_NAME` | `unknown` |
| `environment` | `ZEVSIGHT_ENVIRONMENT`, then `NODE_ENV` | `production` |
| `region` | `ZEVSIGHT_REGION` | `eu` (`https://ingest.zevsight.com`) |
| `endpoint` | `ZEVSIGHT_ENDPOINT` | the region's ingest host |
| `resource` | | none; attached to every line (version, region…) |
| `flushIntervalMs` | | `1000` |
| `maxBatchEvents` | | `500` (max 5000) |
| `maxBatchBytes` | | `1048576` |
| `maxBufferEvents` | | `20000`; beyond this the oldest lines are dropped and counted |
| `maxRetries` | | `10` |
| `maxRetryAfterMs` | | 5 minutes; a longer `Retry-After` (monthly quota reached) gives the batch up |
| `requestTimeoutMs` | | `15000` |
| `handleExit` | | `true`: flush on `beforeExit`, `SIGTERM`, `SIGINT` |
| `shutdownTimeoutMs` | | `5000` |
| `onError(e)` | | prints at most one line per minute to stderr |
| `onQuota(q)` | | none; called past 80% of the monthly quota |

Service and environment names are made into slugs (`Checkout Service` becomes `checkout-service`).

## When things go wrong

| Situation | Behaviour |
|---|---|
| ZevSight unreachable, 5xx, 408 | Retried with exponential backoff (0.5 s to 30 s, jittered) |
| 429 or 503 with `Retry-After` | Waits exactly that long, unless longer than `maxRetryAfterMs` |
| Some lines refused (too old for the project's retention, malformed) | The rest are stored; `onError` gets `{ kind: 'rejected', rejected }` |
| Wrong key or wrong region | Not retried; `onError` gets `delivery_failed` (with the right `endpoint` for a region mismatch) |
| Buffer full while offline | Oldest lines dropped; `onError` gets `{ kind: 'dropped' }` and the next batch carries `zevsight.dropped_events` |

## Shutdown

- **pino transport:** pino closes the transport when your process exits, and the transport flushes what it holds first.
- **Client:** with `handleExit` on, the client flushes on `beforeExit`, `SIGTERM` and `SIGINT`, for up to `shutdownTimeoutMs`. In a short-lived script, `await zevsight.close()` at the end.
- A crash that kills the process (`SIGKILL`, out of memory) loses at most one flush interval of lines.
