# Coco REST API

Source: the Coco API documentation. Base URL: https://api.compositor.video. OpenAPI spec: https://compositor.video/openapi.json

## Quick start

Every request sends an API key as a bearer token: `Authorization: Bearer sk_live_...`.
Make a key in the editor at `https://compositor.video/app/`, on the API keys page. The key is shown once.

Render one video from a template. `template` is `tpl_<id>@v<n>`, or `tpl_<id>` for the latest version. `vars` fills the template's variables.

```sh
curl -X POST https://api.compositor.video/v1/renders \
  -H "Authorization: Bearer $COCO_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: row-213" \
  -d '{
    "template": "tpl_7Kq2@v3",
    "vars": {
      "headline": "Chore jacket",
      "photo": {"asset": "a_lab01"}
    },
    "webhook_url": "https://hooks.example.com/coco"
  }'
```

The reply is `201` with the render: `{"id": "rnd_...", "state": "queued", ...}`.
A text or colour variable takes a string. A video, image or audio variable takes `{"asset": "a_..."}` or `{"url": "https://..."}`.
The same `Idempotency-Key` and body within 48 h returns the first render with `200`.

Read the render with `GET /v1/renders/{id}` until `state` is `succeeded` or `failed`.
On success, `outputs[0].url` is the MP4. The link works until `outputs[0].expires`. The file is kept for 7 days.
`GET /v1/renders/{id}/output` answers 302 to a new link while the file is kept.

## Webhooks

When a render request has `webhook_url`, Coco sends a `POST` to it when the render succeeds or fails.
The `Webhook-Event` header is `render.succeeded` or `render.failed`, and the body is the render.
The `Webhook-Signature` header is `t=<unix>,v1=<hex>`. The hex is HMAC-SHA256 of `<t>.<raw body>` with the `webhook_secret` from `GET /v1/me`.
If the answer is not 2xx, Coco sends the webhook again with backoff for about a day.

## Errors

Every error body is `{"error": {"code": "...", "message": "...", "field": "..."}}`.
`field` names the request part at fault, for example `vars.headline` or `vars.slides[3].photo`.
Code `invalid_value` comes back as `422` before the render starts. Codes `monthly_render_minutes` and `concurrent_render_limit` come back as `429`: they are plan limits.
A render that fails later has `state` `failed` and an `error` with `code` and `message`.

## Template JSON

| Key | Value |
|---|---|
| `name` | Display name. |
| `canvas` | `{"w", "h", "fps", "duration"}`. Width and height in px, even, at most 3840. fps from 1 to 60. duration in seconds, above 0, at most 300. |
| `variables` | Object keyed by variable name (`a-z`, `0-9`, `_`, starting with a letter). |
| `tracks` | List of `{"id", "kind", "clips"}`. |

The server sets `template` (the id) and `version`. All times are seconds, and decimals are allowed.

## Variables

| `type` | Extra keys | Value in `vars` |
|---|---|---|
| `text` | `max`: character limit | A non-empty string. |
| `color` | none | `"#RRGGBB"`. Left out: `#333333`. |
| `image` | `required`, `default` | `{"asset": "a_..."}` |
| `audio` | `required`, `default` | `{"asset": "a_..."}` |
| `video` | `required`, `default`, `trim` | `{"asset": "a_...", "trim": [start, end]}`. `trim` is optional. |
| `list` | `min`, `max`, `section`, `fields` | An array of item objects keyed by field. See [Lists](#lists). |

`default` is `"asset:a_..."`. A render that leaves out an asset variable gets its default. If the variable is `required` or has no default, the render must pass an asset.

`trim` is `[start, end]` in seconds of the video source, with `end` above `start`. The clip plays the source from `start`. When the source reaches `end` before the clip ends, the clip holds its last frame and has no sound for the rest of its span. A `trim` on the variable applies to its `default` and to every render value that has no `trim` of its own. With no `trim` anywhere, the clip plays the source from 0.

## Tracks and clips

A track lists its clips in `clips`. Every clip has `id` (unique in the template), `kind`, `src`, `start` and `dur` (at least 0.1). `start` is below `canvas.duration`. `src` names the variable that the clip shows, in double curly braces.

| Track `kind` | Clip `kind` | `src` variable type | Extra keys | Animation key and values |
|---|---|---|---|---|
| `video` | `video` | video or image | `gain` (0 to 4, default 1), `normalize` (`true` or `false`, default `false`) | `anim`: `fade_in` |
| `video` | `image` | image or video | none | `move`: `zoom_in`, `zoom_out`, `pan_left` |
| `video` | `color` | color | none | `anim`: `fade_in` |
| `text` | `text` | text | `size` (24 to 200), `edge` (`top` or `bottom`) | `anim`: `fade_in`, `slide_up` |
| `overlay` | `overlay` | image | none | `anim`: `fade_in` |
| `audio` | `audio` | audio | `gain` (0 to 4), `normalize` (`true` or `false`, default `false`) | `anim`: `fade_out` |
| `caption` | `caption` | audio | `size` (24 to 200), `edge` (`top` or `bottom`), `color` and `highlight` (`"#RRGGBB"`), `max_words` (1 to 8), `max_lines` (1 to 3) | none |

Leave out the animation key for no animation. A template holds at most 64 clips. At most 8 of them are caption clips.

## Rendering rules

- The video is exactly `canvas.duration` seconds long. A clip that runs past the end is cut.
- Clips on video tracks fill the canvas with a cover crop. Where clips overlap in time, the clip listed first is on top. Time with no clip is black.
- A video source shorter than its clip holds its last frame.
- A dark gradient covers the top of the frame only while a text clip is on screen. It keeps top text readable on bright images. A template with no text clip has no gradient.
- Text is white with a shadow and wraps to the canvas width. `size` is px at a 1080 px wide canvas and scales with the canvas width.
- An overlay clip shows its image in a small box at the bottom left, about a quarter of the canvas width.
- Caption clips draw above text clips and below overlay clips.
- Audio clips play from their `start` and mix together. `fade_out` fades the last second. A short audio file leaves silence after it.
- Text needs a font. The service uses the account's oldest font asset, else its default font.

## Sound

- A video clip plays the sound of its source for its span, from the same source point as its picture. It mixes with the audio clips.
- A video source with no audio stream gives silence for that clip. The render does not fail.
- Image and colour clips have no sound. A video clip that shows an image variable has no sound.
- `gain` sets the sound level of a video or audio clip, from 0 to 4. At 1 the clip plays at the level of its source. `gain: 0` mutes the clip. A video clip with no `gain` plays at 1. An audio clip needs `gain`.
- When a list section repeats a video clip, each item's clip plays its own sound.
- Where video clips overlap in time, the sound of each clip plays. The picture shows the clip on top.
- `normalize: true` levels the sound of the clip to -16 LUFS integrated, with a true peak of at most -1.5 dBTP. The level applies before `gain`. `normalize` is `false` when you leave it out.
- `normalize` measures only the part of the source that the clip plays, not the whole file. A video clip with a `trim` is levelled on its trimmed part.
- A limiter keeps the true peak of the final mix at -1 dBTP or lower. A `gain` above 1 never clips the sound.

## List renders from automation tools

A template with a list variable takes an array of items in `vars`. The section of the template plays once per item.
One request makes one video from any number of items. The caller sends one JSON body with the array. No iterator runs.
Each item asset is a library asset id or a URL value (see Media by URL).
The list format rules and a worked example are in the section Lists of the template guide.

## Lists

A list variable lets one render show any number of items, for example 4 photos with a caption each. The template stores one copy of a span of time, the section. The render plays the section once per item, in item order.

## The list variable

| Key | Value |
|---|---|
| `type` | `"list"` |
| `min` | Fewest items a render takes. An integer, 1 or more. Default 1. |
| `max` | Most items a render takes. At least `min`, at most the item ceiling. Left out, the save writes the ceiling. |
| `section` | `{"start", "dur"}` in seconds, and an optional `"fit"`. `start` is 0 or more. `dur` is at least 0.1 and one frame. `start + dur` is at most `canvas.duration`. |
| `section.fit` | Optional. The key of a video field of the list. Each item then lasts as long as its clip, and `dur` is the design length the editor shows. See Clip length below. |
| `fields` | Object of 1 to 16 fields. Each field takes the keys of a variable of its type. A field cannot be a list. |

A template holds at most one list. The list key and the field keys use `a-z`, `0-9` and `_`, start with a letter and hold at most 16 characters. They cannot hold `__`. In a template with a list, no other variable key holds `__` and no clip id holds `#`. The render uses these names for the copies.

## Section rules

- A clip shows a field as `"{{slides.photo}}"`: the list key, a dot, the field key.
- A clip that shows a field sits inside the section.
- The section holds at least one clip that shows a field.
- A clip sits before the section, inside it, or after it. It can also cover the whole section. No clip crosses an edge of the section.
- The render copies each clip inside the section once per item. Copy k starts `k * dur` seconds after the stored clip.
- Clips after the section move later by `(items - 1) * dur` seconds.
- A clip that covers the whole section and shows no field stretches over all the copies. Music and a logo work this way.
- The video lasts `canvas.duration + (items - 1) * section.dur` seconds.

These timing rules apply to a section with no `fit`. Clip length below gives the rules with `fit`.

## Item ceiling

A render holds at most 64 clips and lasts at most 300 s. The save takes the lower of two counts as the item ceiling:

- By clips: `floor((64 - F) / C)`. `F` is the number of clips outside the section, `C` the number of clips inside it.
- By duration: `floor((300 * fps - round(canvas.duration * fps)) / S) + 1`. `S` is the section length in frames.

With `fit`, the item lengths are known only at render, so the ceiling is the count by clips. The 300 s limit applies at render, when the lengths are known.

A `max` above the ceiling fails the save with `invalid_value` at `variables.<list>.max`. A list with one clip per item and no other clip fits 64 items. Two clips per item fit about 30.

## Render vars

`vars` gives a list an array of items. Each item is an object keyed by field. Each field value has the shape a variable of that type takes. Field order inside an item does not matter. Item order is the film order. An error inside an item names its path, for example `vars.slides[1].caption`.

## Worked example

A vertical photo slideshow: a 2 s intro in the brand colour, a 3 s section per photo, and a 2 s outro. A title shows on the intro and a call to action on the outro. The logo and the music cover the whole film. The item ceiling is 29: 6 clips sit outside the section and 2 inside it.

```json template
{
  "name": "Photo slideshow",
  "canvas": {"w": 1080, "h": 1920, "fps": 30, "duration": 7},
  "variables": {
    "title": {"type": "text", "max": 40},
    "slides": {
      "type": "list",
      "min": 1,
      "max": 29,
      "section": {"start": 2, "dur": 3},
      "fields": {
        "photo": {"type": "image", "required": true},
        "caption": {"type": "text", "max": 40}
      }
    },
    "cta": {"type": "text", "max": 24},
    "brand_color": {"type": "color"},
    "logo": {"type": "image", "default": "asset:a_logo"},
    "music": {"type": "audio", "default": "asset:a_music"}
  },
  "tracks": [
    {"id": "V1", "kind": "video", "clips": [
      {"id": "intro", "kind": "color", "src": "{{brand_color}}", "start": 0, "dur": 2},
      {"id": "shot", "kind": "image", "src": "{{slides.photo}}", "start": 2, "dur": 3, "move": "zoom_in"},
      {"id": "outro", "kind": "color", "src": "{{brand_color}}", "start": 5, "dur": 2}
    ]},
    {"id": "T1", "kind": "text", "clips": [
      {"id": "title", "kind": "text", "src": "{{title}}", "start": 0.2, "dur": 1.6, "size": 110, "edge": "top", "anim": "fade_in"},
      {"id": "cap", "kind": "text", "src": "{{slides.caption}}", "start": 2.3, "dur": 2.5, "size": 84, "edge": "bottom", "anim": "slide_up"},
      {"id": "cta", "kind": "text", "src": "{{cta}}", "start": 5.2, "dur": 1.6, "size": 84, "edge": "bottom", "anim": "slide_up"}
    ]},
    {"id": "O1", "kind": "overlay", "clips": [
      {"id": "logo", "kind": "overlay", "src": "{{logo}}", "start": 0, "dur": 7, "anim": "fade_in"}
    ]},
    {"id": "A1", "kind": "audio", "clips": [
      {"id": "music", "kind": "audio", "src": "{{music}}", "start": 0, "dur": 7, "gain": 0.6, "anim": "fade_out"}
    ]}
  ]
}
```

With 4 items the video lasts 7 + 3 * 3 = 16 s. The photos start at 2, 5, 8 and 11 s. The outro starts at 14 s. Replace `a_1` to `a_4` with image ids from `list_assets`. The template defaults `a_logo` and `a_music` also name library assets.

```json vars
{
  "title": "Autumn drop",
  "slides": [
    {"photo": {"asset": "a_1"}, "caption": "Field jacket"},
    {"photo": {"asset": "a_2"}, "caption": "Brick lane"},
    {"caption": "Crosswalk", "photo": {"asset": "a_3"}},
    {"photo": {"asset": "a_4"}, "caption": "Trio on steps"}
  ],
  "cta": "Shop the drop",
  "brand_color": "#FFB224"
}
```

## Media by URL

A video, image or audio variable takes `{"url": "https://..."}` in place of a library asset id. The caller uploads nothing.
The api-service fetches each address into R2 before it submits the job, so the worker sees only asset ids and R2 URLs.
During the fetch, the render stays `queued` with `step: "fetching_media"`. A fetched file stays cached per user for 24 hours.
A caption clip takes a voice-over by URL. Coco transcribes it after the fetch and keeps the transcript with the fetched file for the same 24 hours.
The section Media by URL of https://compositor.video/llm/mcp.md lists the limits and the error codes.

## Captions

A caption clip shows the words of a voice, one page at a time. The word that is spoken now has the `highlight` colour. The other words have `color`. Each line is centred.

- The `src` of a caption clip is an audio variable. Add an audio clip that plays the same variable, so the video has the sound.
- Time 0 of the voice plays at the caption clip's `start`, the same rule as for an audio clip. Give both clips the same `start` and `dur`.
- A page holds at most `max_words` words on at most `max_lines` lines. A pause of 600 ms starts a new page.
- In a template with a list, a caption clip sits before the section, after it, or covers the whole section. Its `src` is a top-level audio variable, not a list field.
- A caption clip with no words in its time window shows nothing, and the render succeeds.
- The service transcribes each voice asset once, at its first render with a caption clip. Meanwhile the render stays `queued` with `"step": "transcribing"`. Later renders of the same asset use the stored transcript.
- Call `get_transcript` with the asset id to read the words and their times.
- The voice can also be a URL value, `{"url": "https://..."}`. Coco fetches the file first, then transcribes it. The render shows `fetching_media`, then `transcribing`.
- The transcript of a URL voice stays with the fetched file for 24 hours. A second render of the same address in that time transcribes nothing. After that, Coco fetches and transcribes the address again.
- A URL transcription counts toward the 60 min a month, the same as an asset. `get_transcript` does not take a URL voice.

| Limit | Value |
|---|---|
| Caption clips per template | 8 |
| Variables that caption clips bind, per template | 2 |
| Words per render, all caption clips together | 1200 |
| Length of a voice asset | 10 min |
| New transcription per account per month | 60 min. A stored transcript does not count again. |

Captions support languages written left to right with spaces between words. Arabic, Hebrew, Chinese, Japanese, Thai and Russian are examples of languages that fail with `caption_language_unsupported`.

| Code | Where and why |
|---|---|
| `captions_unavailable` | 422 at `render_template`. This service does not render captions yet. |
| `monthly_transcription_minutes` | 429 at `render_template`, or a failed render. The 60 min of new transcription for this month are used. |
| `transcript_not_found` | 404 at `get_transcript`. No render has transcribed the asset yet. |
| `caption_source_no_audio` | Failed render. The voice asset has no sound. |
| `caption_extract_failed` | Failed render. The worker did not read the voice asset. |
| `caption_source_too_long` | Failed render. The voice asset is longer than 10 min. |
| `caption_transcribe_failed` | Failed render. Three transcriptions failed. Try the render again later. |
| `caption_language_unsupported` | Failed render. Captions do not support the language of the voice. |
| `caption_too_many_words` | Failed render. The caption clips hold more than 1200 words. |
| `caption_timeout` | Failed render. The transcription took more than 45 min. Try the render again. |
