# API introduction

> Use the Clokeep API to manage servers, plans and backups from your own tools.

The Clokeep API lets your own tools work with your Clokeep records. You read and change servers, plans, schedules and storages. You start backup runs, list backups, download them and queue restores. Every endpoint works on the client your token belongs to, and on the shared items every client can use.

## Base URL

Every endpoint sits under one base URL:

```text Base URL
https://app.clokeep.com/api/v1
```

Endpoint pages show the full path, which starts with `/api/v1`. In running text, paths leave `/api/v1` out. For example, `GET /platform` calls `https://app.clokeep.com/api/v1/platform`. Send request bodies as JSON, with `Content-Type: application/json`. Answers with a body are JSON, errors included. The two download endpoints are the exception: they send the file.

## Authentication

Every request carries an API token in the `Authorization` header:

```text Header
Authorization: Bearer YOUR_API_TOKEN
```

### Create a token

Only an Admin of the client can create a token.

<Steps>
<Step title="Open API Tokens">
Sign in at [app.clokeep.com](https://app.clokeep.com). In the sidebar, under **Access**, open **API Tokens**.
</Step>
<Step title="Fill in the token">
Click **Create token**. Enter a **Token name**, such as `CI pipeline`.

Under **Abilities**, tick **Read**, **Write** or both. **Read** is ticked by default. Under **Expiration**, pick how long the token works: 1 hour, 5 hours, 24 hours, 1 month, 6 months or **Does not expire**. The default is 1 month.
</Step>
<Step title="Copy the token">
Click **Create token**. The token appears once, in the **API token created** dialog. Copy it before you click **Done**. You cannot see it again.
</Step>
</Steps>

To stop a token, click **Revoke** next to it on the **API Tokens** page and confirm with **Revoke token**. How to choose a token's abilities and expiry, keep it safe and replace it is on [Create and manage API tokens](/docs/developers/api-token).

### What a token can do

A token belongs to the client you created it in. It sees only that client's records, plus the shared schedules, compression profiles and plan templates every client can use. Those shared items are read-only.

- **Read** (`read`) lets the token call every `GET` endpoint.
- **Write** (`write`) lets the token call every endpoint, including create, change, delete, run, cancel and restore.

The token acts as you. It works only while your role allows the call: Admin, Operator or Viewer for `read`, Admin or Operator for `write`. Clokeep checks your role on every request.

A `read` token cannot call an endpoint that changes something. The answer is `403`.

### Make your first call

Call `GET /platform` to check your token:

```bash cURL
curl 'https://app.clokeep.com/api/v1/platform' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -H 'Accept: application/json'
```

A working token gets `200` and a JSON object. Its `api_version` is `v1`. `panel_egress_ips` lists the addresses the panel connects to storages from, empty when Clokeep has not published them. Under `client`, `slug` is the short name of your client, `limits` is its cap for each resource, `null` when there is no cap, and `usage` is what it uses now. The [endpoint page](/docs/api-reference/platform/show-the-platform-and-your-client) shows the full answer.

A missing, wrong, revoked or expired token gets `401`.

## Conventions

- A single record comes back under `data`. A list comes back as `data`, with `links` and `meta` beside it.
- A few endpoints answer with their own top-level fields instead, such as `GET /platform` and `POST /storages/{storage}/test`. Each endpoint page shows its answer.
- Update endpoints accept `PUT` and `PATCH` with the same body. This reference lists `PUT`.
- Timestamps are ISO 8601 strings in UTC.
- Record ids are integers.
- A record of another client answers `404`, as if it did not exist.

## Pagination

Lists come in pages of 15 items. The file list of a backup, `GET /artifacts/{artifact}/entries`, has pages of 50. Pick a page with the `page` query parameter. The first page is `page=1`.

`links` holds the URLs of the first, last, previous and next page. `prev` and `next` are `null` when there is no such page. `meta` says where you are: `current_page`, `last_page`, `per_page`, `total`, and `from` and `to` for the items on this page.

These are the `links` and `meta` of `GET /servers?page=2`, for a client with 37 servers. `data` is left out. `meta.links` has one entry for Previous, one for each page and one for Next. The example shows Previous, page 2 and Next, and leaves out the entries for pages 1 and 3.

```json Page 2 of 3
{
  "links": {
    "first": "https://app.clokeep.com/api/v1/servers?page=1",
    "last": "https://app.clokeep.com/api/v1/servers?page=3",
    "prev": "https://app.clokeep.com/api/v1/servers?page=1",
    "next": "https://app.clokeep.com/api/v1/servers?page=3"
  },
  "meta": {
    "current_page": 2,
    "from": 16,
    "last_page": 3,
    "links": [
      {
        "url": "https://app.clokeep.com/api/v1/servers?page=1",
        "label": "&laquo; Previous",
        "page": 1,
        "active": false
      },
      {
        "url": "https://app.clokeep.com/api/v1/servers?page=2",
        "label": "2",
        "page": 2,
        "active": true
      },
      {
        "url": "https://app.clokeep.com/api/v1/servers?page=3",
        "label": "Next &raquo;",
        "page": 3,
        "active": false
      }
    ],
    "path": "https://app.clokeep.com/api/v1/servers",
    "per_page": 15,
    "to": 30,
    "total": 37
  }
}
```

The page links do not keep your filters, such as `server_id`. When you page through a filtered list, add the filter to each URL yourself.

## Errors

An error answer is JSON with a `message`. A `422` for an invalid body or query adds `errors`. A `416` from a download has no body.

| Status | Meaning | Body |
| --- | --- | --- |
| `401` | The token is missing, wrong, revoked or expired. | `{"message": "Unauthenticated."}` |
| `403` | The token may not do this. | `message` says why. See the list below. |
| `404` | The record, or the file you asked for, is not found. | For a record, `message` names the model and the id. For a file, it says what is missing. |
| `409` | The request conflicts with the current state. | `message` says what conflicts. |
| `422` | The body or the query is invalid, or the request cannot be carried out now. | `message`, plus `errors` when a field is invalid. |
| `429` | Too many requests. See [Rate limit](#rate-limit). | `{"message": "Too Many Attempts."}` |
| `503` | A backup, or its file list, cannot be read from storage. | `message` says why. |

A `403` has one of these messages:

- `This token lacks the required ability.`: a `read` token called an endpoint that needs `write`.
- `Your role does not permit this action.`: your role in the client does not allow it.
- `This account is disabled.`: your user account is disabled.
- `This token is not bound to a client you belong to.`: the token has no client, or its client is not one you are a member of. A token created on **API Tokens** always has its client. Removing you from a client deletes your tokens there, so their next call gets `401`.
- `Client suspended`: the client is suspended.
- `Backup downloads are disabled.`: a download while backup downloads are switched off for the client.
- `Shared catalog items are read-only.`: you tried to change or delete a shared schedule or compression profile.

A `404` for a server with id 12 reads `No query results for model [App\Models\Server] 12`.

A `409` comes, for example, from running a plan that already has a run queued or running, or from deleting a storage that a plan still uses.

A `422` for an invalid field names the first problem in `message` and lists every problem by field in `errors`:

```json 422
{
  "message": "The name field is required.",
  "errors": {
    "name": [
      "The name field is required."
    ]
  }
}
```

Some `422` answers have only a `message`. For example, a create over your client's limit answers `Server limit reached (5/5)`, and running a plan whose server is disabled answers `Server "web-01" is disabled; its backups are paused until it is re-enabled.`

A `503` reads, for example, `The file list of this backup is unavailable: storage unreachable.`

Some endpoints have more answers, such as `410` for a download whose storage was deleted. Each endpoint page lists its own.

## Downloads

`GET /artifacts/{artifact}/download` answers in one of two ways:

- `302`, for a backup that is not encrypted on an S3 storage. `Location` is a signed link on the storage, valid for 5 minutes by default. Follow it to get the archive from the bucket.
- `200` with the archive, for any other backup. Send a `Range` header to resume: the answer is `206` with that part, or `416` with no body when the range lies outside the archive.

Do not send your Clokeep token to the storage. The signed link needs no other credential, and an S3 service can refuse a request that carries both. Some HTTP clients forward the `Authorization` header on a redirect: turn that off, or follow the redirect yourself without the header.

```bash cURL
curl -L -o backup.tar.zst 'https://app.clokeep.com/api/v1/artifacts/123/download' \
  -H 'Authorization: Bearer YOUR_API_TOKEN'
```

cURL's `-L` follows the redirect and drops the `Authorization` header when the redirect leads to another host. To resume a download from S3 after the link expired, call the endpoint again for a new link.

The machine that downloads must reach your S3 endpoint. See [S3 storage](/docs/storage/s3#downloads-come-from-the-bucket).

## Rate limit

Each user may make 120 requests a minute in each client. The count covers all of your tokens for that client together. Your tokens in another client have their own 120.

These endpoints are not limited:

- `GET /plan-templates` and `GET /plan-templates/{planTemplate}`
- `GET /backup-runs`, `GET /backup-runs/{backupRun}` and `GET /backup-runs/{backupRun}/logs`
- `GET /artifacts`, `GET /artifacts/{artifact}`, `GET /artifacts/{artifact}/download`, `GET /artifacts/{artifact}/entries` and `GET /artifacts/{artifact}/entry`

Every other endpoint counts. An answer from a limited endpoint carries `X-RateLimit-Limit` and `X-RateLimit-Remaining` headers. Past the limit, the answer is `429` with a `Retry-After` header. It gives the seconds to wait before you try again.

## Next steps

<CardGroup cols="2">
<Card title="Create and manage API tokens" href="/docs/developers/api-token" icon="code">
Choose what a token may do, keep it safe and replace it.
</Card>
<Card title="Your first API call" href="/docs/developers/first-call" icon="code">
Check your token and list your servers.
</Card>
<Card title="Handle errors and rate limits" href="/docs/developers/errors-rate-limits" icon="code">
React to each status code and stay inside the limit.
</Card>
</CardGroup>
