# Tasks

Start background tasks, follow their events, read their results, and pause, stop or retry them.

Web version: https://www.svanja.com/docs/api/tasks/

A task is a job the assistant runs in the background, like comparing three offers or writing a report. You can see your tasks in [Tasks](https://app.svanja.com/tasks). These endpoints let you hand work to the assistant from a script, follow its progress, and collect the results. See [Tasks](https://www.svanja.com/docs/tasks/) for more on how tasks work.

Reading tasks requires the Tasks area, which is included in Full access and Read-only. Starting and controlling tasks requires Full access or Tasks set to **Read and write**. Tasks cost credits. A task started through the API uses the permissions of the person the API key belongs to, and its outward actions, like sending an email, follow that person's settings.

`POST /tasks` starts a task, `GET /tasks` and `GET /tasks/{task}` list or show them, and `GET /tasks/{task}/outputs` returns what a task produced.

Your request needs a `brief`. If a task might be expensive, also include `credit_cap_microcredits` to set the most it can spend (1 credit is 1,000,000 microcredits):

```json
{
  "brief": "Compare the three hosting offers in the latest thread",
  "credit_cap_microcredits": 50000000
}
```

Svanja estimates every task before starting it. If it can't estimate the cost reliably, or if the estimate is above your threshold for asking, the task won't start without a cap and the API returns `conflict`. Either send a cap or pass `"task_estimate_decision": "start"` to run it anyway. See [Plans, credits and prices](https://www.svanja.com/docs/credits-and-prices/).

`GET /tasks/{task}/events?after_sequence=0` returns up to 200 events after that sequence number, along with `data.next_sequence`. Pass that value as `after_sequence` on your next request to follow a task without missing anything, even after a disconnect.

`POST /tasks/{task}/actions` controls a task by setting `action` to `pause`, `resume`, `resume_wait`, `stop`, `retry`, or `continue`. Use `resume` to continue a task you paused, and `resume_wait` to continue one that is waiting, such as for an answer. If you're working from a terminal, see [svanja task](https://www.svanja.com/docs/cli/tasks/).

## Operations

| Operation | What it does | Effect | Endpoints |
| --- | --- | --- | --- |
| `task.control` | Control a Task. | Changes data in Svanja | `POST /tasks/{task}/actions` |
| `task.events` | Replay Task events. | Reads only | `GET /tasks/{task}/events` |
| `task.read` | Read Tasks. | Reads only | `GET /tasks`<br>`GET /tasks/{task}`<br>`GET /tasks/{task}/outputs` |
| `task.start` | Start a Task. | Changes data in Svanja | `POST /tasks` |

## Endpoints

### GET /api/v1/tasks

List tasks

Lists Tasks visible to the key's current visibility user.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `per_page` | query | integer | no | Page size. Defaults to 25 and is capped at 100. |
| `status` | query | one of queued, running, waiting, paused, cancelling, completed, failed, cancelled | no | Filter tasks by status. |

### POST /api/v1/tasks

Start a task

Starts accepted background work for the key's current visibility user.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `Idempotency-Key` | header | string | no | Optional key for unsafe bearer writes. Reusing the same key and same canonical body replays the original response for 24 hours; reusing it with a different body returns idempotency_conflict. |

| Body field | Type | Required | Notes |
| --- | --- | --- | --- |
| `title` | string | no |  |
| `brief` | string | yes |  |
| `visibility` | one of personal, team | no |  |
| `model_route` | string | no |  |
| `selected_space_id` | string | no |  |
| `expected_result` | string | no |  |
| `credit_cap_microcredits` | integer | no |  |
| `source_refs` | array of object | no |  |

### GET /api/v1/tasks/{task}

Show a task

Shows one Task if the visibility user can currently read it.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `task` | path | string | yes | Task public ULID. |

### GET /api/v1/tasks/{task}/events

Replay task events

Returns durable Task events after the submitted sequence.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `task` | path | string | yes | Task public ULID. |
| `after_sequence` | query | integer | no |  |

### GET /api/v1/tasks/{task}/outputs

List task outputs

Returns retained outputs visible to the key's current visibility user.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `task` | path | string | yes | Task public ULID. |

### POST /api/v1/tasks/{task}/actions

Control a task

Pauses, resumes, stops, retries, or continues a controllable Task.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `task` | path | string | yes | Task public ULID. |
| `Idempotency-Key` | header | string | no | Optional key for unsafe bearer writes. Reusing the same key and same canonical body replays the original response for 24 hours; reusing it with a different body returns idempotency_conflict. |

| Body field | Type | Required | Notes |
| --- | --- | --- | --- |
| `action` | one of pause, resume, resume_wait, stop, continue, retry | yes |  |
| `instructions` | string | no |  |
| `credit_cap_microcredits` | integer | no |  |
