# Images

An element's or the world's `image_url` takes any URL. To host the image on OnlyWorlds instead, there are three steps. The API never handles the image itself: it issues a ticket, and a separate upload host takes the bytes.

## 1. Get a ticket

`POST /api/v2/media/ticket` with a write key and its PIN, and no body. Owner, legacy, member and AI seat keys all work.

```bash
curl -s -X POST "https://www.onlyworlds.com/api/v2/media/ticket" \
  -H "API-Key: {key}" -H "API-Pin: {pin}"
```

`201`:

<!-- shape: POST /api/v2/media/ticket -->
| Field | Meaning |
|:--|:--|
| `ticket` | Opaque: send it to `upload_url` as it is |
| `upload_url` | Where the image goes, on `upload.onlyworlds.com` |
| `prefix` | `u/<world_id>/`, the object key prefix this world's uploads land under |
| `max_bytes` | The most this ticket accepts: 15 MB (15,728,640 bytes), or what the uploading account has left, whichever is smaller. The upload host holds the upload to it |
| `exp` | Unix seconds after which the ticket is refused |
| `uses` | Always `1`: one ticket, one upload |
| `issued_to` | `{account, membership}`: who answers for the upload |

A ticket is valid for 10 minutes.

## 2. Upload the bytes

```bash
curl -s -X POST "{upload_url}" \
  -H "Authorization: Bearer {ticket}" \
  --data-binary @shrike.webp
```

The body is the raw image: webp, png, jpeg or avif. The type is read from the bytes; SVG is not accepted. `201`: `{url, key, bytes, type, etag}`.

An optional `X-Key` header names the object yourself. It must start with the ticket's `prefix`, and an existing key is never overwritten.

The upload host is not part of the API and answers errors in its own shape: see [Upload host errors](/api/errors/#upload-host-errors).

## 3. Set the picture

```bash
curl -s -X PATCH "https://www.onlyworlds.com/api/v2/creature/{id}" \
  -H "API-Key: {key}" -H "API-Pin: {pin}" -H "Content-Type: application/json" \
  -d '{ "image_url": "{url}" }'
```

For the world's own picture, `PATCH /api/v2/world` with the same body (the owner or a co-builder).

## Removing an image

An image uploaded with a ticket can be removed by its uploader, or by the world's owner key; an AI seat answers as its sponsor. Any other key gets `403` [`permission_error`](/api/errors/#permission_error). Removal takes two calls, like the upload.

```bash
curl -s -X POST "https://www.onlyworlds.com/api/v2/media/remove-ticket" \
  -H "API-Key: {key}" -H "API-Pin: {pin}" -H "Content-Type: application/json" \
  -d '{ "key": "u/{world_id}/{name}.webp" }'
```

The `key` is the part of the `image_url` after `https://media.onlyworlds.com/`. The reply is `{ticket, remove_url, exp, key, referenced}`:
- a single-use ticket
- where to send it
- when it expires, in Unix seconds, 10 minutes after it was issued
- `referenced`: how many of the world's elements you can see still show the image in their `image_url`; an image linked inside a text, set as the world's own image or kept in a published page's stored copy is not counted

```bash
curl -s -X POST "{remove_url}" \
  -H "Authorization: Bearer {ticket}"
```

`200`: `{removed, bytes, cache}`. The bytes go back to the uploading account's storage.

- **Removing doesn't touch the elements.** Clear or replace their `image_url` yourself; `referenced` says how many there are.
- **A copy can linger.** Images are cached as unchanging for a year, so a copy that the network or a visitor's browser already holds can keep answering for days or longer after removal. `cache: "may_linger"` says so.
- **Deleting a world removes its images.** Every image uploaded with a ticket for that world is removed with it, as its [files](/docs/development/api/files) are; one whose removal the upload host doesn't confirm is removed by a nightly sweep. A cached copy can still linger, and an `image_url` copied into another world or a folder stops answering.
- **Only ticket uploads.** Images uploaded any other way answer `404` [`not_found`](/api/errors/#not_found).
- **Right after an upload.** Asked before the upload host has recorded the image, the ticket route answers `409` [`upload_pending`](/api/errors/#upload_pending) with `Retry-After: 2`: wait and ask again.
- **One removal at a time.** A second ticket for the same image within 10 minutes answers `409` [`removal_in_flight`](/api/errors/#removal_in_flight), with `Retry-After`.

## Limits

- **200 tickets per world in 24 hours.** More is `429` [`quota_exceeded`](/api/errors/#quota_exceeded), with `Retry-After`.
- **1 GB of images per uploading account** (1,073,741,824 bytes) by default; an account's cap can be raised. Owner and legacy keys count against the owner, a member's key against the member, an AI seat against its sponsor. A full account gets `403` [`storage_full`](/api/errors/#storage_full) and no ticket.
- When image upload is not configured on the server, the ticket route answers `503` [`media_unavailable`](/api/errors/#media_unavailable).

Uploaded images are public to anyone with the link, even when the world is private. They stay until their uploader or the world's owner removes them, or the world is deleted.