# Export history

`crm.exports` — `/v1/audience/exports`

One row per bulk extract, moved by a worker through
`pending → processing → completed | failed`.

Creating a row **queues** the work. When the run completes, `file` holds the
storage key of the written file and `count` the number of rows in it; when it
fails, `error` says why.

The route is `/exports`, not `/contact-exports`: a row's `module` is `contact`
**or** `datastore`.

> **Worker status.** The row, its lifecycle and its counters are live. The
> worker that writes the file is still a stub — it moves a row to `processing`
> and then to `completed` without producing anything. No file is written yet.

| Action | Route | Scope |
| --- | --- | --- |
| [`list`](#list) | `GET /v1/audience/exports` | `contacts:read` |
| [`get`](#get) | `GET /v1/audience/exports/:id` | `contacts:read` |
| [`create`](#create) | `POST /v1/audience/exports` | `contacts:read` |
| [`delete`](#delete) | `DELETE /v1/audience/exports/:id` | `contacts:write` |

**Permissions** — every route here needs `CRM.CONTACT.EXPORT`, reads included:
an export history row *is* the export, so a member who may not export has no
business reading what one produced. The catalogue notes `EXPORT` implies
`CRM.CONTACT.LIST`.

There is no `update`: a row is moved by the worker, not by a caller.

Note the scope on `create` — reading contacts out is a **read**, so an export
can be started with read-only access.


## The export object

```ts
interface ExportHistory {
  id: string;
  member?: string;          // the team member who started it
  module: 'contact' | 'datastore';
  file?: string;            // storage key; absent until the worker writes one
  title: string;
  count: number;            // rows written; 0 until it completes
  status: 'pending' | 'processing' | 'completed' | 'failed';
  error?: string;           // why it failed, when it did
  config: ExportConfig;
  extra: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

interface ExportConfig {
  segment?: string;          // a saved segment's id or its name
  filter?: ContactFilter;    // ad-hoc conditions
  fields?: string[];         // columns to write; [] means the worker's default set
  [key: string]: unknown;    // anything else is carried through
}
```

`config` says **what to write out**, in the same vocabulary the contact list
speaks. Sending both a `segment` and a `filter` narrows the segment, exactly as
`crm.contacts.list` reads the pair — so an export means what the list it came
from means.

Unlike imports, the export schema has a real `error` column, so a failure goes
there rather than into `extra`.

---

## list

```ts
crm.exports.list(query?: Partial<ListExportHistoryQuery>): Promise<Paginated<ExportHistory>>
```

`GET /v1/audience/exports` · `contacts:read` · **200**

Newest first.

| Field | Type | Default | Notes |
| --- | --- | --- | --- |
| `page` | number | `1` | 1-based. |
| `rows` | number | `25` | 1–100. |
| `search` | string | — | 1–120 chars. Matches the **title**. |
| `module` | `'contact' \| 'datastore'` | — | Quick filter. |
| `status` | `HistoryStatus` | — | Quick filter. |

---

## get

```ts
crm.exports.get(id: string): Promise<ExportHistory>
```

`GET /v1/audience/exports/:id` · `contacts:read` · **200**

The progress endpoint: the row with its current `status` and `count`, and — once
finished — `file`.

```ts
const run = await crm.exports.get(id);
if (run.status === 'completed') download(run.file!);
if (run.status === 'failed') console.error(run.error);
```

---

## create

```ts
crm.exports.create(input: CreateExportHistoryInput): Promise<ExportHistory>
```

`POST /v1/audience/exports` · `contacts:read` · **202**

**202, not 201**: the row exists, but no file has been written yet. The response
is the row at `status: 'pending'` with `count: 0` and no `file`.

### Body

| Field | Type | Required | Default | Notes |
| --- | --- | --- | --- | --- |
| `title` | string | yes | — | Trimmed, 1–200 chars. |
| `module` | `'contact' \| 'datastore'` | no | `'contact'` | |
| `config` | `ExportConfig` | no | `{}` | The default exports every contact with the worker's default columns. |
| `extra` | object | no | `{}` | Carried through untouched. |

Only a title is required:

```ts
await crm.exports.create({ title: 'Everyone' });
```

A narrower one:

```ts
await crm.exports.create({
  title: 'High budget, Pune',
  config: {
    segment: 'High budget',
    filter: {
      conditions: [
        { operator: null, field_id: 'city', field_type: 'text', condition: 'equal to', value: 'Pune' },
      ],
    },
    fields: ['name.first_name', 'name.last_name', 'email', 'city'],
  },
});
```

`fields` names the columns to write; leave it out (or empty) for the worker's
default set.

---

## delete

```ts
crm.exports.delete(id: string): Promise<void>
```

`DELETE /v1/audience/exports/:id` · `contacts:write` · **204**, no body.

Removes the history row. **A file already written to storage is not deleted with
it** — take the `file` key first if you need to clean it up.
