# Conventions

> Pagination, idempotency keys, ticket versions with If-Match, and rate limits in the LemuTickets API

URL: https://docs.lemuai.com/tickets-api/conventions

These rules work the same way on every endpoint that uses them.

## Pagination

List endpoints that can return many items use cursors. Send `limit` to choose the page size, and pass the `next_cursor` from one response as `cursor` in the next request. Keep every other parameter the same.
```bash
curl -G https://api.tickets.lemuai.com/v1/tickets \
  -H "Authorization: Bearer ltk_live_your_api_key" \
  --data-urlencode "q=project = SUP ORDER BY created ASC" \
  --data-urlencode "limit=100"
```
```json
{
  "data": [ ... ],
  "next_cursor": "eyJvIjogMTAwfQ",
  "total": null
}
```

| Field | Description |
|---|---|
| `data` | The items on this page. |
| `next_cursor` | Pass it as `cursor` to get the next page. `null` on the last page. |
| `total` | Only on [List and search tickets](https://docs.lemuai.com/tickets-api/tickets/list): the number of matching tickets when you send `with_total=true`, otherwise `null`. |

Treat the cursor as an opaque string. It is a position in the result list, so if tickets change while you page through them, a ticket can show up twice or move to a page you already read. For a full sync, sort by a field that does not change (`ORDER BY created ASC`) and de-duplicate by `id`.

Shorter lists (comments on a ticket, transitions, projects, webhooks) return everything at once as `{"data": [...]}`, without a cursor.

## Idempotency keys

Networks fail. If you send a request and get no answer, you cannot tell whether it worked. An idempotency key lets you send it again safely.

Add an `Idempotency-Key` header with a value that is unique to the action, for example your own record id:
```bash
curl -X POST https://api.tickets.lemuai.com/v1/tickets \
  -H "Authorization: Bearer ltk_live_your_api_key" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: erp-complaint-58213" \
  -d '{ "project": "SUP", "title": "Refund not received for order 58213" }'
```

- The first request runs normally and its response is saved.
- A later request with the same key and the same body gets the saved response back, with the header `Idempotent-Replayed: true`. Nothing is created twice.
- The same key with a different body fails with `409` and the code `idempotency_mismatch`.

Keys are kept per organization and per endpoint path for 24 hours. Keys can be up to 200 characters; a longer key fails with `422` and the code `idempotency_key_too_long`.

The `Idempotency-Key` header is honored by [Create a ticket](https://docs.lemuai.com/tickets-api/tickets/create) and [Add a comment](https://docs.lemuai.com/tickets-api/tickets/add-comment). Other endpoints ignore it.

Tickets have two more ways to avoid duplicates, which work without the header:

- **`source_ref`**: if a ticket with the same `source` and `source_ref` already exists, creating it again returns the existing ticket with status `200` instead of `201`.
- **`external_id`**: must be unique. Creating a second ticket with the same `external_id` fails with `409` and the code `duplicate_external_id`.

## Versions and If-Match

Every ticket has a `version` number. It goes up every time the ticket changes, including when someone comments, logs work, transitions it, or an automation updates it. The current version is in the ticket's `version` field and in the `ETag` header of [Get a ticket](https://docs.lemuai.com/tickets-api/tickets/get), [Create a ticket](https://docs.lemuai.com/tickets-api/tickets/create) and [Update a ticket](https://docs.lemuai.com/tickets-api/tickets/update).

To make sure you do not overwrite someone else's change, send the version you last read in an `If-Match` header on [Update a ticket](https://docs.lemuai.com/tickets-api/tickets/update) and [Transition a ticket](https://docs.lemuai.com/tickets-api/tickets/transition):
```bash
curl -X PATCH https://api.tickets.lemuai.com/v1/tickets/SUP-1042 \
  -H "Authorization: Bearer ltk_live_your_api_key" \
  -H "Content-Type: application/json" \
  -H "If-Match: 7" \
  -d '{ "priority": "p1" }'
```

If the ticket changed since then, nothing is saved and you get `412`:
```json
{
  "error": {
    "type": "precondition_failed",
    "code": "version_mismatch",
    "message": "The ticket changed since you loaded it",
    "request_id": "4f1d9c2b7a6e4e0f8c3b1a2d5e6f7a8b",
    "data": { "current_version": 8 }
  }
}
```

Get the ticket again, decide whether your change still makes sense, and retry with the new version. `If-Match` accepts the plain number (`7`) or the quoted forms (`"7"`, `W/"7"`). Without `If-Match`, the change is applied to whatever is current.

## Rate limits

Each API key has its own limit per minute, set when the key is created (600 by default). OAuth tokens that act for a user share that user's limit of 1,200 requests per minute. The window slides, so a short burst does not lock you out for a full minute.

Responses carry the current state:

| Header | Description |
|---|---|
| `RateLimit-Limit` | Requests allowed per minute. |
| `RateLimit-Remaining` | Requests left in the current window. |

Over the limit, requests fail with `429`. The `Retry-After` header and the error's `data.retry_after` both say how many seconds to wait:
```json
{
  "error": {
    "type": "rate_limited",
    "code": "rate_limited",
    "message": "Too many requests. Try again in 12 seconds.",
    "request_id": "9a0b1c2d3e4f40a1b2c3d4e5f6a7b8c9",
    "data": { "retry_after": 12, "limit": 600, "window": 60 }
  }
}
```

If you need a higher limit for a key, create a new key with a higher **Requests per minute** value.

## Request ids

Every response has an `X-Request-Id` header, and every error repeats it as `request_id`. You can send your own `X-Request-Id` and it is used instead, which makes it easy to match your logs with ours.
