# API conventions: auth, envelope, errors, pagination (https://docs.opensequence.dev/api/conventions)

How every OpenSequence REST endpoint behaves: keys and scopes, the response envelope, field names, ids, pagination, rate limits and dates.

The rules every endpoint follows, so you can call the raw REST API from any language.
The API lives under `/v1` on your instance; it also serves Swagger UI at `/docs` and the OpenAPI document at `/docs/json`.

## Authentication [#authentication]

```bash
curl $SEQ/v1/sequences -H "Authorization: Bearer $KEY"
```

```python
r = requests.get(f"{SEQ}/v1/sequences", headers={"Authorization": f"Bearer {KEY}"})
```

| Caller | Header | Covers |
|---|---|---|
| Project API key | `Bearer sk_live_…` | everything except `/v1/admin/*` |
| Operator token (`SEQ_ADMIN_TOKEN`) | `Bearer <token>` | `/v1/admin/*` only: projects, keys, platform health |

Keys are stored as SHA-256 hashes, shown once, and revocable. Each key belongs to one project; another project's ids return `404`.

## Scopes [#scopes]

Scopes rank `read` < `enroll` < `admin`. A higher scope can do everything a lower one can. Missing scope: `403 INSUFFICIENT_SCOPE`.

| Scope | Can |
|---|---|
| `read` | every `GET`, `contacts:status`, `sequences:validate`, `enrollments:preview`, `/v1/schema*`, `GET /v1/project` |
| `enroll` | contacts (upsert, batch, patch, events), enroll, pause, resume, complete and cancel tasks |
| `admin` | everything else: senders, sequences, publish, exit, outreach consent, settings, credentials, keys, flags, webhooks |

Give `enroll` to code that reacts to user actions. Keep `admin` for configuration.

## Response envelope [#response-envelope]

```json
{ "success": true,  "data": { … }, "meta": { "request_id": "req_…" } }
{ "success": false, "error": { "code": "ALREADY_IN_SEQUENCE", "message": "…", "details": { … } }, "meta": { "request_id": "req_…" } }
```

Branch on `error.code`; every code is on [Errors](/api/errors). Quote `request_id` when you ask for help: it is on every log line of the request. An incoming `X-Request-Id` header is not used yet. Creation returns `201`, everything else `200`.

## Field names [#field-names]

- You send **snake_case**: bodies and query strings (`first_name`, `lock_version`).
- Computed results come back **snake_case**: errors, `meta`, enroll results, analytics, webhook endpoints and every webhook payload.
- Stored records come back **camelCase**: contacts, senders, sequences, versions, enrollments, runs, messages, signals, tasks (`externalId`, `lockVersion`).

Each endpoint's reference page shows the exact shape, and [`openapi.json`](/openapi.json) has it under `components.schemas`.

## Ids [#ids]

Service ids are prefixed ULIDs (`ctc_`, `seq_`, `enr_`, …). Wherever a sender, contact or sequence `{id}` appears, your own id works too. If your id could look like a service id, prefix it with `ext:`.

## Pagination [#pagination]

```bash
curl "$SEQ/v1/enrollments?status=active&limit=100&updated_since=2026-10-13T00:00:00Z" -H "Authorization: Bearer $KEY"
```

`limit` is 1 to 200 (default 50). Pass `meta.next_cursor` back as `cursor` until it is `null`. `data` is the array itself. `updated_since` lets a client without webhooks poll for changes.

## Bodies [#bodies]

JSON only, 2 MB at most. A `POST` with no body is fine where the body is optional (`publish`, `pause`, `exit`). Strict schemas reject unknown fields with `400 VALIDATION_ERROR`.

## Rate limits [#rate-limits]

600 requests per minute per key (per client IP without one), then `429` with `Retry-After`. With several API replicas and no shared store, the count is per replica.

## Idempotency [#idempotency]

`PUT` upserts. Contact events are keyed by your event `id`. Enrolling twice is refused, not duplicated. The `Idempotency-Key` header is not implemented yet, so a repeated `POST /v1/sequences` creates a second sequence.

## Dates, zones and phones [#dates-zones-and-phones]

Timestamps are ISO 8601 UTC. Time zones are IANA names. Durations match `^\d+(m|h|d|bd)$`. Phones are E.164 and countries ISO 3166-1 alpha-2.

## Health [#health]

`GET /healthz` (liveness) and `GET /readyz` (database migrated and reachable) need no auth. `GET /v1/engine/health` (`admin`) returns kill switches, queue depths and the last run of each job. See [Observability](/docs/self-hosting/observability).
