> 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/api-reference/batch.md).

# POST /v1/batch

POST /v1/batch — emit up to 1000 events in one call.

Send up to 1000 events in a single request. Used by the browser SDK for buffered flushes (every 20 events or every 2 seconds, whichever comes first).

## Auth

|                |                 |
| -------------- | --------------- |
| Required scope | `ingest`        |
| Key kinds      | `pk_*` · `sk_*` |

## Request

```http
POST /v1/batch
Authorization: Bearer {key}
Origin: {your-origin}              ← only for pk_* keys
Content-Type: application/json
```

```json
{
  "events": [
    { "event": "$pageview", "anonymous_id": "u_abc",
      "url": "https://yoursite.com/?fbclid=abc" },
    { "event": "click",     "anonymous_id": "u_abc",
      "properties": { "button": "cta" } }
  ]
}
```

Each element in `events` follows the [same schema as `POST /v1/track`](/api-reference/track.md).

## Response

```http
HTTP/1.1 202 Accepted
Content-Type: application/json
```

```json
{ "ok": true, "count": 2 }
```

## Limits

* **Max events per batch**: 1000. Over the limit returns `400 invalid_payload`.
* **Total payload size**: bounded by the 8 KB-per-event `properties` cap × event count.
* **Rate limit**: 5 batch requests/sec, burst of 20 per project (separately from `/v1/track`).

## Atomicity

A batch is **all-or-nothing at the request level** — the Worker either inserts every event or returns an error. ClickHouse itself batches the insert as a single `JSONEachRow` payload. If validation fails on event #4 of 50, no events are written.

## Why batch

* **Browser SDK** uses it to coalesce navigations + interactions, reducing request volume by \~20×.
* **Server backfills** of historical data — pipe N events from a CSV, batch into chunks of 1000.
* **Idempotent replays** — if you set `event_id` on each event, you can safely re-run a batch (no server-side dedup yet — that's still your job).

## Example: server backfill

```js
import fs from "node:fs/promises";
const events = JSON.parse(await fs.readFile("backfill.json", "utf8"));

for (let i = 0; i < events.length; i += 1000) {
  const chunk = events.slice(i, i + 1000);
  await fetch("https://api.millimetric.ai/v1/batch", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.SK_KEY}`,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({ events: chunk })
  });
}
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.millimetric.ai/api-reference/batch.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
