# Tasks API

Every call to list image and video models, price a task, create it, follow it and erase it.

Base URL `https://api.unaligned.pw/v1`. Bodies are JSON. Amounts are in USD, dates in Unix seconds. Every route but the model list takes your API key, as on the [other routes](https://unaligned.pw/docs/authentication.md). How tasks work, what they cost and what is kept: [Images and video](https://unaligned.pw/docs/media.md).

## List the models

**GET** `https://api.unaligned.pw/v1/media/models`

No key needed. `kind` is `image` or `video`; `unit` is what a price is for: `image`, `second` or `request`. `price_from` is the lowest price per unit, in USD. `uncensored` marks the models with fewer limits on content.

curl:

```sh
curl https://api.unaligned.pw/v1/media/models
```

python:

```python
import requests

r = requests.get("https://api.unaligned.pw/v1/media/models")
for model in r.json()["models"]:
    print(model["id"], model["kind"], model["price_from"], "per", model["unit"])
```

node:

```js
const r = await fetch("https://api.unaligned.pw/v1/media/models");
for (const model of (await r.json()).models) {
  console.log(model.id, model.kind, model.price_from, "per", model.unit);
}
```

Response 200:

```json
{
  "models": [
    {
      "id": "acme/sketch-1/text-to-image",
      "name": "sketch-1",
      "org": "acme",
      "kind": "image",
      "tasks": ["text-to-image"],
      "unit": "image",
      "price_from": 0.0078,
      "uncensored": false
    }
  ]
}
```

Response 503:

```json
{
  "error": {
    "code": "unavailable",
    "message": "The image and video models are not available yet: try again shortly.",
    "request_id": "req_4b1f0d2e9a7c6b5d4e3f2a1b"
  }
}
```

Right after the API starts, until it has read the list.

## One model and its settings

**GET** `https://api.unaligned.pw/v1/media/models/{id}`

No key needed. The ID has slashes in it: put it in the path as it is, as in the sample.

**Response**

| Field | Type | Description |
| --- | --- | --- |
| `input` | object | A JSON Schema of the settings the model takes: send them as `input` when you quote or create a task. |
| `prices` | array | `variant` and `price` (USD per unit). A model with one price has the `default` variant. |
| `variant_fields` | array | The settings that choose the variant, such as a resolution. |
| `quantity_field` | string | The setting that counts the units, such as a duration in seconds. Empty when a task is one unit. |
| `timeout_seconds` | integer \| null | How long a generation may take before it fails with `timeout`. |
| `files` | string | Only on a model that takes a file: how to give it one ([Files API](https://unaligned.pw/docs/media-files.md)). |

curl:

```sh
curl https://api.unaligned.pw/v1/media/models/acme/sketch-1/text-to-image
```

python:

```python
import requests

model = requests.get("https://api.unaligned.pw/v1/media/models/acme/sketch-1/text-to-image").json()
print(model["input"]["properties"].keys(), model["prices"])
```

node:

```js
const model = await fetch("https://api.unaligned.pw/v1/media/models/acme/sketch-1/text-to-image").then((r) => r.json());
console.log(Object.keys(model.input.properties), model.prices);
```

Response 200:

```json
{
  "id": "acme/sketch-1/text-to-image",
  "name": "sketch-1",
  "org": "acme",
  "kind": "image",
  "tasks": ["text-to-image"],
  "unit": "image",
  "price_from": 0.0078,
  "uncensored": false,
  "input": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "prompt": { "type": "string", "description": "What to make." },
      "aspect_ratio": { "type": "string", "enum": ["1:1", "16:9", "9:16"], "default": "1:1" }
    },
    "required": ["prompt"]
  },
  "prices": [{ "variant": "default", "price": 0.0078 }],
  "quantity_field": "",
  "variant_fields": [],
  "timeout_seconds": 1800
}
```

Response 404:

```json
{
  "error": {
    "code": "unknown_model",
    "message": "Unknown model: see GET /v1/media/models.",
    "param": "model",
    "request_id": "req_88255e5c2c6f0110d3f3a554"
  }
}
```

The other fields are those of the list. A model’s settings are its own: read them before you build a request, and send only those the schema has. A setting the model doesn’t take is refused with a `400` whose message comes from the model.

## The models’ status

**GET** `https://api.unaligned.pw/v1/media/status`

No key needed. What the [status page](https://unaligned.pw/status) shows: every model is checked every 5 minutes, whether it is still listed and answers a price request. That does not prove a generation succeeds.

**Response**

| Field | Type | Description |
| --- | --- | --- |
| `state` | string | `up` or `down` at the last check; `unknown` when there was none in the last 15 minutes. |
| `availability` | number \| null | The share of checks that found it up over 30 days (%); `null` under 36 checks. |
| `days` | array | The last 30 days, oldest first: `day` (UTC), `polls` (checks that day) and `up` (those that found it up). |

curl:

```sh
curl https://api.unaligned.pw/v1/media/status
```

python:

```python
import requests

for model in requests.get("https://api.unaligned.pw/v1/media/status").json()["models"]:
    print(model["id"], model["state"], model["availability"])
```

node:

```js
const r = await fetch("https://api.unaligned.pw/v1/media/status");
for (const model of (await r.json()).models) {
  console.log(model.id, model.state, model.availability);
}
```

Response 200:

```json
{
  "models": [
    {
      "id": "acme/sketch-1/text-to-image",
      "name": "sketch-1",
      "org": "acme",
      "kind": "image",
      "state": "up",
      "availability": 99.65,
      "days": [
        { "day": "2026-12-02", "polls": 288, "up": 287 },
        { "day": "2026-12-03", "polls": 140, "up": 140 }
      ]
    }
  ]
}
```

The other fields are those of the list. `days` always holds 30 entries; the sample is cut short.

## Price a task

**POST** `https://api.unaligned.pw/v1/media/quote`

What a task with these settings would cost. No task is made and nothing is taken from your balance, but the settings go through the [content filter](https://unaligned.pw/docs/media.md#content-rules) like a task’s, and count in the same minute.

**Body**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `model` | string | yes | The model’s ID. |
| `input` | object | yes | The settings, as the model’s `input` schema describes them. |

**Response**

| Field | Type | Description |
| --- | --- | --- |
| `price` | number | What the task would cost, in USD. |
| `unit` | string | `image`, `second` or `request`. |
| `quantity` | number | How many units the settings make. |
| `expires_at` | integer | Until when this price is expected to hold. A task is priced again when it is created: cap it with `max_price`. |

curl:

```sh
curl https://api.unaligned.pw/v1/media/quote \
  -H "Authorization: Bearer $UNALIGNED_KEY" \
  -d '{
    "model": "acme/sketch-1/text-to-image",
    "input": { "prompt": "A lighthouse at dusk, oil painting" }
  }'
```

python:

```python
import os, requests

r = requests.post(
    "https://api.unaligned.pw/v1/media/quote",
    headers={"Authorization": f"Bearer {os.environ['UNALIGNED_KEY']}"},
    json={"model": "acme/sketch-1/text-to-image", "input": {"prompt": "A lighthouse at dusk, oil painting"}},
)
print(r.json()["price"])
```

node:

```js
const r = await fetch("https://api.unaligned.pw/v1/media/quote", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.UNALIGNED_KEY}` },
  body: JSON.stringify({
    model: "acme/sketch-1/text-to-image",
    input: { prompt: "A lighthouse at dusk, oil painting" },
  }),
});
console.log((await r.json()).price);
```

Response 200:

```json
{
  "price": 0.0078,
  "unit": "image",
  "quantity": 1.0,
  "expires_at": 1796175107
}
```

Response 400:

```json
{
  "error": {
    "code": "invalid_request",
    "message": "size: is not a supported field",
    "param": "input",
    "request_id": "req_7e4691a964a102e862ef37a2"
  }
}
```

The model refused the settings: the message is the model’s.

Response 402:

```json
{
  "error": {
    "code": "insufficient_balance",
    "message": "Your balance is empty: add credit first.",
    "request_id": "req_a41f0c6e92b8d3570e1c4b9f"
  }
}
```

A quote takes nothing, but it needs a balance above zero.

Response 401:

```json
{
  "error": {
    "code": "invalid_key",
    "message": "Missing or invalid credentials.",
    "request_id": "req_01bf480ef9e296c0d36d28c9"
  }
}
```

## Create a task

**POST** `https://api.unaligned.pw/v1/media/tasks`

Checks the settings, prices them, holds the price from your balance and starts the task. The content check and the pricing come first; once the price is held, it answers `202` as soon as the generation service has the task, or after about 25 seconds with the task still `queued`: follow it with `GET`. With `?wait=`, it waits up to `wait` seconds in all, or longer if creating the task takes longer. Anything refused before the hold costs nothing.

**Body**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `model` | string | yes | The model’s ID. |
| `input` | object | yes | The settings, as the model’s `input` schema describes them. |
| `max_price` | number | no | The most you accept to pay, in USD, from 0.000001 to 1,000,000. Above it, a `409` `price_changed` and nothing is taken. |
| `keep_hours` | integer | no | How long the generation service keeps the settings and results, in hours, from 1: 24 by default. You can ask for less than the default, never for more. |

**Query**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `wait` | integer | no | 1 to 60: wait that many seconds in all for the task to end before answering. The creation’s own time counts in it. |

**Headers**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string | no | 1 to 128 visible ASCII characters. Sent again by the same key, it returns the task it made as it stands, with a `200` and at once, whatever `wait` says, even if the settings differ: a retry after a network error never makes a second task. The request is still checked first: an unknown model, malformed settings or a field out of range get their error. If that task was erased, a `409` `conflict`. |

**Response**

The task, as `GET` returns it:

| Field | Type | Description |
| --- | --- | --- |
| `id` | string | The task’s ID. |
| `model` | string | The model it runs on. |
| `status` | string | `queued`, `running`, `succeeded` or `failed`: see [statuses](https://unaligned.pw/docs/media.md#statuses). |
| `price` | number | USD. While `settled` is false, the price held from your balance; then what the task cost in the end. |
| `settled` | boolean | Whether `price` is final. |
| `files` | array | The results of a `succeeded` task: `index`, `mime`, `bytes`, and `width`, `height` or `duration` (seconds) when known. Download one with its `index`. |
| `error` | object | Only on a `failed` task: `code` and `message`. See [why a task fails](https://unaligned.pw/docs/media.md#why-a-task-fails). |
| `created_at` | integer | Unix seconds. |
| `deadline_at` | integer \| null | When the generation service gives up on the task, once known. |
| `completed_at` | integer | When it ended. Absent before. |
| `expires_at` | integer | Until when its `files` can be downloaded. Absent when there are none. |

curl:

```sh
curl https://api.unaligned.pw/v1/media/tasks \
  -H "Authorization: Bearer $UNALIGNED_KEY" \
  -H "Idempotency-Key: order-1042" \
  -d '{
    "model": "acme/sketch-1/text-to-image",
    "input": { "prompt": "A lighthouse at dusk, oil painting" },
    "max_price": 0.01,
    "keep_hours": 6
  }'
```

python:

```python
import os, requests

r = requests.post(
    "https://api.unaligned.pw/v1/media/tasks",
    headers={
        "Authorization": f"Bearer {os.environ['UNALIGNED_KEY']}",
        "Idempotency-Key": "order-1042",
    },
    json={
        "model": "acme/sketch-1/text-to-image",
        "input": {"prompt": "A lighthouse at dusk, oil painting"},
        "max_price": 0.01,
        "keep_hours": 6,
    },
)
task = r.json()
print(task["id"], task["status"])
```

node:

```js
const r = await fetch("https://api.unaligned.pw/v1/media/tasks", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.UNALIGNED_KEY}`,
    "Idempotency-Key": "order-1042",
  },
  body: JSON.stringify({
    model: "acme/sketch-1/text-to-image",
    input: { prompt: "A lighthouse at dusk, oil painting" },
    max_price: 0.01,
    keep_hours: 6,
  }),
});
const task = await r.json();
console.log(task.id, task.status);
```

Response 202:

```json
{
  "id": "29f285d9-5df5-4de8-9394-1ac9dc93cb72",
  "model": "acme/sketch-1/text-to-image",
  "status": "queued",
  "price": 0.0078,
  "settled": false,
  "files": [],
  "created_at": 1796174813,
  "deadline_at": 1796176613
}
```

Response 200 (again):

```json
{
  "id": "29f285d9-5df5-4de8-9394-1ac9dc93cb72",
  "model": "acme/sketch-1/text-to-image",
  "status": "queued",
  "price": 0.0078,
  "settled": false,
  "files": [],
  "created_at": 1796174813,
  "deadline_at": 1796176613
}
```

The same `Idempotency-Key` again: the task it made, as it stands now, at once.

Response 400:

```json
{
  "error": {
    "code": "content_refused",
    "message": "This request was refused by the content filter.",
    "request_id": "req_9c8c57b3800ae78a7e9bb0d3"
  }
}
```

See [content rules](https://unaligned.pw/docs/media.md#content-rules). Nothing was sent or charged.

Response 400 (inline data):

```json
{
  "error": {
    "code": "invalid_request",
    "message": "Inline data isn't accepted: upload the file with POST /v1/media/files and use its reference.",
    "param": "input",
    "request_id": "req_8b065c5c4004a470bef1b0e4"
  }
}
```

Response 402:

```json
{
  "error": {
    "code": "insufficient_balance",
    "message": "Your balance is too low for this task.",
    "request_id": "req_3d2c1b0a9f8e7d6c5b4a3f2e"
  }
}
```

Response 409:

```json
{
  "error": {
    "code": "price_changed",
    "message": "The price is above max_price.",
    "request_id": "req_53ebaf103e911b861cd4f4ca"
  }
}
```

Response 429:

```json
{
  "error": {
    "code": "too_many_requests",
    "message": "Too many image and video tasks unfinished at once: wait for one to end.",
    "request_id": "req_6f5e4d3c2b1a0f9e8d7c6b5a"
  }
}
```

Other answers: a `404` `unknown_model`; a `400` for a setting the model refuses, a reference to a file you can’t use, a text too long for the [content filter](https://unaligned.pw/docs/media.md#content-rules) or a field out of range (`param` names it); a `413` when the settings are over 256 KiB; a `429` when the account’s quotes and new tasks are paused after refusals; a `503` when the generation service or the filter can’t be reached, with nothing charged.

## Follow a task

**GET** `https://api.unaligned.pw/v1/media/tasks/{id}`

The task, if this key created it. With `?wait=`, the answer comes as soon as the task ends, or when the time is up: one call a minute is enough to follow it.

**Query**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `wait` | integer | no | 1 to 60: seconds to wait for the task to end. A key can have 4 such calls waiting at once; a fifth is answered at once. |

curl:

```sh
curl "https://api.unaligned.pw/v1/media/tasks/$TASK_ID?wait=60" \
  -H "Authorization: Bearer $UNALIGNED_KEY"
```

python:

```python
import os, requests

r = requests.get(
    f"https://api.unaligned.pw/v1/media/tasks/{task_id}",
    headers={"Authorization": f"Bearer {os.environ['UNALIGNED_KEY']}"},
    params={"wait": 60},
)
print(r.json()["status"])
```

node:

```js
const r = await fetch(`https://api.unaligned.pw/v1/media/tasks/${taskId}?wait=60`, {
  headers: { Authorization: `Bearer ${process.env.UNALIGNED_KEY}` },
});
console.log((await r.json()).status);
```

Response 200:

```json
{
  "id": "29f285d9-5df5-4de8-9394-1ac9dc93cb72",
  "model": "acme/sketch-1/text-to-image",
  "status": "succeeded",
  "price": 0.0078,
  "settled": true,
  "files": [
    { "index": 0, "mime": "image/png", "bytes": 1048576, "width": 1024, "height": 1024 }
  ],
  "created_at": 1796174813,
  "deadline_at": 1796176613,
  "completed_at": 1796174864,
  "expires_at": 1796196464
}
```

Response 200 (failed):

```json
{
  "id": "a48cf466-6de4-4ac2-81eb-20e195214b16",
  "model": "acme/sketch-1/edit",
  "status": "failed",
  "price": 0.0,
  "settled": true,
  "files": [],
  "error": { "code": "content_rejected", "message": "The model refused this content." },
  "created_at": 1796174892,
  "deadline_at": 1796176692,
  "completed_at": 1796174893
}
```

Response 404:

```json
{
  "error": {
    "code": "not_found",
    "message": "Not found.",
    "request_id": "req_2301a21cf3f6c2c220e05828"
  }
}
```

The fields are those of a new task. A task of another key, an erased task and an unknown ID all get the same `404`.

## List your tasks

**GET** `https://api.unaligned.pw/v1/media/tasks`

This key’s latest tasks, newest first, as `{"tasks": […]}`. When more are left, the answer also carries `next`: pass it as `before` to read the following page.

**Query**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `limit` | integer | no | 1 to 100. Default 20. |
| `before` | string | no | The `next` of the page before: the tasks older than the last one it listed. Still valid if that task was erased meanwhile. |
| `since` | integer | no | Unix seconds: only the tasks created at or after this time. |
| `source` | string | no | `site`: only the tasks made on the website, when signed in there. Refused with an API key, which sees only its own tasks. |

**Response**

| Field | Type | Description |
| --- | --- | --- |
| `tasks` | array | The tasks, newest first, each as `GET /v1/media/tasks/{id}` gives it. |
| `next` | string | Present when more tasks are left: the `before` of the next page. Absent on the last page. |

curl:

```sh
curl "https://api.unaligned.pw/v1/media/tasks?limit=20" -H "Authorization: Bearer $UNALIGNED_KEY"
```

python:

```python
import os, requests

r = requests.get(
    "https://api.unaligned.pw/v1/media/tasks",
    headers={"Authorization": f"Bearer {os.environ['UNALIGNED_KEY']}"},
    params={"limit": 20},
)
for task in r.json()["tasks"]:
    print(task["id"], task["status"], task["price"])
```

node:

```js
const r = await fetch("https://api.unaligned.pw/v1/media/tasks?limit=20", {
  headers: { Authorization: `Bearer ${process.env.UNALIGNED_KEY}` },
});
for (const task of (await r.json()).tasks) console.log(task.id, task.status, task.price);
```

Response 200:

```json
{
  "tasks": [
    {
      "id": "29f285d9-5df5-4de8-9394-1ac9dc93cb72",
      "model": "acme/sketch-1/text-to-image",
      "status": "queued",
      "price": 0.0078,
      "settled": false,
      "files": [],
      "created_at": 1796174813,
      "deadline_at": 1796176613
    }
  ],
  "next": "1796175107123456_0b6c1f0e9d4a4c558a7e3f2d1c0b9a87"
}
```

## Erase a task

**DELETE** `https://api.unaligned.pw/v1/media/tasks/{id}`

Erases a task that has ended: the generation service is asked to delete its settings and results at once (if it does not, they go at their normal expiry). Answers `204`, no body. From then on, no route shows the task.

curl:

```sh
curl -X DELETE https://api.unaligned.pw/v1/media/tasks/$TASK_ID -H "Authorization: Bearer $UNALIGNED_KEY"
```

python:

```python
import os, requests

r = requests.delete(
    f"https://api.unaligned.pw/v1/media/tasks/{task_id}",
    headers={"Authorization": f"Bearer {os.environ['UNALIGNED_KEY']}"},
)
print(r.status_code)
```

node:

```js
const r = await fetch(`https://api.unaligned.pw/v1/media/tasks/${taskId}`, {
  method: "DELETE",
  headers: { Authorization: `Bearer ${process.env.UNALIGNED_KEY}` },
});
console.log(r.status);
```

Response 409:

```json
{
  "error": {
    "code": "pending",
    "message": "The task is still running; it can't be cancelled.",
    "request_id": "req_cfde00b5362dcd3980923432"
  }
}
```

The task hasn’t ended: try again once it has.

Response 404:

```json
{
  "error": {
    "code": "not_found",
    "message": "Not found.",
    "request_id": "req_2301a21cf3f6c2c220e05828"
  }
}
```

Response 503:

```json
{
  "error": {
    "code": "unavailable",
    "message": "The task could not be erased right now: try again shortly.",
    "request_id": "req_0e9d8c7b6a5f4e3d2c1b0a9f"
  }
}
```

Nothing was erased: send it again.
