# Run a backup through the API

> Start a plan's backup from a script, follow the run until it ends, and read its log.

A script starts a backup the same way **Run now** does in the panel. The run then goes to the server's agent, and the script follows it until it ends.

## Prerequisites

- A token with the **Write** ability, whose owner is an Admin or Operator in the client. Only an Admin can create one. See [Create and manage API tokens](/docs/developers/api-token).
- A plan whose server has the agent installed. See [Plans](/docs/backups/plans).

## Run the backup

<Steps>
<Step title="Find the plan's id">
Call `GET /plans`. Plans come in name order, 15 to a page.

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

Response, shortened: most of each plan's keys, and `links` and `meta`, are left out.

```json Response, shortened
{
  "data": [
    {
      "id": 41,
      "server_id": 12,
      "name": "Nightly files",
      "type": "files",
      "type_label": "Files",
      "enabled": true
    }
  ]
}
```

Note the plan's `id`. It does not change, so a script can keep it.
</Step>
<Step title="Start the run">
Call `POST /plans/{plan}/run` with the plan's id. The request has no body.

<CodeGroup>
```bash cURL
curl -X POST 'https://app.clokeep.com/api/v1/plans/41/run' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -H 'Accept: application/json'
```

```php PHP
<?php

$ch = curl_init('https://app.clokeep.com/api/v1/plans/41/run');

curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    'Authorization: Bearer YOUR_API_TOKEN',
    'Accept: application/json',
  ],
]);

$response = curl_exec($ch);

echo $response;
```

```javascript Node.js
const response = await fetch("https://app.clokeep.com/api/v1/plans/41/run", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_API_TOKEN",
    "Accept": "application/json",
  },
});

console.log(await response.text());
```

```python Python
import requests

response = requests.post(
    "https://app.clokeep.com/api/v1/plans/41/run",
    headers={
        "Authorization": "Bearer YOUR_API_TOKEN",
        "Accept": "application/json",
    },
)

print(response.text)
```
</CodeGroup>

The answer is `202` with the new run. Response, shortened: `queue`, `laravel_job_uuid`, `plan` and `server` are left out.

```json Response, shortened
{
  "data": {
    "id": 1874,
    "plan_id": 41,
    "server_id": 12,
    "kind": "backup",
    "kind_label": "Backup",
    "trigger": "api",
    "trigger_label": "API",
    "status": "queued",
    "status_label": "Queued",
    "progress_percent": 0,
    "current_step": null,
    "message": null,
    "started_at": null,
    "finished_at": null,
    "cancel_requested_at": null,
    "error_message": null,
    "initiated_by_user_id": 8,
    "restore": null,
    "created_at": "2026-11-20T10:15:00+00:00",
    "updated_at": "2026-11-20T10:15:00+00:00"
  }
}
```

`data.id` is the run's id. The run waits as `queued` until the server's agent picks it up.
</Step>
<Step title="Follow the run until it ends">
Call `GET /backup-runs/{backupRun}` with the run's id until `status` is final. This endpoint does not count toward the [rate limit](/docs/api-reference/introduction#rate-limit), so a call every 10 to 30 seconds is fine.

```python Python
import time

import requests

API = "https://app.clokeep.com/api/v1"
HEADERS = {
    "Authorization": "Bearer YOUR_API_TOKEN",
    "Accept": "application/json",
}
FINAL = {"completed", "partial", "failed", "cancelled"}

run_id = 1874

while True:
    response = requests.get(f"{API}/backup-runs/{run_id}", headers=HEADERS, timeout=30)
    response.raise_for_status()
    run = response.json()["data"]
    print(run["status"], run["progress_percent"], run["current_step"], run["message"])

    if run["status"] in FINAL:
        break

    time.sleep(15)

if run["error_message"]:
    print(run["error_message"])
```

What the fields hold:

- `status` is `queued`, then `running`, then one of the four final values: `completed`, `partial` (label `Partially completed`), `failed` or `cancelled`. A final status never changes again.
- `progress_percent` goes from 0 to 100.
- `current_step` is the step the agent is on: `prepare`, `dump`, `archive`, `compress`, `upload` or `index`. A queued run can already show `prepare`, so test `status`, not the step. A completed run shows `null`. A failed or cancelled run keeps the step it stopped at.
- `message` is the run's latest status line, such as `Archiving backup`, and at the end a closing line, such as `Backup completed successfully.`
- `error_message` says why a `failed` run failed, or which items a `partial` run could not back up. It is `null` otherwise.
- `finished_at` is set when the run ends.

A `partial` run stored real backups for the items that worked. Treat it as a warning, not as a failed run.

The run's backups are listed under `artifacts` in the same answer. You need their ids to [restore one](/docs/developers/queue-restore).
</Step>
<Step title="Read the log">
Call `GET /backup-runs/{backupRun}/logs`. Lines come oldest first, 15 to a page, each with a `level` (`debug`, `info`, `warning` or `error`), a `message` and `created_at`. Follow `links.next` for the rest. This endpoint does not count toward the rate limit either.

```bash cURL
curl 'https://app.clokeep.com/api/v1/backup-runs/1874/logs' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -H 'Accept: application/json'
```
</Step>
</Steps>

The run is listed under **Backup Runs** in the panel. Its timeline reads "Triggered through the API by" and your name. The API has no option to start a full backup on an incremental plan. Use **Run full backup now** in the panel for that. See [Incremental backups](/docs/backups/incremental).

## When the run is refused

`409` means the plan already has a run that is queued or running:

```json 409
{
  "message": "Plan \"Nightly files\" already has an active backup run."
}
```

A plan has one active backup run at a time. Do not send the request again in a loop. Find the active run with `GET /backup-runs?plan_id=41&status=running`, or `status=queued`, and pick the run whose `kind` is `backup`. Follow it instead. Or wait until it ends and run the plan then.

`422` means the plan cannot run until something changes in its setup. The `message` says what:

- `Server "web-01" is disabled; its backups are paused until it is re-enabled.`
- `web-01 has no agent yet. Install it from the server page first.`
- `Nightly files has no active storage to upload to. Attach one to the plan, or switch its storage back on.`
- for a **Plesk server** or **cPanel server** plan, a message that every account the plan backs up is gone from the server.

Sending the request again does not help. Log the message and fix the plan or the server first.

Other errors are on [Errors and rate limits](/docs/developers/errors-rate-limits).

## Stop a run

Call `POST /backup-runs/{backupRun}/cancel`. A queued run is cancelled at once. A running run gets a `cancel_requested_at` time and stops at the agent's next check. Keep polling until `status` is `cancelled`. A run that ends before then keeps its own final status.

A run that has already ended answers `422` with `Backup run is not active and cannot be cancelled.`

What a stopped run leaves on the storage is on [Run and stop a backup](/docs/backups/run-stop#what-a-stopped-run-leaves).

## Next steps

<CardGroup cols="2">
<Card title="Queue a restore through the API" href="/docs/developers/queue-restore" icon="layers">
Put one of the run's backups back on the server.
</Card>
<Card title="Run a plan now" href="/docs/api-reference/plans/run-a-plan-now" icon="code">
Every field of the run, in the API reference.
</Card>
</CardGroup>
