# Liberas Image in five minutes

Liberas Image is an HTTP API: send a PNG or JPEG with the steps to apply, get a PNG back. Every step is a WebAssembly component that contributors compete on, and the best verified one answers your call. This page is for people and for agents; the full HTTP surface is in [api.md](api.md).

## 1. Get a key

A key is free for the first 200 calls, once per account.

- **In the browser**: sign in with GitHub on [the product page](/products/image) and press **Start free**. The key is on the page that opens, and stays reachable from the product page as **Your free key**.
- **From a shell or an agent**: with an API token from **/me**,

```sh
liberas login                  # once: a token from /me
liberas trial image            # prints the key and saves it for `liberas run`
```

(The `liberas` CLI installs with `npm i -g https://liberas.dev/cli/liberas.tgz`, no token needed. The same call is `POST /api/products/image/trial`.)

A paid pack is 5,000 calls per key. Keys have no expiry.

## 2. Call it

One request: `POST /api/run/image-pipeline` with the key, the file as base64, and the steps in order.

```sh
KEY="LIBERAS1-…"
DATA=$(base64 -i photo.jpg | tr -d '\n')
curl -s https://liberas.dev/api/run/image-pipeline \
  -H 'content-type: application/json' \
  -d "{\"key\":\"$KEY\",\"input\":{\"data\":\"$DATA\",\"steps\":[
        {\"tag\":\"resize\",\"val\":{\"width\":800,\"height\":600,\"filter\":\"bilinear\"}},
        {\"tag\":\"grayscale\"}]}}" \
  | jq -r .output | base64 -d > out.png
```

```js
const data = Buffer.from(await readFile("photo.jpg")).toString("base64");
const res = await fetch("https://liberas.dev/api/run/image-pipeline", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ key, input: { data, steps: [{ tag: "resize", val: { width: 800, height: 600, filter: "bilinear" } }, { tag: "grayscale" }] } }),
});
const { ok, output, error } = await res.json();
if (ok) await writeFile("out.png", Buffer.from(output, "base64"));
```

The answer:

```json
{ "ok": true, "output": "<PNG as base64>", "fuel": 3616028,
  "deps": { "image-decode-jpeg": { "calls": 1, "fuel": 618680 }, "image-resize": { "calls": 1, "fuel": 1715488 }, … },
  "component": "7aa03c7d…" }
```

`ok: false` with an `error` string is the pipeline's own answer (a crop outside the image, a file that is not PNG or JPEG). `fuel` is the work the call took; `deps` shows which components did it, since the pipeline is assembled from the specs it imports.


With the CLI, the saved key is used and `--out` writes the PNG:

```sh
printf '{"data":"%s","steps":[{"tag":"grayscale"}]}' "$(base64 -i photo.jpg | tr -d '\n')" > in.json
liberas run image-pipeline @in.json --out out.png
```

## 3. Steps

Steps run in the order given. Each is `{ "tag": …, "val": … }`; `grayscale` has no `val`.

| step | `val` | notes |
|---|---|---|
| `resize` | `{ "width", "height", "filter" }` | `filter` is `"nearest"` or `"bilinear"`. Any size from 1×1 up to 16,777,216 pixels. |
| `crop` | `{ "x", "y", "width", "height" }` | Must lie inside the image; otherwise the call's `error`. |
| `transform` | `"rotate90"`, `"rotate180"`, `"rotate270"`, `"flip-horizontal"`, `"flip-vertical"` | Rotations are clockwise. |
| `grayscale` | — | BT.601 luma, alpha kept. |

Input: PNG (any color type and bit depth; palettes and transparency are honored, 16-bit is reduced to 8) or baseline and progressive JPEG (grayscale or YCbCr). Output: always an 8-bit RGBA PNG. Images are limited to 16,777,216 pixels before and after every step. Exactly what each step computes, down to the rounding, is in the spec of each component: [image-resize](/specs/image-resize), [image-crop](/specs/image-crop), [image-transform](/specs/image-transform), [image-grayscale](/specs/image-grayscale), [image-decode-png](/specs/image-decode-png), [image-decode-jpeg](/specs/image-decode-jpeg), [image-encode-png](/specs/image-encode-png). You can also call any of them on its own at `POST /api/run/{spec}` with the same key: they take and return decoded images (`{ "width", "height", "pixels": "<RGBA as base64>" }`).

### A thumbnail

[image-thumbnail](/specs/image-thumbnail) makes a PNG smaller in one call: `{ "data": "<PNG as base64>", "width": 160, "height": 120 }` → a base64 PNG of that size, resized with the bilinear filter. It is three specs wired together (decode, resize, encode), and its implementation is not code but a declaration of those three calls, a composition ([ADR 0068](https://github.com/pairful/liberas/blob/main/docs/adr/0068-compositions.md)): the one in the catalog is `examples/image/thumbnail.json`.

### Steps on decoded pixels

[image-edit](/specs/image-edit) applies the pipeline's steps (section 3, `blur` included) to a decoded image rather than a file: `{ "input": image, "steps": [ … ] }` → image. Use it between your own decoding and encoding, or to chain the operations below. It is defined by a composition, a fold over the steps with a match on each, not by code: `examples/image/edit.json`.

### Operations on decoded pixels

Six more operations take and return a decoded image rather than a file, each at its own `POST /api/run/{spec}` with the same key and `input` named as below. Decode first with `image-decode-png` or `image-decode-jpeg` (`{ "data": "<file as base64>" }` → `{ "width", "height", "pixels" }`), and encode the result with `image-encode-png` (`{ "input": image }` → base64 PNG).

| spec | `input` | what it does |
|---|---|---|
| [image-invert](/specs/image-invert) | `{ "input": image }` | 255 − each color channel, alpha kept. |
| [image-threshold](/specs/image-threshold) | `{ "input": image, "level": 128 }` | Black or white by BT.601 luma: white from `level` up. |
| [image-adjust](/specs/image-adjust) | `{ "input": image, "brightness": 20, "contrast": 30 }` | Brightness −255..255 added, then contrast −100..100 about 128. |
| [image-blur](/specs/image-blur) | `{ "input": image, "radius": 2 }` | Box blur; the window shrinks at the edges; `radius` up to 64. |
| [image-pad](/specs/image-pad) | `{ "input": image, "left": 4, "top": 4, "right": 4, "bottom": 4, "color": { "r": 255, "g": 255, "b": 255, "a": 255 } }` | A border of that color. |
| [image-composite](/specs/image-composite) | `{ "bottom": image, "top": image, "x": 10, "y": 10 }` | Lays `top` over `bottom` at (x, y), blending by `top`'s alpha. |

These are not pipeline steps yet: a pipeline that runs them is a spec waiting for someone to propose it (section 5). Each of the six is an open bounty on [/bounties](/bounties): the first component that passes earns its shares.

## 4. Limits and errors

| status | meaning |
|---|---|
| `400 bad_input` | The JSON does not match the interface: a wrong step tag, a missing field, a value out of range. Nothing ran and nothing was counted. |
| `402 quota_exhausted` | The key has made every call its pack holds. Get a paid pack on the product page. |
| `403 license_invalid` | The key is malformed, revoked, or for another product. |
| `429 rate_limited` | More than 300 calls in a minute on one key. Wait a moment. |

A call may run for up to about ten seconds of CPU (its fuel budget); a 1-megapixel JPEG takes about one second. Requests count toward the pack whether or not the pipeline reports an error (`ok: false`), since the work was done; `400`s do not.

## 5. Make it better, get paid

Every step is a spec anyone can beat. Submit a component that passes the same cases with at least 10% less fuel and you replace the current one, with shares of every future Image sale ([contributing.md](contributing.md)). The six operations on decoded pixels have no component yet: the first to pass takes the whole bounty (`liberas specs --open --product image`). Missing a filter, a format, a color space, or a pipeline that runs the new operations as steps? Propose it as a spec; once approved it is yours.
