# Conventions

> How the LemuAI API pages through results, which limits apply, how to retry safely, and how to avoid counting the same order twice.

URL: https://docs.lemuai.com/api-reference/conventions
For: Developers
Checked against LemuAI app version 1.0.12
Related: [Overview](https://docs.lemuai.com/api-reference/overview), [Errors](https://docs.lemuai.com/api-reference/errors), [Quickstart](https://docs.lemuai.com/api-reference/quickstart)

## Requests and responses

- Base URL `https://app.lemuai.com/api/v1`, HTTPS only.
- Authenticate with the `X-API-Key` header ([Authentication](https://docs.lemuai.com/api-reference/authentication)).
- Send JSON with `Content-Type: application/json`; answers are JSON.
- Times are ISO 8601. Times LemuAI returns are in UTC.
- Phone numbers are international format with country code: `+919876543210`.
- Money is in major units (`1499.5` rupees, not paise) with an ISO 4217 `currency` (`INR`).

## Paging through results

List endpoints use one of three styles. Each endpoint's page lists its own parameters.

| Style | Parameters | Next page | Used by |
|---|---|---|---|
| Page number | `page` (from 1), `limit` | `page + 1` until `page` reaches `total_pages` (or a page comes back short) | [List Contacts](https://docs.lemuai.com/api-reference/contacts/list) (limit up to 500), [List Contact Lists](https://docs.lemuai.com/api-reference/contacts/lists) (up to 100), [List Contacts in a List](https://docs.lemuai.com/api-reference/contacts/list-members) (up to 1,000), [List Calls](https://docs.lemuai.com/api-reference/agents/runs/list) |
| Offset | `limit`, `offset` | `offset + limit` until `offset` reaches `total` | [List Events](https://docs.lemuai.com/api-reference/events/list) (limit up to 500) |
| Cursor (newest first) | `limit`, `before` / `before_id` | Pass `next_before` / `next_before_id` from the last answer; stop when it's `null` | [Contact Activity](https://docs.lemuai.com/api-reference/contacts/activity), [Contact Messages](https://docs.lemuai.com/api-reference/contacts/messages) (limit up to 200) |

Example, all contacts in pages of 500:
```python
import os, requests

page, contacts = 1, []
while True:
    r = requests.get(
        "https://app.lemuai.com/api/v1/contacts",
        headers={"X-API-Key": os.environ["LEMU_API_KEY"]},
        params={"page": page, "limit": 500},
        timeout=30,
    )
    r.raise_for_status()
    body = r.json()
    contacts += body["contacts"]
    if page >= body["total_pages"]:
        break
    page += 1
```

## Sizes and limits

| What | Limit |
|---|---|
| [Bulk Upsert Contacts](https://docs.lemuai.com/api-reference/contacts/bulk-upsert) | 5,000 contacts per request |
| [Create or Update Records in Bulk](https://docs.lemuai.com/api-reference/records/batch) | 1,000 records per request |
| One custom record | 16 KB of data, key up to 255 characters, text values up to 2,000 characters |
| Event name | 1 to 100 characters |
| Segment rules | 30 per segment; **is in** lists up to 500 values |

The REST API has no fixed per-key request rate today, but be a good citizen: use the bulk endpoints for imports instead of one request per row, and keep to a few requests per second per key. Website and app events sent with a tracking-source write key are rate limited per source and per device, and answer `429 Too many requests` when exceeded.

## Retries

- **Retry** on network errors, `429`, and `5xx`, with a growing wait (1 s, 2 s, 4 s… up to about a minute) and a little randomness.
- **Don't retry** `400`, `401`, `403`, `404` or `422` without changing the request: they won't succeed as sent. The `detail` field says why ([Errors](https://docs.lemuai.com/api-reference/errors)).

## Sending the same thing twice

| Request | Safe to repeat? |
|---|---|
| Upsert a contact, bulk upsert, create or update a record | Yes. They match on phone / key and update; repeating changes nothing |
| Send an event (`Purchase` and other conversion events) | Orders count once per `order_id`. Always send `order_id`, so a retried order isn't counted twice. Each event still starts journeys that listen for it, so avoid resending non-order events |
| Trigger an outbound call | No. Each request places a call. Retry only when you're sure the first request failed (a network error before any answer, or a `4xx`/`5xx`) |
| Send a transactional email | No. Each request sends an email |

## OpenAPI

The public endpoints are described in an OpenAPI 3 file: [`/openapi.json`](https://docs.lemuai.com/openapi.json). Import it into Postman or Insomnia, generate a client, or give it to your coding assistant.
