# Liberas Layers in five minutes

Liberas Layers is a layer compositor as an HTTP API: send a document (a background and layers with a position, an opacity, one of 27 blend modes and an optional mask) and get the rendered RGBA image back. It is built from the leaves up: `layers-blend`, `layers-mask` and `layers-place` are specs of their own, and `layers-render` is assembled from them. This page is for people and for agents; the full HTTP surface is in [api.md](api.md).

## 1. Get a key

Sign in, open the [product page](/products/layers) and press **Start free**: 200 calls, once per account. The key is shown on the order page. With the CLI:

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

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

## 2. Render a document

`POST /api/run/layers-render-png` with the key and a `doc` whose layers are PNG or JPEG files (base64). The answer is a PNG.

```sh
PHOTO=$(base64 -i photo.jpg | tr -d '\n'); LOGO=$(base64 -i logo.png | tr -d '\n')
curl -s https://liberas.dev/api/run/layers-render-png \
  -H 'content-type: application/json' \
  -d "{\"key\":\"$KEY\",\"input\":{\"doc\":{
        \"width\":640,\"height\":480,\"background\":{\"r\":255,\"g\":255,\"b\":255,\"a\":255},
        \"layers\":[
          {\"data\":\"$PHOTO\",\"x\":0,\"y\":0,\"opacity\":255,\"mode\":\"normal\",\"mask\":null,\"visible\":true},
          {\"data\":\"$LOGO\",\"x\":480,\"y\":400,\"opacity\":180,\"mode\":\"multiply\",\"mask\":null,\"visible\":true}
        ]}}}" \
  | jq -r .output | base64 -d > out.png
```

The answer is `{ "ok": true, "output": "<PNG as base64>", "fuel": …, "deps": { "image-decode-jpeg": …, "image-decode-png": …, "layers-render": …, "layers-blend": …, "image-encode-png": … } }`. `deps` shows which components did the work: the decoders and the encoder are Liberas Image's specs, linked across products, and their contributors are paid by this call too (ADR 0050).

`POST /api/run/layers-render` is the same with decoded pixels: each layer's `pixels` and `mask` are `{ "width", "height", "pixels": "<RGBA as base64>" }` and the output is one such image.

## 3. The document

| field | meaning |
|---|---|
| `width`, `height` | The canvas, 1×1 up to 16,777,216 pixels. |
| `background` | `{ r, g, b, a }`, 0..255 each. The canvas starts filled with it. |
| `layers[]` | Bottom first. Each: `data` (a PNG or JPEG file; `pixels`, an image, for `layers-render`), `x`, `y` (may be negative; what falls outside the canvas is dropped), `opacity` 0..255, `mode`, `mask` (a file or image of the same size, or `null`; its luma scales the layer's alpha), `visible`. |
| `mode` | `normal`, `dissolve`, `darken`, `multiply`, `color-burn`, `linear-burn`, `darker-color`, `lighten`, `screen`, `color-dodge`, `linear-dodge`, `lighter-color`, `overlay`, `soft-light`, `hard-light`, `vivid-light`, `linear-light`, `pin-light`, `hard-mix`, `difference`, `exclusion`, `subtract`, `divide`, `hue`, `saturation`, `color`, `luminosity`. |

Exactly what each step computes, down to the rounding, is in the spec of each component: [layers-blend](/specs/layers-blend), [layers-mask](/specs/layers-mask), [layers-place](/specs/layers-place), [layers-render](/specs/layers-render), [layers-render-png](/specs/layers-render-png). Each can be called on its own at `POST /api/run/{spec}` with the same key.

### Adjustment layers

`POST /api/run/layers-render-2` takes the same document with one more kind of layer: an adjustment, applied to everything composited below it. Each layer has either `pixels` or `adjustment`, the other `null`.

```json
{ "pixels": null, "adjustment": { "tag": "levels", "val": { "in-black": 30, "in-white": 220, "out-black": 0, "out-white": 255 } },
  "x": 0, "y": 0, "opacity": 255, "mode": "normal", "mask": null, "visible": true }
```

| adjustment | `val` | spec |
|---|---|---|
| `levels` | `{ inBlack, inWhite, outBlack, outWhite }` | [layers-adjust-levels](/specs/layers-adjust-levels) |
| `curves` | `[{ x, y }, …]` from x = 0 to x = 255 | [layers-adjust-curves](/specs/layers-adjust-curves) |
| `hue-saturation` | `{ hue, saturation, lightness }` | [layers-adjust-hue-saturation](/specs/layers-adjust-hue-saturation) |
| `posterize` | levels, 2..255 | [layers-adjust-posterize](/specs/layers-adjust-posterize) |
| `gradient-map` | `[{ position, color: { r, g, b } }, …]` from 0 to 255 | [layers-adjust-gradient-map](/specs/layers-adjust-gradient-map) |
| `channel-mixer` | `{ red: { red, green, blue, constant }, green: …, blue: … }` in percent | [layers-adjust-channel-mixer](/specs/layers-adjust-channel-mixer) |

An adjustment layer's `mask` is canvas-sized (`x`, `y` are ignored) and its `opacity` is the strength of the adjustment. These seven specs are open bounties: `layers-render-2` answers `404 not_built` until contributors have built them all, and the [roadmap](/products/layers) shows which are done. Once built it is the new version of `layers-render`: the same URL takes documents with or without adjustment layers, and `layers-render-png` renders through it.

## 4. Limits and errors

| status | meaning |
|---|---|
| `400 bad_input` | The JSON does not match the interface: an unknown mode, 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. |

`ok: false` with an `error` string is the renderer's own answer: a file that is not PNG or JPEG, a mask of another size than its layer, a zero or oversized canvas, a layer whose pixels are not width × height × 4 bytes.

## 5. Make it better, get paid

Every spec here is a bounty: 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 Layers sale ([contributing.md](contributing.md)). Adjustment layers and layer styles are the next specs (the product's roadmap is on its product page); propose one and, once approved, it is yours.
