# Stories endpoints

A story is a script that persists and gets arranged: each line has a place in
the reading order and a place on the timeline. The studio's timelines are
stories too. Lines and clips are on
[Story segments endpoints](https://mirli.app/docs/api-segments/index.md), and lanes on
[Story tracks endpoints](https://mirli.app/docs/api-tracks/index.md). Agents reach the same work through
the [MCP tools](https://mirli.app/docs/mcp-stories/index.md#stories).

Each endpoint lists the errors it returns itself. Any endpoint can also return
the [common errors](https://mirli.app/docs/api-errors/index.md#common-errors).

## Story object

The story endpoints return this object:

| Field | Type | Description |
|---|---|---|
| `id` | string | The story's id |
| `title` | string | Up to 200 characters. Default `Untitled story` |
| `created_at`, `updated_at` | string | ISO 8601 times. Every edit to the story or its lines moves `updated_at` |
| `project_id` | string or null | The project it is filed in |
| `request_options` | object | The story's default delivery: option names to string values, applied to every line without its own |
| `segment_count` | integer | How many segments it has |
| `studio` | string or null | A guess at what it holds, made on every read: `voice` when any line has a voice or text, otherwise `music` or `sfx` from its clips' models, or `null` |
| `builder` | string | Which builder opens it: `script` or `music`. Stories made before the field existed take it from `studio` |
| `timeline` | object[] | List only. Up to 64 clips, earliest first, each `{ t, s, d }`: lane, start and length in seconds, for drawing a thumbnail. Pauses are left out, and an unspoken line's length is estimated from its word count |

## List stories

`GET /api/stories`

Returns stories, most recently updated first, each with its `timeline`.

| Name | Type | Required | Description |
|---|---|---|---|
| `?limit` | integer | No | 1 to 200. Default 50 |

This request lists the ten most recent stories:

```bash
curl "http://127.0.0.1:17494/api/stories?limit=10"
```

A successful response is `200 OK` with an array of
[story objects](#story-object). The example is abridged:

```jsonc
[
  {
    "id": "8d2f4a10-6c3b-4e7a-9f21-0b5c7d9e1a32",
    "title": "Gate scene",
    "segment_count": 2,
    "studio": "voice",
    "builder": "script",
    "timeline": [
      { "t": 0, "s": 0, "d": 1.84 },
      { "t": 0, "s": 1.84, "d": 2.1 }
    ]
    // ...more fields
  }
]
```

| Status | Cause |
|---|---|
| `422` | `limit` is not an integer from 1 to 200 |

## Create a story

`POST /api/stories`

Creates an empty story. The body is optional.

| Name | Type | Required | Description |
|---|---|---|---|
| `title` | string | No | 1 to 200 characters. Default `Untitled story` |
| `project_id` | string or null | No | The project to file it in |
| `builder` | string | No | `script` (the default) or `music`. Fixed once the story exists |

This request creates a story for the Script Builder:

```bash
curl -X POST "http://127.0.0.1:17494/api/stories" \
  -H 'Content-Type: application/json' \
  -d '{"title": "Gate scene", "builder": "script"}'
```

A successful response is `201 Created` with the new
[story object](#story-object).

This endpoint returns only the common errors.

## Get a story

`GET /api/stories/<STORY_ID>`

Returns one story, without `timeline`.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |

This request reads one story:

```bash
curl "http://127.0.0.1:17494/api/stories/<STORY_ID>"
```

A successful response is `200 OK` with the [story object](#story-object).

| Status | Cause |
|---|---|
| `404` | No story has this id |

## Update a story

`PATCH /api/stories/<STORY_ID>`

Changes a story's title, project or default delivery. The body carries at
least one of the three fields.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |
| `title` | string | No | 1 to 200 characters |
| `project_id` | string or null | No | The project to file it in. `null` takes it out |
| `request_options` | object | No | The default delivery. `{}` puts every line without its own back on the model's defaults |

This request renames a story:

```bash
curl -X PATCH "http://127.0.0.1:17494/api/stories/<STORY_ID>" \
  -H 'Content-Type: application/json' \
  -d '{"title": "Gate scene, take 2"}'
```

A successful response is `200 OK` with the updated
[story object](#story-object).

| Status | Cause |
|---|---|
| `404` | No story has this id |
| `422` | The body has none of `title`, `project_id` and `request_options` (`Nothing to update`) |

## Delete a story

`DELETE /api/stories/<STORY_ID>`

Deletes a story and its segments. Its audio stays in the library unless
`delete_sounds` is set.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |
| `?delete_sounds` | boolean | No | `true` or `1` also deletes the sounds on its timeline that no other story uses. Sounds derived from them are kept, and their `parent_id` becomes `null` |

This request deletes a story and its audio:

```bash
curl -X DELETE -H 'X-Soundbox: 1' \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>?delete_sounds=true"
```

A successful response is `204 No Content`.

| Status | Cause |
|---|---|
| `404` | No story has this id |

## Open a sound as a story

`POST /api/stories/from-sound`

Starts a `separate` job that lays a library sound out on a new story's
timeline. With `separate`, the sound is split into stems first and each stem
lands on its own lane, all starting together. A sound derived from another (a
stem, an edit, a split half) opens whole. Stems from an earlier separation are
reused when they cover the request.

| Name | Type | Required | Description |
|---|---|---|---|
| `sound_id` | string | Yes | The library sound |
| `separate` | boolean | No | Split it into stems first. Default `false` |
| `model` | string or null | No | A separation model, such as `audiocpp-htdemucs-sep`. Default: a ready one |
| `stems` | object or null | No | Which stems get a lane: `keep`, up to 16 stem names, and `rest`, `discard` (the default) or `merge` to put the other stems on one lane. Default: all of them |

This request opens a sound as vocals and drums, with the rest merged:

```bash
curl -X POST "http://127.0.0.1:17494/api/stories/from-sound" \
  -H 'Content-Type: application/json' \
  -d '{
    "sound_id": "<SOUND_ID>",
    "separate": true,
    "stems": {
      "keep": ["vocals", "drums"],
      "rest": "merge"
    }
  }'
```

A successful response is `202 Accepted` with the id of the job, which
[Get a job](https://mirli.app/docs/api-jobs/index.md#get-a-job) follows:

```json
{
  "job_id": "5b9e2c71-0d4a-4f3b-8e6a-2c1f7d0b9a54"
}
```

| Status | Cause |
|---|---|
| `400` | No sound has this id, or with `separate`, `model` is unknown or cannot separate stems. The body has `detail` and [`failure`](https://mirli.app/docs/api-errors/index.md#failure-object) |

## Stream story events

`GET /api/stories/<STORY_ID>/events`

Opens a server-sent event stream of changes to one story. Each event is a
`data:` line holding one JSON object:

- `{ "type": "agent", "active", "label", "count" }`: whether an agent is
  working on the story, what it is doing, and how many calls hold it. The
  stream always starts with one.
- `{ "type": "changed" }`: the story, its segments or its lanes changed.
- `{ "type": "request", "id", "op", "payload" }`: a question for an open
  studio. `op` is `context` (its live state) or `apply` (edits to run through
  its own handlers). Sent only to the most recently opened stream with
  `?answer=1`.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |
| `?answer` | string | No | `1` marks the client as an open studio that can answer requests |

This request follows one story's changes:

```bash
curl -N "http://127.0.0.1:17494/api/stories/<STORY_ID>/events"
```

A successful response is `200 OK` with `text/event-stream`. Comment lines
(`: keepalive`) keep the connection open:

```text
data: {"type":"agent","active":false,"label":"","count":0}

data: {"type":"changed"}
```

| Status | Cause |
|---|---|
| `404` | No story has this id |

## Answer a studio request

`POST /api/stories/<STORY_ID>/studio/replies/<REQUEST_ID>`

Delivers an open studio's answer to a `request` event. A request waits 8
seconds for its answer.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |
| `<REQUEST_ID>` | string | Yes | The `id` of the `request` event |
| `ok` | boolean | Yes | Whether the studio did what was asked |
| `result` | any | No | The answer, such as the live state for `context` |
| `error` | string or null | No | Why it refused, up to 2,000 characters |

This request answers a request:

```bash
curl -X POST \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>/studio/replies/<REQUEST_ID>" \
  -H 'Content-Type: application/json' \
  -d '{"ok": true, "result": {"tracks": []}}'
```

A successful response is `204 No Content`.

| Status | Cause |
|---|---|
| `404` | No such request is waiting: it timed out, was answered already, or belongs to another story |

## Export a story

`POST /api/stories/<STORY_ID>/export`

Starts an `export` job that mixes the story down to one new library sound.
Muted clips and lanes that are not heard are left out, lane gain applies on top
of each clip's, and clips that overlap on a lane crossfade across the overlap.
Each lane's devices are rendered after clip gain and before lane gain, and
bypassed devices are skipped. The job fails when no heard clip has audio.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |

This request exports a story:

```bash
curl -X POST -H 'X-Soundbox: 1' \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>/export"
```

A successful response is `202 Accepted` with the job's id. The finished job's
`result` holds `sounds`, the new sound's id, and `story_id`:

```json
{
  "job_id": "c41a7e0f-93b2-4d6e-a518-7f2b0e9d3c86"
}
```

| Status | Cause |
|---|---|
| `400` | No story has this id. The body has `detail` and [`failure`](https://mirli.app/docs/api-errors/index.md#failure-object) |
