# Media generations

Images, video and audio run asynchronously: you create a generation, get its id and pick up the result when it is ready. Short jobs can be awaited in the same request.

| Method and path | What it does |
|---|---|
| `POST /v1/generations` | Create a generation |
| `GET /v1/generations/{id}` | Status and result |
| `GET /v1/generations` | Your generations |
| `POST /v1/generations/{id}/cancel` | Cancel while the job has not started |
| `DELETE /v1/generations/{id}` | Delete the files of a finished generation |
| `POST /v1/quotes` | Price of the same request without running it |
| `POST /v1/uploads` | Upload your own file for an input |
| `GET /v1/openapi.json` | OpenAPI 3.1 with a request schema for every model and mode |
| `GET /v1/files/...` | A result file by signed link, no key |

The same generations in the OpenAI format: [images and speech](/developers/images-audio).

Which models exist and what they take: the [catalog](/developers/models) and `GET /v1/models/{id}`.

## Create

```bash
curl https://api.unitool.ai/v1/generations \
  -H "Authorization: Bearer $UNITOOL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1009-cover" \
  -H "Prefer: wait=30" \
  -d '{
    "model": "nanobanana",
    "input": { "prompt": "A coffee cup on a wooden table, morning light", "aspect_ratio": "1:1" },
    "max_cost_tokens": 20,
    "metadata": { "order_id": "A-1009" }
  }'
```

| Field | Meaning |
|---|---|
| `model` | Media model id, optionally with a mode: `seedance:seedance-2-5` |
| `mode` | Model mode, when not given in `model`. Without a mode the first mode of `GET /v1/models/{id}` is used |
| `input` | Parameters by the mode's `input_schema`. Enum values are accepted as strings and numbers. `prompt` is required, except in modes that require a file |
| `max_cost_tokens` | Price ceiling. Above it: `402 price_exceeds_limit`, nothing charged |
| `metadata` | Up to 16 string pairs, returned in the generation object |

The answer is `202` with `Location: /v1/generations/{id}` and `Retry-After: 5`. If the generation finished within the wait (below), it is `200` with the finished object.

```json
{
  "id": "gen_6f1c0e9a2b7d4c3e8f5a1b2c3d4e5f60",
  "object": "generation",
  "model": "nanobanana",
  "mode": null,
  "status": "succeeded",
  "progress": 1,
  "cost": { "estimated_tokens": 12, "charged_tokens": 12, "refunded_tokens": 0, "settled": true },
  "outputs": [
    {
      "index": 0,
      "type": "image",
      "file_ref": "attachment:...",
      "url": "https://...",
      "content_type": "image/png",
      "width": 1024,
      "height": 1024,
      "size": 1843200,
      "expires_at": "2026-12-05T12:00:00.000Z"
    }
  ],
  "error": null,
  "metadata": { "order_id": "A-1009" },
  "created_at": "2026-11-28T12:00:00.000Z",
  "completed_at": "2026-11-28T12:00:18.000Z",
  "expires_at": "2026-12-05T12:00:00.000Z"
}
```

Statuses: `queued` → `running` → `succeeded`, `failed` or `canceled`. A `queued` generation waits its turn: an account runs a limited number of generations at once, the rest wait instead of being refused.

`GET /v1/generations?limit=20&cursor=...&model=...&status=...` lists your generations newest first in a short form; the full object is by id.

## Waiting: Prefer: wait

`Prefer: wait=N` or `?wait=N` in the URL holds the request up to N seconds, 60 at most. Ready earlier: `200` with the result; not ready: `202`, and you poll `GET /v1/generations/{id}` after the `Retry-After` pause. `GET` understands `wait` too. Video usually takes minutes: polling is more reliable than a long wait there, and a [webhook](/developers/webhooks) instead of polling is easier still: the event comes when the generation finishes.

## Idempotency

`Idempotency-Key` is 8 to 128 printable ASCII characters. A retry with the same key and body returns the same generation with `Idempotent-Replayed: true` and charges nothing again. The same key with another body is `422 idempotency_key_reused`. Keys never expire: one key is one generation. Without the header every request creates a new generation.

Retry creation after a network error, 429, 5xx or a timeout with the same key only. A new key is a new paid generation.

## Price first: /v1/quotes

Same body as creation. Nothing is reserved or downloaded: a file by link or `data:` URL counts as present. The exception is modes priced by the length of an input video: there the video must be a `file_ref` (upload it through `/v1/uploads`), or the answer is a `400` naming the field. You can also create the generation right away with `max_cost_tokens`.

```json
{
  "object": "quote",
  "model": "seedance",
  "mode": "seedance-2-5",
  "price": {
    "tokens": 200,
    "kind": "fixed",
    "breakdown": { "base": 120, "rate": { "per": "duration", "units": 8, "tokens_per_unit": 10 }, "addons": [] }
  },
  "available_tokens": 1500,
  "held_tokens": 40,
  "can_afford": true,
  "normalized_input": { "prompt": "...", "duration": 8, "resolution": "1080p" }
}
```

`price.tokens` is what creating the same request now would charge. Quotes are free.

## Files in the input

A file field in `input` takes, inside lists too (character cards, for example):

- the `file_ref` of an upload (`asset:...` from `/v1/uploads`) or of an earlier result (`attachment:...` from `outputs`);
- an https link: the file is downloaded to us before the run, with at most 3 redirects, 60 seconds and 100 MiB; private addresses, `http://` and non-standard ports are refused;
- a base64 `data:` URL.

At most 10 links and `data:` URLs per request; upload the rest first. At most two files by link download at once per account; an extra request gets `429 rate_limit_exceeded` with `Retry-After`. A video whose duration cannot be measured is `400 file_unreadable`.

A file from your disk is uploaded in two steps:

```bash
curl https://api.unitool.ai/v1/uploads \
  -H "Authorization: Bearer $UNITOOL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "frame.png", "mime_type": "image/png", "size": 482133}'
```

```json
{
  "object": "upload",
  "file_ref": "asset:3f2a...",
  "upload_url": "https://...storage.../incoming/...?X-Amz-Signature=...",
  "method": "PUT",
  "headers": { "Content-Type": "image/png", "Content-Disposition": "attachment" },
  "upload_expires_at": "2026-11-28T12:10:00.000Z",
  "expires_at": "2026-11-29T12:00:00.000Z"
}
```

`upload_url` points straight to our storage. `PUT` the file bytes there with exactly the returned `headers`, within 10 minutes (before `upload_expires_at`). The size must equal the `size` you declared. Then pass `file_ref` in the `input` field. The file is available until `expires_at`. Limits: images 20 MiB, audio and video 100 MiB, documents 50 MiB. The number and volume of uploads per minute and per day are limited.

```bash
curl -X PUT "$UPLOAD_URL" -H "Content-Type: image/png" -H "Content-Disposition: attachment" --data-binary @frame.png
```

The file is checked the first time you use its `file_ref`. Not uploaded yet: `400 file_not_found` (the upload link still stands). Another size or format: `400 file_unreadable`; the file is removed and has to be uploaded again.

## Results and retention

- `outputs[].url` is a signed link like `https://api.unitool.ai/v1/files/.../file.png`, valid for one hour. Every `GET /v1/generations/{id}` issues fresh links. Downloading needs no key, and `Range` is supported. An expired or altered link: `404 file_link_expired`; a range outside the file: `416 range_not_satisfiable`.
- The files themselves are kept for 7 days, the deadline is `expires_at`. Need them longer: download them. To pass a result to another generation, use `file_ref`: it does not expire with the link.
- Links in [webhook](/developers/webhooks) events are valid for one hour from the event; read the generation again later for fresh ones.
- After that, `outputs` is empty and `outputs_expired: true` is set.
- The model returned fewer variants than ordered: the ordered price is charged, and `outputs_filtered` says how many variants are missing.

## Deleting

`DELETE /v1/generations/{id}` removes the result files and the files you uploaded for it from our storage at once. The answer is `{"id": "gen_...", "object": "generation.deleted", "deleted": true}`; a repeat answers the same. The record, its cost and the receipt stay: `GET` shows the same status, an empty `outputs` and `deleted: true`.

- Only finished generations can be deleted: cancel a running one or wait, else `409 generation_not_finished`.
- Deleted: the results and the files uploaded for this generation (`/v1/uploads`, links, `data:` URLs). Not deleted: a file you passed by `file_ref` from another generation or from the site, and a file that anything else still uses. A deleted upload no longer works as a `file_ref` (`file_expired`).
- Storage unavailable: `503`, nothing is marked, retry.
- Needs the `generations` scope.

## Cancel

`POST /v1/generations/{id}/cancel` cancels a generation that has not started yet and returns the tokens. A repeated cancel also answers `200`. A started generation cannot be cancelled: `409 not_cancellable`. Some models cannot be cancelled at all: also `409`.

## Generation failures

A generation with `status: "failed"` has the reason in `error.code` and the fate of the tokens in `error.refund`: `full` (returned), `not_charged` (nothing was charged), `pending` (refunded by hand: the model delivered but the result could not be saved).

| `error.code` | What happened |
|---|---|
| `content_rejected` | Moderation rejected the request or the result |
| `invalid_input` | The model did not accept the parameters or a file |
| `model_error` | The model failed |
| `timeout` | The model ran out of time |
| `insufficient_balance` | The balance was short at start time |
| `internal_error` | Our failure |

Errors of the request itself (a bad parameter, unknown model, no balance) come back at once as an HTTP error, see [Errors](/developers/errors). While generations are switched off in the API, creation answers `503 service_paused` and `GET /v1/openapi.json` answers `404`; status and cancel of existing generations keep working.
