# REST basics (https://flito.ai/docs/rest)

## The request [#the-request]

Every capability is one call:

```http
POST $FLITO_URL/api/cap/{capability id}
Authorization: Bearer $FLITO_API_KEY
Content-Type: application/json

{"input": {...}, "approved": true, "idempotencyKey": "..."}
```

- `input` is the capability's input, as its [reference page](/reference) describes. Send `{}` when it takes none.
- `approved` is only for [changes that need approval](#changes-that-need-approval).
- `idempotencyKey` makes a change [safe to retry](#safe-retries).

A body that is not valid JSON is read as `{}`, so the capability answers that its fields are missing.

## The answer [#the-answer]

A call that ran answers `200`:

```json
{
  "result": { "...": "the capability's output" },
  "commitId": "…",
  "eventId": "…",
  "idempotentReplay": false
}
```

- `result` is the capability's output.
- `commitId` names the version a change made, for records that keep versions. It is `null` for reads.
- `idempotentReplay` is `true` when the answer is a replay of an earlier call with the same key.

Any other status is an error with `code`, `message` and `correlationId`. See [Errors and limits](/errors).

## Reads and changes [#reads-and-changes]

Reads change nothing. A change runs at once, as the key's person, emails and other sends included. Each one is kept in the record's history with who made it, where the app can undo it.

## Changes that need approval [#changes-that-need-approval]

Some changes are hard to undo: deleting, archiving and merging records, sending quotes for signature, bulk email and broadcasts, sending invoices, and switching automations on. With an API key they answer `409 APPROVAL_REQUIRED` unless the body says `"approved": true`:

```bash
curl -s "$FLITO_URL/api/cap/company.archive" \
  -H "Authorization: Bearer $FLITO_API_KEY" -H "Content-Type: application/json" \
  -d '{"input": {"id": "cmp_…"}, "approved": true}'
```

<Callout title="For AI agents calling REST">
  Set `"approved": true` only after the person you work for said yes to that exact change. Over
  MCP, Flito's own tools handle this for you.
</Callout>

Each capability's reference page says whether it needs approval: always, never, or depending on the input (an automation needs it only when it is switched on).

## Safe retries [#safe-retries]

A network error can leave you not knowing whether a change ran. Send an idempotency key with each change, and send the same key when you retry:

```bash
curl -s "$FLITO_URL/api/cap/task.create" \
  -H "Authorization: Bearer $FLITO_API_KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: import-2026-11-10-row-42" \
  -d '{"input": {"title": "Call Ada back", "priority": "high"}}'
```

- Put it in the `Idempotency-Key` header or in the body as `idempotencyKey`. The body wins when both are there.
- A key counts for 24 hours, per capability.
- The same key with the same input replays the first answer, with `"idempotentReplay": true`. Nothing runs twice.
- The same key with a different input answers `409 IDEMPOTENCY_CONFLICT`.
- Only calls that worked are remembered, so a retry after an error runs again.
- Retry after the first call has answered. Two calls sent at the same moment with the same key can both run.

## Dry runs [#dry-runs]

`POST $FLITO_URL/api/cap/{id}/dry-run` with `{"input": {...}}` runs the change, rolls it back, and answers what it would have done. It never needs `approved`.

```json title="Answer"
{
  "diffs": [{ "entityType": "contacts", "entityId": "con_…", "before": { "...": "..." }, "after": { "...": "..." } }],
  "sideEffects": ["…"],
  "warnings": [],
  "approvalRequired": false
}
```

Use it to show a person exactly what a change will do before you make it.

## Lists, search and paging [#lists-search-and-paging]

List capabilities (`company.list`, `contact.list`, `deal.list`, `lead.list` and the like) share these inputs:

- `search`: words to match on names, emails and the like.
- `limit`: up to 200 (500 for `task.list`), 50 when left out.
- `offset`: how many to skip, for the next page.
- `sortField` and `sortDir` (`asc` or `desc`), or `sorts` for up to three.
- `filter`: conditions on any field, below.
- `archived` for archived records only, `includeArchived` for both.

They answer `{"rows": [...], "rowCount": n}`, where `rowCount` counts every match. Page until you have them all:

```js
async function listAll(capability, input = {}) {
  const rows = []
  for (let offset = 0; ; offset += 200) {
    const res = await fetch(`${process.env.FLITO_URL}/api/cap/${capability}`, {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${process.env.FLITO_API_KEY}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ input: { ...input, limit: 200, offset } }),
    })
    const { result } = await res.json()
    rows.push(...result.rows)
    if (rows.length >= result.rowCount || result.rows.length === 0) return rows
  }
}
```

### Filters [#filters]

A filter is groups of conditions, joined with `and` or `or`:

```json
{
  "combinator": "and",
  "groups": [
    {
      "combinator": "or",
      "conditions": [
        { "column": { "field": "lifecycleStage" }, "operator": "eq", "value": "customer" },
        { "column": { "field": "lifecycleStage" }, "operator": "eq", "value": "prospect" }
      ]
    }
  ]
}
```

Operators: `eq`, `neq`, `contains`, `not_contains`, `contains_any`, `not_contains_any`, `starts_with`, `ends_with`, `gt`, `gte`, `lt`, `lte`, `between`, `in`, `not_in`, `is_empty`, `is_not_empty`, `is_true`, `is_false`. A workspace's own fields work too: [discover](/discover) lists them.

## History and undo [#history-and-undo]

Changes to companies, contacts and deals keep versions, and their answer carries the `commitId`. To see a record's versions:

```bash
curl -s "$FLITO_URL/api/config/commits?objectType=contacts&objectId=con_…" \
  -H "Authorization: Bearer $FLITO_API_KEY"
```

`GET $FLITO_URL/api/config/commits/{commitId}` shows one version and what it changed. To undo it, call `config.revert` with `{"commitId": "…"}`. That needs the `config:revert` scope, which owners and admins hold.

## Ids and values [#ids-and-values]

- Ids are a prefix and a UUID: `cmp_` companies, `con_` contacts, `deal_` deals, `lead_` leads, `task_` tasks.
- Dates and times are ISO 8601, such as `2026-11-10` or `2026-11-10T15:00:00Z`.
- Money is a number in the workspace's currency (`workspace.currency` from [discover](/discover)).
- In an update, a field you leave out stays as it is, and `null` clears it.

## Tracing a call [#tracing-a-call]

Send an `X-Correlation-Id` header with any value you like. Errors carry it back as `correlationId`, so you can match a failure to your own logs. When you leave it out, Flito makes one.

## Shortcuts for common records [#shortcuts-for-common-records]

A few plain REST routes call the same capabilities:

- `POST /api/companies`, `/api/contacts`, `/api/activities` create one. The body is the input itself, not wrapped in `input`, and they answer `201`.
- `PATCH /api/companies/{id}` (and contacts, activities) updates one.
- `GET /api/companies?search=&limit=` (and contacts, activities) lists them; `GET /api/companies/{id}` reads one.

They take no `approved` flag. Everything else goes through `/api/cap/{id}`.
