# The API

Everything the page does, it does through the API under `/api/v1/`, and so can any other program: a script, a monitoring system, a client of your own. The Desktop App is one.

## The promise

- Within `v1`, requests, fields, states, codes and words are added. None is taken away or given another meaning.
- A program leaves out what it does not know: an answer with more in it than expected is not wrong.
- A change that would break a program gets `/api/v2/`, beside `v1`. `v1` is then served on for at least twelve months and two minor releases, whichever is longer, with `Deprecation` and `Sunset` headers.
- `qkvm/api_v1.json`, in every installed copy, lists each promised request: who may ask, what it takes and answers, its refusal codes, and since which version it is promised.
- Other requests under `/api/v1/` are the page's own and change with it.

The promised requests are listed on [API requests](https://qkvm.com/docs/web/api-requests).

## Three ways in

| Who | How | Roles |
|---|---|---|
| A person | `POST /api/v1/login`, then the `qkvm_session` cookie; every request that changes something also carries `X-QKVM: 1` | viewer, operator, admin |
| A script | `Authorization: Bearer qkvm_...` with every request, no cookie | viewer or operator, never admin |
| A wall screen | `POST /api/v1/display` with the key from its link | viewer |

Bodies are JSON with a `Content-Length`, 64 KiB at the most, but for the two that carry a file. A field that is wrong is answered 422 `invalid`, and the message names the field, never its value.

## Errors

Every refusal is `{"code": "...", "message": "..."}`. `code` is one word, for a program; `message` is a sentence fit to show a person.

| Status | Common codes | Means |
|---|---|---|
| 401 | `unauthorized` | sign in again; do not loop |
| 403 | `forbidden`, `not_allowed` | the header is missing, or the role does not allow it |
| 404 | `not_found`, `no_picture` | not there, or not given to this account |
| 409 | `busy`, `machine_changed` | something else is under way, or the tile now points at another machine |
| 422 | `invalid` | a field is wrong |
| 429 | `rate_limited` | wait the seconds `Retry-After` says |
| 502 | `unreachable`, `auth_failed`, `action_failed` | the device's |
| 503 | `locked`, `no_stream` | the wall cannot reach its devices yet, or ask again shortly |
| 504 | `timeout` | the device did not answer |

After an action, `unreachable` means nothing was sent. `timeout` and `dropped` mean it may have been done: never offer a blind retry of a power button.

## The right machine

- Send the machine's `target`, from `GET /api/v1/wall`, with an action. Where an admin has pointed the tile at another machine since, the action is refused with 409 `machine_changed` and nothing is sent.
- On a KVM with several inputs, send `input` (the status's `ports.active`) with keys and text. Where another input is on screen, they are refused with 409 `input_changed`.

## What a machine can do

Each machine's `can` lists what it can be asked: `screen`, `keys`, `text`, `power`, `reset`, `live`, `control`, `media` and more. Build buttons from `can`, never from the kind of device. A word you have not met was added since: leave it out.

## Manners

- Ask for the status, and for a tile's picture, no more often than the wall's `interval`.
- Do not ask in the background for a window nobody sees.
- Ask before power, a reset, a restart, and anything sent to several machines, as the page does.

## Related

- [API requests](https://qkvm.com/docs/web/api-requests)
- [Clients and versions](https://qkvm.com/docs/web/clients-and-versions)
- [Scripts and wall screens](https://qkvm.com/docs/web/scripts-and-wall-screens)

[Full reference](https://github.com/qkvm/qkvm/blob/main/GUIDE.md#the-api)
