# Queue a restore through the API

> Pick a backup, send the restore body that fits its kind, and follow the restore run until it ends.

A restore through the API does what **Restore selected backup** does in the panel, with the same choices. The agent downloads the backup from your storage and writes it onto the server the backup came from.

<Warning>
A restore writes onto the live server and cannot be undone. Before you script one, read what each choice overwrites on [Restore files, email and databases](/docs/restores/files-databases) and [Restore a Plesk or cPanel account](/docs/restores/hosting-account).
</Warning>

## 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).
- The agent installed on the backup's server.
- For a files restore, a target folder the agent may write to. See [Allowed folders](/docs/restores/files-databases#allowed-folders).

## Queue the restore

<Steps>
<Step title="Find the backup">
Call `GET /artifacts`. Backups come newest first, 15 to a page. To list only the backups of one run, add `?backup_run_id=1873`. A run's backups are also listed under `artifacts` in `GET /backup-runs/{backupRun}`.

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

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

```json Response, shortened
{
  "data": [
    {
      "id": 2291,
      "backup_run_id": 1873,
      "filename": "nightly-files-2026-11-20-020007.tar.gz",
      "label": null,
      "hosting_account_id": null,
      "created_at": "2026-11-20T02:14:20+00:00"
    }
  ]
}
```

Note the backup's `id`.
</Step>
<Step title="Check what kind of backup it is">
The body depends on the plan type of the backup. Call `GET /backup-runs/{backupRun}` with the backup's `backup_run_id` and read `data.plan.type`.

| `plan.type` | Body fields |
| --- | --- |
| `files`, `email` | `target_path`, `overwrite_policy` |
| `database` | `database_connection_id` |
| `plesk_subscription`, `cpanel_account` | `components` and the account fields, or `mailbox` |

A few Plesk and cPanel backups take the files fields instead: those for which the panel shows the files form, such as one whose account no longer exists. See [Files](/docs/restores/files-databases#files).

A field that does not fit the backup is refused with `422`, for example `The database connection id field does not apply to this backup.` Nothing is queued.
</Step>
<Step title="Send the restore">
Call `POST /artifacts/{artifact}/restore` with the backup's id and the body for its kind.

**A files or email backup.** `target_path` is an absolute folder on the server, inside the agent's allowed folders. `overwrite_policy` is `overwrite` (the default), `skip_existing` or `rename`.

For a backup of an incremental plan, every choice restores the newest version of each file. Only `overwrite` also deletes from the target the files deleted during the chain. See [Files](/docs/restores/files-databases#files).

<CodeGroup>
```bash cURL
curl -X POST 'https://app.clokeep.com/api/v1/artifacts/2291/restore' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "target_path": "/var/www/restore",
    "overwrite_policy": "skip_existing"
  }'
```

```php PHP
<?php

$ch = curl_init('https://app.clokeep.com/api/v1/artifacts/2291/restore');

curl_setopt_array($ch, [
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    'Authorization: Bearer YOUR_API_TOKEN',
    'Accept: application/json',
    'Content-Type: application/json',
  ],
  CURLOPT_POSTFIELDS => json_encode([
    'target_path' => '/var/www/restore',
    'overwrite_policy' => 'skip_existing',
  ]),
]);

$response = curl_exec($ch);

echo $response;
```

```javascript Node.js
const response = await fetch("https://app.clokeep.com/api/v1/artifacts/2291/restore", {
  method: "POST",
  headers: {
    "Authorization": "Bearer YOUR_API_TOKEN",
    "Accept": "application/json",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "target_path": "/var/www/restore",
    "overwrite_policy": "skip_existing",
  }),
});

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

```python Python
import requests

response = requests.post(
    "https://app.clokeep.com/api/v1/artifacts/2291/restore",
    headers={
        "Authorization": "Bearer YOUR_API_TOKEN",
        "Accept": "application/json",
        "Content-Type": "application/json",
    },
    json={
        "target_path": "/var/www/restore",
        "overwrite_policy": "skip_existing",
    },
)

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

**A database backup.** `database_connection_id` is a database connection on the backup's server. The dump is imported into the database that connection names. List the server's connections with `GET /database-connections?server_id=12` and pick an `id`.

```json Body
{
  "database_connection_id": 9
}
```

**A Plesk or cPanel backup.** `components` lists the parts to restore. `databases` names the databases, and `database_mode` is `new` (into `name_restored`) or `overwrite`. To restore one mailbox, send `mailbox` alone instead. Every field is on [Restore a backup](/docs/api-reference/backups/restore-a-backup).

```json Body
{
  "components": ["files", "databases"],
  "databases": ["wp"],
  "database_mode": "new"
}
```

Left out, `database_mode` follows the same rule as the panel. It is `overwrite` when you send `apply_settings` with `settings` in `components` and the account was missing from the server at the last account sync: the re-created account's databases come back empty, and the data goes into them. Otherwise it is `new`. Send `database_mode` yourself when you know better, for example for an account deleted since the last sync. With `overwrite`, each named database is dropped and created again before the import, also for an account that still exists. See [Restore a Plesk or cPanel account](/docs/restores/hosting-account).
</Step>
<Step title="Read the answer">
The answer is `202` with the new run. Its `kind` is `restore`, and `restore` holds your choices. Response, shortened: `queue`, `laravel_job_uuid`, `plan` and `server` are left out.

```json Response, shortened
{
  "data": {
    "id": 1875,
    "plan_id": 41,
    "server_id": 12,
    "kind": "restore",
    "kind_label": "Restore",
    "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": {
      "source_artifact_id": 2291,
      "mode": "alternate_path",
      "target_path": "/var/www/restore",
      "overwrite_policy": "skip_existing",
      "database_connection_id": null,
      "account": null
    },
    "created_at": "2026-11-20T10:40:00+00:00",
    "updated_at": "2026-11-20T10:40:00+00:00"
  }
}
```
</Step>
<Step title="Follow the restore">
Poll `GET /backup-runs/{backupRun}` with the run's id, as for a backup. See [Follow the run until it ends](/docs/developers/run-backup#follow-the-run-until-it-ends). The final statuses are the same. A restore's steps are `download`, `extract`, `restore` and `verify`.

A files restore into a folder the agent may not write to fails with an `error_message` that reads "restore target … is not within an allowed root". See [Allowed folders](/docs/restores/files-databases#allowed-folders).
</Step>
</Steps>

## When the restore is refused

- `409`: another restore on the same server is queued or running, for example `Server "web-01" already has an active restore run.` A server runs one restore at a time. Wait for that run to end, then send the request again.
- `422` with `errors`: a field is missing, invalid or does not fit the backup. Fix the body.
- `422` without `errors`: the backup cannot be restored. The `message` says why, for example that its server or plan was deleted, or that it was missing from storage at the last check.
- `503`: the backup's file list cannot be read from storage. `message` says why. For a Plesk or cPanel backup, it starts with `The file list of this backup is unavailable:`. Nothing is queued. Try again later if the storage was unreachable or the file list was still being fetched by another request. If the file list is missing from storage, a retry does not help.

To stop a restore, call `POST /backup-runs/{backupRun}/cancel`, as for a backup. What a stopped restore already wrote stays on the server. See [One restore at a time, and stopping one](/docs/restores/files-databases#one-restore-at-a-time-and-stopping-one).

## Next steps

<CardGroup cols="2">
<Card title="Errors and rate limits" href="/docs/developers/errors-rate-limits" icon="book">
How a script should react to each error.
</Card>
<Card title="Restore a backup" href="/docs/api-reference/backups/restore-a-backup" icon="code">
Every body field and answer, in the API reference.
</Card>
</CardGroup>
