> For the complete documentation index, see [llms.txt](https://docs.millimetric.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.millimetric.ai/sdks/node.md).

# Node — @millimetric/track-node

A thin wrapper over `POST /v1/track` and `POST /v1/batch` with batching, retries on `5xx`, and exponential backoff. Zero dependencies.

## Install

```bash
npm i @millimetric/track-node
```

Requires Node 18+ (global `fetch`). Works in Bun and Deno too.

## Quick start

```ts
import { init, track, flush } from "@millimetric/track-node";

init({ key: process.env.AOA_SK!, host: "https://api.millimetric.ai" });

// somewhere in a request handler:
track({
  event: "purchase",
  anonymous_id: req.cookies.aid,
  user_id: user.id,
  properties: { amount_cents: 4900, currency: "usd" }
});

// before a serverless function returns:
await flush();
```

## Public API

### `init(options) → MillimetricClient`

```ts
type ClientOptions = {
  /** Your sk_* key (use sk_ on servers, not pk_). */
  key: string;
  /** Worker base URL. */
  host: string;
  /** Flush at N queued events. Default 1 (send immediately). */
  flushAt?: number;
  /** Flush after this many ms. Default 1000. */
  flushIntervalMs?: number;
  /** Retries on 5xx / network errors. Default 3. */
  maxRetries?: number;
};
```

You can also construct a client directly without the singleton:

```ts
import { MillimetricClient } from "@millimetric/track-node";
const client = new MillimetricClient({ key, host });
client.track({ event: "x" });
await client.flush();
```

### `track(event)`

```ts
track({
  event: "signup",
  anonymous_id: "u_abc",
  user_id: "user_42",
  properties: { plan: "free" }
});
```

Same field shape as the [`POST /v1/track`](/api-reference/track.md) HTTP body.

### `flush()`

```ts
await flush();
```

Flushes the queue synchronously. **Call this before a serverless function exits** — otherwise queued events may be lost when the function is frozen.

## Retry behavior

* `5xx` response → retried with exponential backoff (100, 200, 400, 800 ms).
* Network error → same.
* `4xx` response → thrown immediately, no retry. (The payload is bad; retrying won't help.)
* After `maxRetries` attempts, the in-flight batch is re-added to the head of the queue and the error bubbles up from `flush()`.

## Batching

`flushAt` controls how many events to accumulate before sending:

```ts
init({ key, host, flushAt: 50, flushIntervalMs: 5000 });
```

With `flushAt > 1`, the SDK uses `POST /v1/batch`. With `flushAt = 1` (the default), it uses `POST /v1/track` directly — no extra latency.

The internal timer is `unref()`'d so it won't keep a Node process alive on its own. You still need `flush()` before exit.

## Pattern: long-running server

```ts
init({ key, host, flushAt: 50, flushIntervalMs: 2000 });

// fire and forget — the SDK batches in the background
app.post("/signup", async (req, res) => {
  // … your signup logic …
  track({ event: "signup", anonymous_id: req.cookies.aid, user_id: user.id });
  res.json({ ok: true });
});

// on shutdown:
process.on("SIGTERM", async () => { await flush(); process.exit(0); });
```

## Pattern: serverless (Vercel / Cloudflare / Lambda)

```ts
init({ key, host, flushAt: 1 });   // send immediately

export async function POST(req: Request) {
  // … handle the request …
  track({ event: "form_submit", anonymous_id });
  await flush();                    // important — function freezes after this returns
  return new Response("ok");
}
```

## Pattern: background backfill

```ts
import { MillimetricClient } from "@millimetric/track-node";
import fs from "node:fs/promises";

const client = new MillimetricClient({ key: process.env.AOA_SK!, host, flushAt: 1000 });

const events = JSON.parse(await fs.readFile("backfill.json", "utf8"));
for (const e of events) client.track(e);
await client.flush();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.millimetric.ai/sdks/node.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
