# Conventions

Source: https://watership.ca/api-docs/docs/reference/conventions

Base URL, authentication, versioning, pagination, and the headers every response carries.

## Base URL [#base-url]

| Server | URL | Status |
|---|---|---|
| Local mock (Prism) | `http://127.0.0.1:4010` | Runs from the spec with `npm run mock` |
| Hosted service | `https://api.watership.ca` | Planned. Not live. |

Every path starts with the major version, such as `/v1/checks`.

## Authentication [#authentication]

Send an API key as a bearer token in the `Authorization` header:

```http
Authorization: Bearer wsk_test_example
```

| Key prefix | Use |
|---|---|
| `wsk_test_` | Testing. The mock accepts any key. |
| `wsk_live_` | Production, when the hosted service exists |

A request with no key, or a key that isn't valid, gets `401` with the problem type `unauthorized` and a `WWW-Authenticate: Bearer realm="watership"` header. Keep keys out of source control and out of browser code.

## Requests and responses [#requests-and-responses]

- Request bodies are JSON, sent with `Content-Type: application/json`. Any other content type gets `415`.
- Successful responses are `application/json`. Errors are `application/problem+json`. See [Errors](https://watership.ca/api-docs/docs/reference/errors).
- Field names are `snake_case`.
- Text is UTF-8. Positions in a finding's `span` count UTF-16 code units for `index` and `length`, and Unicode code points for `column`. See [The finding object](https://watership.ca/api-docs/docs/reference/finding#span).
- A field with no value is `null`, not left out, unless the reference marks it optional.

## Versioning [#versioning]

The API has two version numbers, and they change for different reasons.

| Version | Where | Changes when |
|---|---|---|
| API version | The path: `/v1/` | The contract breaks: a field is removed or renamed, or a type changes |
| Release | The `Watership-Release` header, and `release` in a check result | The Watership checks change: a check is added, removed, or reworded, or the guides they come from change |

Within `v1`, changes are additive: new endpoints, new optional request fields, new response fields, and new values in lists such as `group`. Write clients that ignore fields they don't know and handle enum values they haven't seen.

A release is the version of the Watership checks, such as `0.2.0`. The checks are the Watership Vale package, which is generated from the guides, so the release is that package's version, and a new rule or a changed check in a guide means a new release. A new release can add findings to text that passed before, because a new rule or check now applies. The same text checked against the same release always gets the same findings. To compare results over time, store the release with them.

## Pagination [#pagination]

The list endpoints (`GET /v1/rules`, `GET /v1/terms`, and `GET /v1/voices`) return one page at a time.

| Query parameter | Meaning |
|---|---|
| `limit` | Items per page, from 1 to 100. The default is 25. |
| `cursor` | The `next_cursor` from the previous page. Leave it out for the first page. |

| Response field | Meaning |
|---|---|
| `data` | The items on this page |
| `has_more` | `true` if there's another page |
| `next_cursor` | The cursor for the next page, or `null` on the last page |

Cursors are opaque strings. Use them with the same filters that produced them. [List every rule in a guide](https://watership.ca/api-docs/docs/tasks/list-every-rule) shows a complete loop.

## Response headers [#response-headers]

| Header | On | Meaning |
|---|---|---|
| `Watership-Release` | Every `2xx` | The release of the checks, the Watership Vale package's version |
| `RateLimit-Limit` | Every response | Requests allowed per minute for this key |
| `RateLimit-Remaining` | Every response | Requests left this minute |
| `RateLimit-Reset` | Every response | Seconds until the count resets |
| `Retry-After` | `429` | Seconds to wait before the next request |
| `WWW-Authenticate` | `401` | The scheme the API expects |

The limits themselves are in [Limits](https://watership.ca/api-docs/docs/reference/limits).
