# Files API

Send a file for a model to work from, and download what a task made.

A file goes through us both ways: you never deal with the generation service. Both routes take your API key. A file you upload can be used by the key that uploaded it, or by your account signed in on the site.

## Upload a file

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

The body is the file itself, not JSON. Use the answer’s `ref` as the value of a file setting, such as `"image_url": "media://5a1ad081-…"`.

**Headers**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `Content-Type` | string | yes | `image/jpeg`, `image/png`, `image/webp`, `image/gif`, `audio/wav`, `audio/mpeg`, `video/webm`, `video/mp4` or `video/quicktime`. |
| `Content-Length` | integer | yes | The file’s size in bytes. Images are 10 MiB at most; audio and video 90 MiB by default. |

**Response**

| Field | Type | Description |
| --- | --- | --- |
| `id` | string | The file’s ID. |
| `ref` | string | What a task’s settings point to it with: `media://` and its ID. |
| `mime` | string | Its type. |
| `bytes` | integer | Its size. |
| `expires_at` | integer | Until when it can be used in a task. |

curl:

```sh
curl https://api.unaligned.pw/v1/media/files \
  -H "Authorization: Bearer $UNALIGNED_KEY" \
  -H "Content-Type: image/png" \
  --data-binary @photo.png
```

python:

```python
import os, requests

with open("photo.png", "rb") as f:
    r = requests.post(
        "https://api.unaligned.pw/v1/media/files",
        headers={
            "Authorization": f"Bearer {os.environ['UNALIGNED_KEY']}",
            "Content-Type": "image/png",
        },
        data=f,
    )
print(r.json()["ref"])
```

node:

```js
import { readFile } from "node:fs/promises";

const r = await fetch("https://api.unaligned.pw/v1/media/files", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.UNALIGNED_KEY}`,
    "Content-Type": "image/png",
  },
  body: await readFile("photo.png"),
});
console.log((await r.json()).ref);
```

Response 201:

```json
{
  "id": "5a1ad081-5283-4984-9861-7efb864ed3e0",
  "ref": "media://5a1ad081-5283-4984-9861-7efb864ed3e0",
  "mime": "image/png",
  "bytes": 2048,
  "expires_at": 1796261285
}
```

Response 400:

```json
{
  "error": {
    "code": "invalid_request",
    "message": "The Content-Type must be one of image/jpeg, image/png, image/webp, image/gif, audio/wav, audio/mpeg, video/webm, video/mp4, video/quicktime.",
    "request_id": "req_05f456abb6231b39dec1ff0d"
  }
}
```

Response 413:

```json
{
  "error": {
    "code": "payload_too_large",
    "message": "A file of this type is 10 MiB at most.",
    "request_id": "req_0a875eac8d24716821a18d09"
  }
}
```

Response 429:

```json
{
  "error": {
    "code": "too_many_requests",
    "message": "60 files an hour at most: retry later.",
    "request_id": "req_2a3b4c5d6e7f8091a2b3c4d5"
  }
}
```

- Without `Content-Length`: a `411` `length_required`. A body shorter or longer than it said, or one that stops for 30 seconds: a `400`, and the file is not kept.
- Uploading needs a balance above zero (`402` otherwise), but costs nothing.
- Each key uploads 60 files an hour and 4 at once at most; all the keys of an account together, 600 an hour and 16 at once. A failed upload counts in its hour.
- The files one task points to must all be usable together: if one is refused for that, upload it again.

## Download a result

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

The file at `index` `n` of a `succeeded` task, until its `expires_at`. Anything that can’t be had (another key’s task, a task not `succeeded`, a file past its expiry or an index out of range) gets the same `404`.

**Headers**

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `Range` | string | no | One range of bytes: `bytes=0-1023`, `bytes=1024-` or `bytes=-1024`. Answered `206`. Several ranges at once are refused with a `416`. |

curl:

```sh
curl -OJ https://api.unaligned.pw/v1/media/tasks/$TASK_ID/files/0 \
  -H "Authorization: Bearer $UNALIGNED_KEY"
```

python:

```python
import os, requests

with requests.get(
    f"https://api.unaligned.pw/v1/media/tasks/{task_id}/files/0",
    headers={"Authorization": f"Bearer {os.environ['UNALIGNED_KEY']}"},
    stream=True,
) as r:
    with open("result-0.png", "wb") as out:
        for chunk in r.iter_content(1 << 16):
            out.write(chunk)
```

node:

```js
import { writeFile } from "node:fs/promises";

const r = await fetch(`https://api.unaligned.pw/v1/media/tasks/${taskId}/files/0`, {
  headers: { Authorization: `Bearer ${process.env.UNALIGNED_KEY}` },
});
await writeFile("result-0.png", Buffer.from(await r.arrayBuffer()));
```

Response 200 (headers):

```json
content-type: image/png
content-length: 1048576
accept-ranges: bytes
cache-control: private, no-store
content-disposition: attachment; filename="29f285d9-5df5-4de8-9394-1ac9dc93cb72-0.png"
```

The body is the file itself.

Response 206 (headers):

```json
content-type: image/png
content-length: 1024
content-range: bytes 0-1023/1048576
accept-ranges: bytes
cache-control: private, no-store
content-disposition: attachment; filename="29f285d9-5df5-4de8-9394-1ac9dc93cb72-0.png"
```

With `Range: bytes=0-1023`.

Response 416:

```json
{
  "error": {
    "code": "invalid_request",
    "message": "The range asked for is outside the file.",
    "request_id": "req_e6a8b2e5df1ef9af4bccc06d"
  }
}
```

With `Content-Range: bytes */1048576`: the file’s size.

Response 404:

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

- `Content-Type` is the file’s type, as in the task’s `files`. `Content-Disposition` names it after the task, its index and its type.
- `Content-Length`, `Content-Range` and `Accept-Ranges` are sent when known. `Cache-Control: private, no-store`: no cache on the way keeps a copy.
- Downloads have their own count a minute, as many as the key’s requests a minute, and a key can have 4 under way at once (`429` beyond).
