# Sync leads

To **sync** leads is to copy new or changed lead data into another system. Copy only the fields the destination
needs. Use CLI browser approval for a one-time local export. Use an API key with only `leads:read` for a scheduled
server sync. Never copy contact values or customer data into an AI prompt.

Before you start, confirm the exact paths, parameters, and response fields in the
[generated OpenAPI contract](https://themochi.app/docs/api/openapi.json).

## Plan the sync

1. Check the current OpenAPI operations and response fields.
2. For the first import, request the first page without `updated_since`. Follow `next_cursor` until it is `null`.
3. Save each page by Mochi lead ID in a durable batch. An **upsert** means update an existing ID or insert a new one.
4. Save the opaque cursor only after that page is durable. Never decode or construct a cursor.
5. After a completed run, save its UTC time as a checkpoint. Pass that time as `updated_since` on the next run. Use
   a small time overlap and duplicate-safe upserts so retries or equal timestamps do not lose leads.
6. Request lead detail only when the list does not contain the field you need.
7. For failures, record the sanitized `X-Request-ID`, method, path, status, and UTC time. Never record the
   authorization header, cursor, response body, or contact data.

## Request leads changed since the last run

Set `MOCHI_UPDATED_SINCE` to the UTC checkpoint from the last completed run. Check the parameter names against
OpenAPI before shipping, because OpenAPI is the current endpoint contract.

### cURL

```bash
curl --fail-with-body --silent --show-error --get \
  "${MOCHI_BASE_URL}/v1/leads/" \
  --header "Authorization: Bearer ${MOCHI_API_KEY}" \
  --header "Accept: application/json" \
  --data-urlencode "updated_since=${MOCHI_UPDATED_SINCE}" \
  --data-urlencode "page_size=100"
```

### Python

```python
import os

import requests

response = requests.get(
    f"{os.environ['MOCHI_BASE_URL']}/v1/leads/",
    headers={"Authorization": f"Bearer {os.environ['MOCHI_API_KEY']}"},
    params={"updated_since": os.environ["MOCHI_UPDATED_SINCE"], "page_size": 100},
    timeout=30,
)
response.raise_for_status()
page = response.json()
print({"count": len(page["data"]), "has_next_page": page["next_cursor"] is not None})
```

### Node.js / TypeScript

```typescript
const baseUrl = process.env.MOCHI_BASE_URL ?? "https://api.themochi.app";
const url = new URL("/v1/leads/", baseUrl);
url.searchParams.set("updated_since", process.env.MOCHI_UPDATED_SINCE!);
url.searchParams.set("page_size", "100");
const response = await fetch(url, {
  headers: { Authorization: `Bearer ${process.env.MOCHI_API_KEY}` },
  signal: AbortSignal.timeout(30_000),
});
if (!response.ok) throw new Error(`Mochi HTTP ${response.status}`);
const page = (await response.json()) as { data: Array<{ id: number }>; next_cursor: string | null };
console.log({ count: page.data.length, hasNextPage: page.next_cursor !== null });
```

### PHP

```php
<?php
$query = http_build_query([
    "updated_since" => getenv("MOCHI_UPDATED_SINCE"),
    "page_size" => 100,
]);
$handle = curl_init((getenv("MOCHI_BASE_URL") ?: "https://api.themochi.app") . "/v1/leads/?" . $query);
curl_setopt_array($handle, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => ["Authorization: Bearer " . getenv("MOCHI_API_KEY")],
]);
$body = curl_exec($handle);
$status = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
curl_close($handle);
if ($body === false || $status < 200 || $status >= 300) throw new RuntimeException("Mochi read failed");
$page = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
echo json_encode(["count" => count($page["data"]), "has_next_page" => $page["next_cursor"] !== null]);
```

## Instagram, WhatsApp and Messenger leads

Mochi supports Instagram and WhatsApp, and Facebook Messenger for organizations that have it. A lead who has only
written to your organization on WhatsApp or Messenger has no Instagram account. Lead responses carry these fields for
that case:

- `instagram_username` and `instagram_name` are `null` for a lead with no Instagram account (a WhatsApp-only or
  Messenger-only lead).
  A lead with an Instagram account returns the same strings as before. Store both as nullable columns.
- `display_name` is the name to show for any lead: the Instagram name or username, else the WhatsApp profile name,
  else the WhatsApp number in international format (`+` and digits), else the lead's Facebook name (`"Facebook user"`
  when Facebook does not share it). It is an empty string when none is known. Use
  it instead of `instagram_username` when you need a label for every lead.
- `channels` lists where the lead can be reached: any of `"instagram"`, `"whatsapp"` and `"messenger"`, in that
  order. To keep a sync Instagram-only, skip leads whose `channels` does not contain `"instagram"`. Treat a value you
  do not recognise as another channel rather than an error.
- `conversations` lists every conversation of the lead as `{"id": ..., "channel": ...}`, the
  main one first. `channel` is `"instagram"`, `"whatsapp"` or `"messenger"`. A lead on several channels has at least
  one conversation per channel; a merged lead can have more than one on the same channel. The list is empty when the lead has no conversation yet.

Until your organization connects a WhatsApp number or a Facebook Page, every lead is an Instagram lead: `channels` is
`["instagram"]` and the Instagram fields are filled as before. Check the
[generated OpenAPI contract](https://themochi.app/docs/api/openapi.json) for which
operations return each field.

## Filter by location and link to the Mochi inbox

Every lead carries three fields for this:

- `location` is `{"country": "DE", "city": "Berlin"}`: the ISO country code and city of the lead's most recent click
  on a tracked Mochi link. It is where the lead's device was when they clicked, not a verified home address. It is
  `null` when the lead never clicked a tracked link or the click could not be placed, and `city` is `null` when only
  the country is known. Instagram does not share where a lead lives, so only leads who clicked have a location.
- `conversation_id` is the lead's main Mochi conversation, the first entry of `conversations`. It is not the lead
  `id`.
- `inbox_url` opens that conversation in the Mochi inbox. It only works for someone signed in to Mochi as a member
  of your organization. Both are `null` when the lead has no conversation yet.

To list leads by location, repeat `location`: `DE` for a whole country, `DE:Berlin` for one city in it. It matches
the lead's latest click only, exactly like the location filter in the app, so a lead who clicked from Germany and
later from Austria is an Austrian lead.

```bash
curl --fail-with-body --silent --show-error --get \
  "${MOCHI_BASE_URL}/v1/leads/" \
  --header "Authorization: Bearer ${MOCHI_API_KEY}" \
  --data-urlencode "location=DE" \
  --data-urlencode "location=AT:Vienna" \
  --data-urlencode "page_size=100"
```

A new click does not change a lead's `updated_at`, so an `updated_since` run does not pick up a changed location.
When locations matter, re-read them with the `location` filter or a full sync instead of relying on the checkpoint.

## Pagination, retries, and verification

Pass `next_cursor` back unchanged and keep the same `updated_since` throughout one run. Retry only the read
failures described in [Fix an API error](https://themochi.app/docs/api/troubleshooting-and-safety/errors) and [Handle rate limits](https://themochi.app/docs/api/troubleshooting-and-safety/rate-limits). Limit the number of
attempts. Stop if Mochi rejects a cursor; silently restarting at page one can duplicate an entire export.

Verify the destination count, unique Mochi IDs, first/last checkpoint, page count, and a small set of non-sensitive fields. A read sync has no Mochi rollback; rollback the destination batch and retain the previous durable checkpoint if validation fails.
