# Pagination, idempotency and limits

> Three rules every call follows: lists page with a cursor, retries are safe with an `Idempotency-Key`, and each connection has a limit per minute.

## Pagination

Lists come back like this. Any summary fields go in `meta`.

```json
{
  "data": [ { … }, { … } ],
  "nextCursor": "eyJvIjoyMH0",
  "hasMore": true
}
```

- `?limit=` runs from 1 to 50. The default is 20.
- Every list pages this way, to the end. `GET /creators` and `GET /marketplace/campaigns` are ranked afresh on each call, so a creator can move between pages while you read: dedupe on `id`.
- `?cursor=` takes the `nextCursor` from the page before. Treat it as opaque: don't build or change one.
- Stop when `hasMore` is false. Then `nextCursor` is `null`.

Node:

```js
let cursor = null;
do {
  const url = new URL("https://yapfactory.com/api/v1/deals");
  url.searchParams.set("limit", "50");
  if (cursor) url.searchParams.set("cursor", cursor);
  const res = await fetch(url, { headers: { Authorization: `Bearer ${process.env.YAP_API_KEY}` } });
  const page = await res.json();
  for (const deal of page.data) console.log(deal.id);
  cursor = page.hasMore ? page.nextCursor : null;
} while (cursor);
```

Python:

```python
import os, requests

headers = {"Authorization": f"Bearer {os.environ['YAP_API_KEY']}"}
cursor = None
while True:
    params = {"limit": 50, **({"cursor": cursor} if cursor else {})}
    page = requests.get("https://yapfactory.com/api/v1/deals", headers=headers, params=params).json()
    for deal in page["data"]:
        print(deal["id"])
    if not page["hasMore"]:
        break
    cursor = page["nextCursor"]
```

## Idempotency

Networks drop answers. An `Idempotency-Key` lets you retry without doing the thing twice.

- Send an `Idempotency-Key` header, 8 to 200 characters, on any `POST`, `PATCH` or `DELETE`. A UUID is ideal.
- It's **required** on `outbound` operations, the ones that tell other people (publishing, inviting, answering, messaging, scheduling a post). Without one they return `400`.
- A success is stored for 24 hours. The same key with the same body returns that stored response, with the header `Idempotent-Replayed: true`, and nothing runs again.
- A failure isn't stored. Send the same key again once whatever stopped it has changed, and it runs then.
- The same key with a different body returns `409 idempotency_conflict`.
- The same key while the first request is still running returns `409 conflict`. Wait a moment and send it again.
- Keys belong to the connection (the OAuth grant or the API key), so two apps can't collide.

```bash
curl -X POST "https://yapfactory.com/api/v1/campaigns/$CAMPAIGN_ID/publish" \
  -H "Authorization: Bearer $YAP_API_KEY" \
  -H "Idempotency-Key: 4f1c2b0e-7d4a-4f43-9a51-0c6a1b9e2d7f"
```

## Rate limits

Limits are counted every minute, per connection and per person. The REST API and the MCP server share them.

| What | Each connection | All of a person's connections |
| --- | --- | --- |
| Reads | 120 a minute | 300 a minute |
| Changes (`write` and `outbound`) | 20 a minute | 40 a minute |
| `outbound` alone | 10 a minute | 15 a minute |

Every response from an operation carries these, counted for the connection:

| Header | Means |
| --- | --- |
| `RateLimit-Limit` | The limit for this kind of call. |
| `RateLimit-Remaining` | Calls left in this window. |
| `RateLimit-Reset` | Seconds until the window resets. |
| `Retry-After` | On a `429` only: seconds to wait before trying again. |

Some things have their own limits, the same as on the site, such as 8 hand-picked creators per campaign. Past one, the call returns `409 conflict` with a sentence that says so.

## Versions

- The version is in the path: `/api/v1`. Within v1 we only add: new operations, new optional inputs, new fields in a response, new webhook event types and new values for a status. Write code that ignores what it doesn't know.
- Anything that would break working code (removing or renaming a field, changing a type, tightening an input) waits for `/api/v2`. We'd run v1 beside it for at least 12 months and email everyone with a live key or connection.
- Every change is in the [changelog](https://yapfactory.com/developers/changelog.md).

## Keys belong on a server

Never put an API key in a web page or a mobile app: anyone can read it there. The API answers browsers (CORS is open) so that apps using OAuth can call it with a person's own token; keys are for your server and your scripts. If a key gets out, revoke it in **Settings → AI agents** and make a new one.

## Formats

- **Money** is `{ "minor": 1500, "currency": "USD", "formatted": "$15.00" }`. Integer minor units (pence or cents) everywhere, never decimals.
- **Times** are ISO 8601 strings in UTC.
- **Ids** are UUIDs.

---

HTML version: https://yapfactory.com/developers/pagination
