<!-- Node.js — Heystack docs. Rendered: https://heystack.dev/docs/nodejs · Index: https://heystack.dev/llms.txt -->
# Node.js

For a long-running Node server — Express, Fastify, NestJS, or plain Node — use the `@heystack/otel/node` entry. `initHeystack` bundles auto-instrumentations (HTTP and common libraries) and registers a graceful-shutdown flush.

## Install

```bash
npm install "@heystack/otel@>=0.19.1"
```

## Initialize as the very first import

`initHeystack` must run **before** your framework loads, so auto-instrumentation can patch modules. Make it the first import in your entry file:

```ts
import { initHeystack } from "@heystack/otel/node";

initHeystack({ apiKey: process.env.HEYSTACK_API_KEY, service: "my-app" });

// ...then the rest of your imports / app...
```

This enables auto-instrumentations so you get spans without manual wiring.

> **Entry-ordering tip:** if your build can't guarantee this runs first, use `node --import` with a small `heystack.mjs` that calls `initHeystack`, or your framework's instrumentation hook.

## Set the key

```bash
# .env (loaded before initHeystack runs, and gitignored)
HEYSTACK_API_KEY=sk_live_…
```

## Graceful shutdown

`initHeystack` registers `SIGTERM`/`SIGINT` handlers automatically, so the SDK flushes and shuts down before exit — you won't lose the last batch on short-lived processes.

## Privacy

Recorded URLs have secrets removed (`?token=`, `?code=`, `?apikey=`, userinfo, …) and the end user's IP address is not recorded unless you pass `captureClientIp: true`. Add your own parameter names with `redactQueryParams: ["invite"]`. See [Privacy](https://heystack.dev/docs/sdk#privacy-url-redaction-and-client-ip).

## Verify

Pass `debug: true` to print OTel export logs while you're checking the wiring:

```ts
initHeystack({ apiKey: process.env.HEYSTACK_API_KEY, service: "my-app", debug: true });
```

Then run your app and make a request — traces appear in the [console](https://console.heystack.dev), usually within a minute.
