# Conventions

Requests, scopes, personal keys, errors, rate limits, retries, pagination, incremental sync, IDs, bulk requests and course write concurrency.

> Part of [Tayl's API docs](https://tayl.pro/api/v1/docs.md): requests go to `https://tayl.pro` with `Authorization: Bearer <key>`, and [Errors](https://tayl.pro/api/v1/docs/errors.md) explains every error code.

## Requests

Send every request over HTTPS with `Authorization: Bearer tayl_live_…` and `Accept: application/json`. Send JSON bodies with `Content-Type: application/json`, as valid JSON without comments or duplicate keys, and booleans as JSON `true` or `false`.

Requests are checked strictly rather than quietly coerced:

- An unknown query parameter returns `400 unknown_query_param`. An invalid filter value, timestamp, boolean or pagination value, or malformed JSON, returns `400 bad_request`.
- Operations that take no body, such as deletes, archiving a course or rotating a webhook secret, act only on what their URL names. A body sent to one returns `400 unexpected_request_body`; an empty body or `{}` is fine.

Routes use `PATCH` for updates, which merge: fields you leave out keep their values. Course drafts and their chapters, pages and outline also take `PUT`, which replaces them.

## Scopes

Each operation needs one scope, shown beside it as **Scope**. A key holds the scopes an organisation admin chose for it:

- `users:read`, `users:write`: people.
- `groups:read`, `groups:write`: groups, group categories and memberships.
- `content:read`: courses, pathways and signable documents, and course drafts and their content.
- `content:write`: creating courses, changing and discarding drafts, deleting courses that were never published, and uploading images.
- `content:publish`: publishing, archiving and unarchiving courses.
- `content:settings`: the settings of Tayl's own courses, which only Tayl staff's own keys hold.
- `assignments:read`, `assignments:write`: assignments.
- `tasks:read`, `completions:read`, `certificates:read`: learner tasks, completions, and certificates and their PDFs.
- `reports:read`: training summaries by organisation, group and content, which count people without naming them.
- `webhooks:manage`: webhook endpoints and deliveries. Subscribing to an event also needs the read scope of what it carries, such as `users:read` for `user.*` events.

A `403 insufficient_scope` names the scope that's missing. An organisation admin can add it to an organisation key in Tayl; for a personal key, its person creates a new key that includes it.

## Personal keys

A personal key acts as its person. Whatever scopes it holds, it reaches only what they can in Tayl:

- Records outside their reach read as `404 not_found`, as if they weren't there. A manager reaches the people in the groups they manage, those groups, their people's tasks, completions and certificates, and assignments with any target among them.
- An action their role doesn't allow returns `403 not_permitted`, such as a manager changing an admin or creating a group, or changing an assignment that also reaches people they don't manage. Don't retry it.
- A course's content, published or draft, is for people who author courses, as it holds the answers to its quizzes. Others, such as managers, list courses and read each one's details.
- A conflict's `existing_id` names the other record only if they can see it.
- While an admin has personal keys turned off, every request except `GET /api/v1/api-keys/current` returns `403 personal_keys_disabled`. While the person can't use the organisation in Tayl, such as during maintenance, it returns `403 organisation_unavailable`.
- Once the person loses access to the organisation, the key returns `401 invalid_token`.

Every change records the person, the key and whether it came through the API or an AI assistant.

## Errors

Errors share one envelope:

```json
{
  "errors": [
    {
      "code": "insufficient_scope",
      "message": "Token lacks the required scope: tasks:read",
      "documentation_url": "https://tayl.pro/api/v1/docs/errors#insufficient_scope"
    }
  ],
  "request_id": "019cf5b0-7c00-7000-8000-000000000099"
}
```

Branch on the HTTP status and `code`, never on `message`. Validation errors add `field` and a JSON Pointer in `pointer`, and an external-ID conflict adds `existing_id` when the key can see that record. Each error's `documentation_url` links its entry in [Errors](https://tayl.pro/api/v1/docs/errors.md), which says what the code means and how to fix it. Bulk results carry their own `errors` the same way.

`request_id` also comes back in the `X-Request-Id` header. Record it with a failed call, without the key or any sensitive body, and include it with the time of the call when asking Tayl for support. An unexpected `500` can come from outside the API with a different JSON body, so parse errors defensively.

While an organisation's account needs attention, because its plan does or Tayl has put it on hold, every request except `GET /api/v1/api-keys/current` returns `402 organisation_restricted` until it's resolved in Tayl; the self-check's `restriction` gives the reason. Don't retry it automatically.

## Rate limits and retries

Each key has its own limit of requests per second, and requests without a key are limited to 30 a minute from one IP address. A `429` carries `Retry-After` in seconds, with `X-RateLimit-Limit`, `X-RateLimit-Remaining` and `X-RateLimit-Reset`. Wait at least `Retry-After`, then retry with jitter.

Retry network failures and `5xx` responses on reads with bounded exponential backoff. A timeout or `5xx` on a write doesn't prove it failed: before retrying a create or update, look for the resource by its Tayl UUID or your `external_id`, or the retry may duplicate it or repeat a side effect. Don't retry a `4xx` unchanged; fix the key, scope, ID or body first. Put a limit on attempts and report a failure once it's reached.

## Pagination

Lists take one-based `page` and `per_page` parameters: 25 by default, 100 at most. Responses carry a `meta` object with the current page and totals, and an RFC 8288 `Link` header with `first`, `prev`, `next` and `last` URLs where they apply:

```http
Link: <https://tayl.pro/api/v1/tasks?page=2&per_page=100>; rel="next", <https://tayl.pro/api/v1/tasks?page=8&per_page=100>; rel="last"
```

Follow the `rel="next"` URL until there isn't one rather than building page numbers yourself, and check it's HTTPS and the same origin before sending the key to it. Records can move while you page through a list, so expect duplicates.

## Incremental sync

Lists of tasks, completions, certificates, users, groups, assignments and content offer a `since` filter: an inclusive ISO 8601 timestamp of changes to include. To sync:

1. Note the time just before you start reading.
2. Read every page, upserting each record by its `id`. Records at the boundary can repeat, so processing must be idempotent.
3. Only once every page has succeeded, use the time you noted as the next `since`.

`since` isn't a snapshot, and each list's description says which changes count. Business dates such as `completed_after` and `issued_after` aren't safe cursors, because an old completion can be recorded today. Lists don't report every deletion or removed relationship, so run a full reconciliation regularly; webhooks reduce delay but don't replace it.

## IDs and external IDs

Tayl's UUIDs are opaque strings: don't parse them or infer an order from them. People, groups, group categories and assignments can also carry your own `external_id`, unique within the organisation. Use an immutable ID from your source system, not an email address or name, and address records by it with the `/by-external-id/…` operations, URL-encoding it in the path. Another record already using an `external_id` returns `409 duplicate_external_id`, with its UUID in `existing_id` when the key can see it. Archiving or deleting a record releases its `external_id`.

## Bulk requests

Bulk user and group-membership requests take up to 500 entries and process each one independently. The overall status summarises the result:

- `201` or `200`: every entry succeeded. `200` can mean some memberships were already as requested.
- `207 Multi-Status`: some entries succeeded and some failed.
- `422`: no entry succeeded, or the request itself was invalid.

Check every entry's result, even on `200` and `201`, and retry only failed entries whose error is temporary. Replaying a whole batch can repeat what already succeeded.

## Concurrency on course writes

Every course draft write needs the `etag` from your last read as the `If-Match` header, or returns `428 precondition_required`. If the course changed since, including in the course builder, it returns `412 version_changed`: read it again and reapply your changes. One etag covers the whole course, and each write returns the new one, so send the latest each time.
