# Import history

`crm.imports` — `/v1/audience/imports`

One row per bulk load, and the record a worker moves through
`pending → processing → completed | failed`.

Creating a row **queues** the work; it does not do it. The file itself is
uploaded out of band and named here by its storage key. `statistics` fill in as
the worker walks the file, so poll `get` to follow a run.

The route is `/imports`, not `/contact-imports`: a row's `module` is `contact`
**or** `datastore`, so these were never contact-only.

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

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

**Permissions** — the catalogue publishes `CRM.CONTACT.EXPORT` but **no
`IMPORT`**, so these map to what an import does: `create` and `delete` need
`CRM.CONTACT.CREATE` (an import is bulk contact creation) and the reads need
`CRM.CONTACT.LIST`. If importing should be grantable separately from creating
one contact at a time, the catalogue needs an `IMPORT` verb.

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


## The import object

```ts
interface ImportHistory {
  id: string;
  member?: string;          // the team member who started it
  module: 'contact' | 'datastore';
  file: string;             // storage key of the uploaded file
  title: string;
  status: 'pending' | 'processing' | 'completed' | 'failed';
  config: ImportConfig;
  extra: Record<string, unknown>;
  statistics: {
    total: number;
    success: number;   // = new + duplicate
    failed: number;
    new: number;       // rows that created a contact
    duplicate: number; // rows that matched one that already existed
  };
  createdAt: string;
  updatedAt: string;
}

interface ImportConfig {
  mapping?: Record<string, string>;          // column header -> contact field
  duplicateStrategy?: 'skip' | 'update';
  [key: string]: unknown;                    // anything else is carried through
}
```

`config` and `extra` are untyped columns. The keys the platform acts on are
named above; anything else you put there is stored and handed back untouched.
A whole-run failure puts its reason in **`extra.error`** — the import schema has
no `error` column of its own.

---

## list

```ts
crm.imports.list(query?: Partial<ListImportHistoryQuery>): Promise<Paginated<ImportHistory>>
```

`GET /v1/audience/imports` · `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. |

```ts
const running = await crm.imports.list({ status: 'processing' });
```

---

## get

```ts
crm.imports.get(id: string): Promise<ImportHistory>
```

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

The progress endpoint: the row with its current `status` and `statistics`.

```ts
const poll = async (id: string) => {
  for (;;) {
    const run = await crm.imports.get(id);
    if (run.status === 'completed' || run.status === 'failed') return run;
    await new Promise((r) => setTimeout(r, 2000));
  }
};
```

---

## create

```ts
crm.imports.create(input: CreateImportHistoryInput): Promise<ImportHistory>
```

`POST /v1/audience/imports` · `contacts:write` · **202**

**202, not 201**: the row exists, but nothing has been read out of the file yet.
The response is the row at `status: 'pending'` with every counter at zero.

### Body

| Field | Type | Required | Default | Notes |
| --- | --- | --- | --- | --- |
| `file` | string | yes | — | 1–1024 chars. Storage key of the already-uploaded file; the worker resolves it. |
| `title` | string | yes | — | Trimmed, 1–200 chars. |
| `module` | `'contact' \| 'datastore'` | no | `'contact'` | |
| `config` | `ImportConfig` | no | `{}` | Defaults to an empty mapping and `duplicateStrategy: 'skip'`. |
| `extra` | object | no | `{}` | Carried through untouched. |

```ts
const run = await crm.imports.create({
  title: 'March contacts',
  file: 'uploads/2026-03/contacts.csv',
  config: {
    mapping: { Email: 'email', 'First name': 'name.first_name', City: 'city' },
    duplicateStrategy: 'update',
    delimiter: ';',          // unknown keys survive the round trip
  },
});

run.status;     // 'pending'
run.statistics; // { total: 0, success: 0, failed: 0, new: 0, duplicate: 0 }
```

`duplicateStrategy` decides what a row matching an existing contact does:
`'skip'` leaves the contact alone, `'update'` writes the row's values over it.

---

## delete

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

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

Removes the history row. **Contacts the import already created are not touched** —
this deletes the receipt, not the work.
