> ## Documentation Index
> Fetch the complete documentation index at: https://docs.turtle.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Sonar API conventions

> How Sonar lists, filters and pages, how writes are recorded and approved, and its errors, limits and versioning.

The rules on this page hold across the Sonar API. Each endpoint's page in the API reference has its own parameters, bodies and statuses.

## Paths and names

Every path is under `https://sonar.turtle.xyz/v1/organization/{organizationId}`, and these pages write paths relative to it.

The API keeps a few names of its own:

| The API says | The dashboard says |
| - | - |
| engagement | deal |
| thread | storyline |
| pipeline | board |
| stint | a contact's role at an organization |
| mutation | a change in the log |
| draft | a change waiting for a person to decide |

## Reading

* **Lists** answer `{"<entities>": [...], "nextCursor": "…"}`: `GET /contacts` answers `{"contacts": [...]}`. A **single read** answers `{"<entity>": {...}}`, with its relations as sibling keys, and returns at least what its listing returns.
* **Paging.** `limit` is 1 to 100, default 25. Pass `nextCursor` back as `cursor`; no `nextCursor` means the last page. Every ordering ends on the ID, so a tie at a page boundary never skips or repeats a row. A few small catalogs are not paged, such as pipelines, rules, entity cards and templates.
* **Sorting.** Listings share `sort` names where they mean the same thing: `created` (newest first, the usual default), `recent` (latest activity first) and `stale` (longest silence first). Some add their own, such as `revenue` on `/engagements`, and `/events` sorts by `ingested` or `occurred`. A listing refuses a sort it does not take.
* **Filters.** Repeat a parameter to OR its values (`?stage=a&stage=b`, at most 100 values). Different parameters AND together. Instants are RFC 3339 and days are `YYYY-MM-DD`. No listing requires a filter.
* **Unknown parameters are refused.** A query parameter the endpoint does not take answers `400` and names it, so a misspelled filter never reads as no filter.
* **`search`** keeps the rows holding every word of it, in any order, case-insensitive.
* **`updatedAfter`** returns only the rows changed after that instant. It is how you sync organizations, contacts, threads, deals, tasks, documents, wallets and reports.
* **`count=true`** also answers `total`: how many rows match, across every page.
* **`drafts=true`** attaches the open proposals on each row, as `present` and `future`, on the reads that take it.
* **References.** A related record comes as a small ref beside its ID: `counterpartyId` next to `counterparty: {id, name}`. An ID with no ref beside it means the record is gone or has no name, not an error.
* **Derived fields** say *Derived* in their description, and are read-only.

### Following events

Events never change, so `/events` takes no `updatedAfter`. To follow new events as they arrive, read the listing as a poll:

1. Start with `GET /events?poll=true&since=<instant>`. A poll reads oldest first.
2. Pass each page's `nextCursor` back as `cursor` on the next call.
3. An empty page means you are caught up: keep the cursor you sent, and poll again later.

## Writing

### Who a write is recorded as

A write is recorded as whoever the key acts for:

* A **personal key** writes as you. The write applies at once, and `rationale` and `evidence` are optional, as in the dashboard.
* A **personal key with `"source": "ai"`** writes as your agent, with you as its user.
* An **organization key** writes as an agent.

The last two are **agent writes**, held to the rules Sonar's own agent follows below.

### Rationale and evidence

An agent write carries a `rationale` (one or two sentences) and `evidence`, the events it rests on:

```json theme={null}
{
  "rationale": "The counterparty accepted the terms, so the deal moves to contract.",
  "evidence": [
    { "eventId": "<uuid>", "quotes": ["Terms work for us. Send the agreement."] }
  ]
}
```

* At least one citation supports the write with a verbatim quote from the event it cites, or is a teammate's message, cited as `{messageId, quotes}` instead of `eventId`.
* `stance` is `supports` (the default) or `retracts`, which takes back an event an earlier write cited and needs no quote.
* An agent's write to a deal may cite in support only events a thread already holds, so thread them first.
* A DELETE has no body: it takes `?rationale=&evidence=`, with the same array as URL-encoded JSON.

### The response

Every write answers `{applied, entityId, mutation?, draft?}`:

* `applied: true`: the write landed, and `mutation` is its row in the log.
* `applied: false` with a `draft`: it is held for a person. The draft is the row the log will hold if it is approved.
* `applied: false` with no draft: nothing needed to change, and `entityId` is the existing record.

Here a personal key moves a deal as your agent. The write says why and quotes its evidence:

```bash theme={null}
curl -X PUT "https://sonar.turtle.xyz/v1/organization/$ORG_ID/engagements/ENGAGEMENT_ID" \
  -H "Authorization: Bearer $SONAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "stage": "Contract",
    "source": "ai",
    "rationale": "The counterparty accepted the terms, so the deal moves to contract.",
    "evidence": [{ "eventId": "EVENT_ID", "quotes": ["Terms work for us. Send the agreement."] }]
  }'
```

If your workspace holds the write, it comes back as a draft for a person to decide:

```json theme={null}
{ "applied": false, "entityId": "<uuid>", "draft": { "status": "draft", "…": "…" } }
```

### Held writes and drafts

* **An agent's held writes.** Any delete, merge or split, and a board or card change that rewrites records, waits for a person. The exception is deleting a record an agent created that nobody has touched since, which never applies to a deal. Every other agent write applies, and your workspace can hold or free others with [permission policies](https://docs.turtle.xyz/sdk/sonar/reference/sonar/set-a-permission-policy).
* **Your own writes are never held** by those permissions.
* **Holding a write yourself.** Send `"draft": true` (`?draft=true` on a DELETE) to hold any write: an agent's waits for a person to approve it, and your own waits for you to confirm it. Sending the same proposal again returns the same draft.
* **Deciding.** `POST /mutations/{id}/approve` makes a draft, optionally with `amend` (your own values for some of its fields) and a `reason`. `POST /mutations/{id}/reject` declines it. `POST /reports/{id}/decide` decides a report's drafts together. Deciding takes an owner or admin (see [Authentication](https://docs.turtle.xyz/sdk/sonar/authentication.md#what-takes-an-owner-or-admin)).
* **Undo.** `POST /mutations/{id}/revert` takes an applied write back as a new write, and `GET` on the same path shows what it would do first. An agent's undo is itself a draft.

### Updating, merging and retrying

* **Patching.** A PUT changes only the keys you send. Custom `fields` merge key by key: a key you leave out keeps its value, a key you send replaces it (a list is replaced whole), and `null`, `""` or `[]` clears it.
* **Custom fields** take only the keys the record's card declares. A deal's card is its board's (`GET /pipelines/{id}`); every other kind's is in `GET /entity-cards`. An unknown or derived key is refused by name.
* **Merging.** `POST /{entities}/{id}/merge` with `{"absorbedId": "<uuid>"}` moves everything on the absorbed record onto `{id}`, and deletes the absorbed one. Organizations, contacts, deals and threads merge.
* **Idempotent writes.** A PUT means "make it so", and deleting a link that is already gone changes nothing. A write that changes nothing answers `applied: false` and logs nothing. A deleted record keeps its history but leaves reads, so deleting it again is a `404`.
* **Retrying a create.** Most creates resolve to an existing record when there is one: organizations by name, contacts by address, documents by counterparty and name, wallets by address, and open tasks by deal and description. A deal or a thread is new on every create, so check before you retry one.

## Errors

An endpoint answers an error as JSON, with the status text and a message saying why:

```json theme={null}
{ "status": "Bad Request", "error": "…why the request was refused…" }
```

It may also carry `code` (an application error code) and `context`. A request refused before it reaches an endpoint (no usable key, a key that may not reach the organization, a path the API does not have) gets a plain-text body instead.

| Status | Meaning |
| - | - |
| `400` | The request was refused, and `error` says why. For example: an event cited twice, an agent's write with no quote, a card key the card does not declare, a query parameter the endpoint does not take, or a filter repeated more than 100 times. |
| `401` | No usable key. See [Authentication](https://docs.turtle.xyz/sdk/sonar/authentication.md#authentication-errors). Deciding drafts and permission policies also answer `401` to a personal key whose owner is not an owner or admin. |
| `403` | The key is valid but may not do this, such as an organization key deciding a draft. |
| `404` | The record, or an event you cite, is not in this organization. |
| `412` | A precondition the endpoint names was not met, such as approving a draft whose record changed while it waited. |
| `500` | Something failed on our side. Retrying a read, a PUT or a DELETE is safe; for a create, see [retrying a create](#updating-merging-and-retrying). |

Each endpoint's page in the API reference lists the statuses it can return.

## Limits

* **Page size** is 1 to 100, default 25.
* **A repeatable filter** takes at most 100 values, and some take fewer (the reference gives each one's limit). Past that it is refused with `400`, never cut short.
* **Long texts**, such as descriptions, have a size budget. A write reports it back in `budgets`, and it is never enforced.
* **Inbound webhooks** take 100 deliveries a day each, with a body of at most 64 KB. See [Inbound webhooks](https://docs.turtle.xyz/sdk/sonar/webhooks.md#limits).

## Versioning and deprecation

v1 is stable. Within v1, changes are additive only:

* new operations, and new optional query parameters or request fields;
* new response fields;
* new values in a response enum.

Ignore response fields you do not know, and tolerate enum values you do not know. Anything breaking (removing or renaming an operation, a parameter or a field, making an optional input required, changing a type or what a field means) ships only in a new version, `/v2`.

An operation is retired in steps. First it is marked `deprecated` in the spec, and every answer it gives carries three headers:

| Header | Value |
| - | - |
| `Deprecation` | The date it was deprecated ([RFC 9745](https://www.rfc-editor.org/rfc/rfc9745)), such as `@1767225600`. |
| `Sunset` | The date it stops answering ([RFC 8594](https://www.rfc-editor.org/rfc/rfc8594)), at least 90 days later. |
| `Link` | `<https://sonar.turtle.xyz/v1/openapi.json>; rel="deprecation"`, the policy. |

Watch for the `Deprecation` header: it is the notice.

The spec at `https://sonar.turtle.xyz/v1/openapi.json` is the contract: the API serves exactly the operations it lists. The Sonar dashboard also reads two views that v1 does not carry, its overview's activity cards and its pipeline history, because they are shaped after a screen and change with it.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.