# Event export

Export a project's raw events for a period as gzipped JSON-lines files, one per day, to load them into your data warehouse or keep your own copy. Exports run in the background; you follow their progress and download each file through a short-lived signed link.

Exports are for organization owners. A member gets `403`, and so does a
credential from another organization. Each export request, each download
link issued and each cancellation or deletion is recorded in the
organization's audit log.

On Cohorly Cloud, requesting an export and asking for a download link need a
plan that includes event export (a self-hosted server has every feature).
Without it they answer `403` with `code: "plan_feature_required"` and
`feature: "exports"`. After a downgrade that shortens how long your data is
kept, export stays available for the 30 days before the older data is deleted.
Listing, reading and cancelling exports, and links already issued, keep working.

## How it works

1. Request an export of one project for a period (`from` and `to`, at most
   366 days), optionally only some events.
2. The export runs in the background, one day at a time. Poll it to follow
   `days_done`, `days_total` and `rows`.
3. When it is `completed`, ask for a download link for each file and fetch
   it. A link works for **15 minutes**; ask again for a fresh one.
4. Files are kept for **7 days** after the export completes, then deleted
   (status `expired`). You can delete them sooner.

An organization has at most **one active export** (`queued` or `running`) at
a time. There is no monthly limit.

Days are read in the project's time zone, exactly like `from` and `to` on
[`GET /api/events`](/api/admin): for the same period, the number of exported
lines equals that endpoint's `total` minus the events marked dropped, which
`GET /api/events` still lists and an export leaves out. On a day when the
clock changes (daylight saving), a day file covers 23 or 25 hours.

## What is exported

- Every event of the project in the period, except events marked **dropped**
  in the lexicon (their stored history included).
- Every stored property of each event, except properties marked **dropped**.
  Hidden events and properties are exported: hiding only affects the
  dashboard.
- `distinct_id` is the user's **canonical id**: an id later linked into
  another user (for example with `/alias`) is exported as that user. The id
  the event was stored with is kept in `$distinct_id_before_identity_fix`,
  unless the event already carries one.
- With `events`, each named event and the events merged into it. A dropped
  event name exports nothing.

Profiles are not exported.

## Line format

Each line is one event, in the shape [`POST /track`](/api/ingestion)
accepts. Lines are ordered by `time`. Properties the server added at
ingestion (geo, `$mp_api_timestamp_ms`, ...) are part of the stored event and
are exported too.

```json
{"event":"Purchase","properties":{"plan":"pro","amount":12,"distinct_id":"account-42","time":1772438400000,"$insert_id":"purchase-0042","$distinct_id_before_identity_fix":"anon-7"}}
```

| Field                    | Meaning                                  |
| ------------------------ | ---------------------------------------- |
| `event`                  | The event name as it was stored.         |
| `properties.distinct_id` | Canonical user id.                       |
| `properties.time`        | Unix milliseconds.                       |
| `properties.$insert_id`  | The event's deduplication id.            |
| other `properties`       | Stored properties, dropped ones removed. |

## Files

One file per day that has events; a day without events has no file. A file
is named `events-YYYY-MM-DD.jsonl.gz` and is gzip compressed.

A very large day (over 64 MiB of uncompressed JSON by default) is split into
parts: `events-2026-03-02.jsonl.gz`, then `events-2026-03-02.2.jsonl.gz`,
`events-2026-03-02.3.jsonl.gz`, and so on. Each entry of `files` lists its
`name`, `day`, `part`, `rows` and compressed `bytes`.

## Request an export

POST /api/exports Queue an export of one project's events for a period. Owners only.

projectId integer The project to export.

from string First day, YYYY-MM-DD.

to string Last day, YYYY-MM-DD, at most 366 days after from (both included).

events string\[] Only these events (1 to 100 names). Omit to export every event.

```bash
curl -X POST https://cohorly-service.velloalabs.com/api/exports \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "projectId": 1, "from": "2026-03-01", "to": "2026-03-31" }'
```

```json
{
  "export": {
    "id": 12,
    "project_id": 1,
    "status": "queued",
    "from": "2026-03-01",
    "to": "2026-03-31",
    "events": null,
    "days_total": 31,
    "days_done": 0,
    "rows": 0,
    "files": [],
    "error": null,
    "created_at": 1775000000000,
    "started_at": null,
    "finished_at": null,
    "expires_at": null
  }
}
```

```json
{ "error": "invalid date range" }
```

```json
{ "error": "forbidden" }
```

```json
{ "error": "export already active", "code": "export_active" }
```

`400 invalid events` when `events` is not a list of 1 to 100 non-empty
names.

## Follow an export

GET /api/exports/{id} One export: status, progress and files.

GET /api/exports?projectId= The project's 50 most recent exports, newest first, as { exports: \[...] }.

`status` moves from `queued` to `running` to `completed`. An export that
fails ends `failed` with `error` set to `timeout` (it ran past its time
limit, or one read stayed slow after a retry) or `internal`; its partial
files are deleted. Request it again.

```bash
curl https://cohorly-service.velloalabs.com/api/exports/12 \
  -H "Authorization: Bearer $API_KEY"
```

```json
{
  "id": 12,
  "project_id": 1,
  "status": "completed",
  "from": "2026-03-01",
  "to": "2026-03-31",
  "events": null,
  "days_total": 31,
  "days_done": 31,
  "rows": 1843021,
  "files": [
    { "name": "events-2026-03-01.jsonl.gz", "day": "2026-03-01", "part": 1, "rows": 60122, "bytes": 2290431 }
  ],
  "error": null,
  "created_at": 1775000000000,
  "started_at": 1775000004000,
  "finished_at": 1775000391000,
  "expires_at": 1775605191000
}
```

## Download a file

POST /api/exports/{id}/files/{name}/link A signed download link for one file of a completed export, valid 15 minutes.

The returned `path` is relative to the API base URL and needs no credential:
anyone holding it can download that one file until `expiresAt`. A tampered
or expired link answers `403 { "error": "invalid or expired link" }`.

```bash
curl -X POST https://cohorly-service.velloalabs.com/api/exports/12/files/events-2026-03-01.jsonl.gz/link \
  -H "Authorization: Bearer $API_KEY"
```

```json
{
  "path": "/api/exports/download/12/events-2026-03-01.jsonl.gz?expires=1775001291000&signature=4be1...",
  "expiresAt": 1775001291000
}
```

```json
{ "error": "not found" }
```

## Cancel or delete

DELETE /api/exports/{id} Cancel a queued or running export, or delete a completed export's files.

A running export stops at its next batch and its partial files are deleted
(`cancelled`). A completed export's files are deleted at once (`expired`). Deleting a
project does the same to its exports.
The response is `{ "export": {...} }` after the change.

## Automate it

A complete script: request, wait, download every file and read it with `jq`.

```bash
API=https://cohorly-service.velloalabs.com
AUTH="Authorization: Bearer $API_KEY"

ID=$(curl -s -X POST "$API/api/exports" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{ "projectId": 1, "from": "2026-03-01", "to": "2026-03-31" }' | jq -r '.export.id')

while :; do
  STATUS=$(curl -s "$API/api/exports/$ID" -H "$AUTH" | jq -r '.status')
  echo "export $ID: $STATUS"
  case "$STATUS" in completed) break ;; failed|cancelled|expired) exit 1 ;; esac
  sleep 10
done

for NAME in $(curl -s "$API/api/exports/$ID" -H "$AUTH" | jq -r '.files[].name'); do
  LINK=$(curl -s -X POST "$API/api/exports/$ID/files/$NAME/link" -H "$AUTH" | jq -r '.path')
  curl -s -o "$NAME" "$API$LINK"
done

gunzip -c events-2026-03-01.jsonl.gz | jq -c '{event, user: .properties.distinct_id}' | head
```
