> ## 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 quickstart

> Make your first Sonar read with a personal API key, then keep a copy of your pipeline in sync.

This is the shortest path from nothing to reading your organization's Sonar and keeping a copy of it in sync: create a key, make a first read, then pull only what changed.

New to Sonar? Start with the [Sonar API overview](https://docs.turtle.xyz/sdk/sonar/overview.md) for access and what the API covers.

> [!NOTE]
>
> Every call needs a secret key, as `Authorization: Bearer` or on the `X-API-Key` header. See [Authentication](https://docs.turtle.xyz/sdk/sonar/authentication.md).

## Prerequisites

You need two things before the first call:

1. A Turtle organization with Sonar enabled, that you are a member of. The Turtle team enables Sonar per organization.
2. Your **organization ID**: the ID in the dashboard's address bar while you are in the organization, `dashboard.turtle.xyz/organizations/<organizationId>/...`.

## Step 1: Create a personal API key

Sign in to the [dashboard](https://dashboard.turtle.xyz), open your [account page](https://dashboard.turtle.xyz/account), and create a key under **Personal API Keys**. Any member can, not only admins.

The key is shown once. Keep it in a secret store, never in code or chat. It acts as you, in every organization you are a member of, so what your script does is recorded as you.

## Step 2: Make a first read

Fetch the deals that have gone quiet the longest.

```bash curl theme={null}
export SONAR_BASE="https://sonar.turtle.xyz/v1"
export SONAR_API_KEY="sk_live_xxxxx"   # your personal key
export ORG_ID="YOUR_ORGANIZATION_ID"

curl "$SONAR_BASE/organization/$ORG_ID/engagements?sort=stale&limit=2" \
  -H "Authorization: Bearer $SONAR_API_KEY"
```

```typescript TypeScript theme={null}
const base = `https://sonar.turtle.xyz/v1/organization/${orgId}`;
const headers = { Authorization: `Bearer ${process.env.SONAR_API_KEY}` };

const res = await fetch(`${base}/engagements?sort=stale&limit=2`, { headers });
const { engagements, nextCursor } = await res.json();
```

A `200` with an `engagements` array means you are in:

```json theme={null}
{
  "engagements": [
    {
      "id": "<uuid>",
      "title": "…",
      "stage": "…",
      "counterpartyId": "<uuid>",
      "counterparty": { "id": "<uuid>", "name": "…" },
      "updatedAt": "…"
    }
  ],
  "nextCursor": "…"
}
```

A `401` means the key is missing, wrong, expired or not a secret key. A `403` means the organization does not have Sonar, or you are not a member of it.

## Step 3: Keep in sync

Store the time of your last pull and pass it back as `updatedAfter`. Page with `cursor` until no `nextCursor` comes back. Take the timestamp a minute before the pull starts, so a change made while you page, or missed because your clock runs ahead of ours, is picked up next time. A record can then come back twice, so store records by `id`.

```typescript TypeScript theme={null}
async function pullEngagements(since?: string) {
  const startedAt = new Date(Date.now() - 60_000).toISOString();
  const rows = [];
  let cursor: string | undefined;

  do {
    const params = new URLSearchParams({ limit: '100' });
    if (since) params.set('updatedAfter', since);
    if (cursor) params.set('cursor', cursor);

    const res = await fetch(`${base}/engagements?${params}`, { headers });
    if (!res.ok) throw new Error(`Sonar answered ${res.status}: ${await res.text()}`);
    const page = await res.json();
    rows.push(...(page.engagements ?? []));
    cursor = page.nextCursor;
  } while (cursor);

  return { rows, nextSince: startedAt };
}
```

The same loop works on the listings of organizations, contacts, threads, tasks, documents, wallets and reports. Events are different, because they never change: to follow new ones, see [Reading](https://docs.turtle.xyz/sdk/sonar/conventions.md#reading).

## Step 4: Make a write

Move a deal to another stage. With a personal key the write is recorded as yours, so the `rationale` is optional, and it applies at once.

```bash curl theme={null}
curl -X PUT "$SONAR_BASE/organization/$ORG_ID/engagements/ENGAGEMENT_ID" \
  -H "Authorization: Bearer $SONAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "stage": "Contract",
    "rationale": "The counterparty accepted the terms, so the deal moves to contract."
  }'
```

```typescript TypeScript theme={null}
const res = await fetch(`${base}/engagements/${engagementId}`, {
  method: 'PUT',
  headers: { ...headers, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    stage: 'Contract', // one of the stages the deal's board declares
    rationale: 'The counterparty accepted the terms, so the deal moves to contract.',
  }),
});
const { applied, entityId, mutation } = await res.json();
```

`applied: true` means the change is in the log, and `mutation` is its row. The stage must be one the deal's board declares: read it with `GET /pipelines/{pipelineId}`.

A tool that writes on its own judgement, such as an AI assistant, should send `"source": "ai"` so the write is recorded as your agent's. It then owes evidence and passes the same approvals as Sonar's agent. See [Writing](https://docs.turtle.xyz/sdk/sonar/conventions.md#writing).

## What's next

* **[Authentication](https://docs.turtle.xyz/sdk/sonar/authentication.md)** - Personal and organization keys, and what each may read and write.
* **[Conventions](https://docs.turtle.xyz/sdk/sonar/conventions.md)** - Filters, sorting, evidence, drafts, errors and limits.
* **[Inbound webhooks](https://docs.turtle.xyz/sdk/sonar/webhooks.md)** - Send a form, a zap or your backend's events into Sonar.
* **[List Engagements](https://docs.turtle.xyz/sdk/sonar/reference/sonar/list-engagements)** - Every filter a deal listing takes, in the API reference.


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