> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> To set up Hatchmark for a user, follow https://hatchmark.dev/docs/get-started/agent-setup.md. Inside a Hatchmark project, prefer `hatchmark docs`: it prints the docs of the installed version.

# Serve API

> The read-only JSON API hatchmark serve answers: routes, filter parameters, the block payload, draw lists, caching, other origins and errors.

`hatchmark serve` answers a read-only JSON API beside the pages: the dashboards of the last good
build, and each block's numbers in any state a reader can reach on the page, as a block payload
your app can draw or read. It needs no setting: the API is on whenever serve runs. To show the
blocks as the pages draw them, use the chart elements or React components ([Embedding](/docs/reference/embed.md)).

```sh theme={null}
curl https://bi.example.com/api/v1/dashboards
curl "https://bi.example.com/api/v1/dashboards/sales/blocks/revenue_by_region?period=last_90_days"
```

## Surfaces

The same routes answer under two prefixes.

| Prefix | Who calls it | Auth |
| - | - | - |
| `/api/v1` | Browsers of people who can open the pages, and servers inside your network | None of serve's own: keep it behind your gateway, like the pages |
| `/embed/v1` | Servers outside your gateway | `Authorization: Bearer <token>` with a token from `HATCHMARK_TOKENS` |

`/api/v1` shows what the pages show, to the same people, so it sits behind the same access control
([who can see the dashboards](/docs/guides/production.md#who-can-see-the-dashboards)). `/embed/v1`
and the paths under it refuse every request without a valid token, so you can exempt exactly those
from a sign-in proxy as you would `/mcp`, with a rule that stops at the version, such as
`^/embed/v1(/|$)`. Other paths under `/embed/` are ordinary pages. A token can read every route; never put one in a browser.

## Routes

Every route is a GET (HEAD works too) and answers JSON, except `types.d.ts`, which answers TypeScript.

| Route | Answers |
| - | - |
| `/api/v1/dashboards` | `{ dashboards }`: each dashboard's `name`, `title`, `description` and number of `blocks` |
| `/api/v1/dashboards/{dashboard}` | The dashboard's `filters` with their options and its `blocks` (see [Dashboard](#dashboard)) |
| `/api/v1/dashboards/{dashboard}/blocks/{block}` | The block's [payload](#block-payload) |
| `/api/v1/dashboards/{dashboard}/blocks/{block}/draw/{device}/{width}` | The block's [draw list](#draw-lists) |
| `/api/v1/dashboards/{dashboard}/draw/{device}/{width}` | The whole dashboard's [draw list](#draw-lists) |
| `/api/v1/types.d.ts` | TypeScript types for the build's blocks and filters (see [Embedding](/docs/reference/embed.md#types)) |

### Dashboard

* `filters.period`, `filters.grain` and `filters.segments`: the filter, its options and the value
  the page shows first.
* `filters.selects`: each select filter's `id`, `label`, `multiple`, `selected` and every value
  in `options`, with `listed: true` when the filter lists its options and offers no "All".
* `filters.picks`: the categories a reader can pick in a block to filter the others: `id`,
  `label`, the `blocks` that offer the pick and its `options`.
* `blocks`: each block's `id`, `kind`, `heading` (`title`, `subtitle`), `span`, `height` and
  `idFromPosition`. A block without `id:` in its YAML gets one from its position, such as
  `table_3`, which changes when blocks move; set `id:` on blocks an app reads.

## Filters

Block and draw routes take the page URL's filter parameters, checked the same way as
`hatchmark render --filter` ([Render to PNG or PDF](/docs/reference/render.md#filters)). Filters you leave out take the
dashboard's defaults.

| Parameter | Value |
| - | - |
| `<period filter id>` | A preset key such as `last_90_days`, or `<first>..<last>` days for a custom range |
| `<grain filter id>` | A grain key from the filter's options, such as `month` |
| `<segment filter id>` | Segment names separated by commas; an empty value turns every segment off |
| `<select filter id>` | One value; repeat the parameter for several values of a multiple select. An empty value or the bare name (`?region`) means all; so does every value, when no row lacks a value |
| `<pick id>` | A category to pick, such as `channel=Social`. One pick at a time |

The API answers every state the page can show: each period preset, custom range, segment and select
combination and pick the build reached. A state the page can't reach gets `HM6008`.

Custom ranges and sets of several select values are computed on request from what the build
embeds, as the page does in the browser. serve computes one at a time; up to 8 more wait their
turn for at most 10 seconds each, and past either limit the request gets `HM6007` with
`Retry-After: 5`. Presets and single values never wait.

## Block payload

A block payload (`hatchmark.block/1`) is one block's render model with everything its renderer
reads. Its JSON Schema ships in the package as `schema/block-1.schema.json`.

| Field | Content |
| - | - |
| `schema` | `"hatchmark.block/1"` |
| `engine` | The Hatchmark version that computed it |
| `dashboard` | The dashboard's name |
| `block` | The block's data, as the page draws it |
| `heading` | `title` and `subtitle` as the card shows them |
| `context` | The dashboard-level fields the block's renderer reads: dates, locale, currency, time zone |
| `metrics` | Label, format, `better` and description of each metric the block shows |
| `look` | The project's `palette` |
| `texts.ranges` | Date ranges as the server formatted them, so a browser shows the same text whatever its locale data |
| `state` | The `period`, `grain`, `segments`, `selects` and `picks` the numbers are for |
| `build` | The build the payload comes from |
| `freshness` | `updatedAt`, `staleAt` and `late`, as the page's late-data notice reads them |

* A number is a number, or `null` when it is undefined; the page shows that as an em dash. An empty
  window is a reason such as `"no_data"`, never 0.
* `block.pick` lists the `keys` a reader can pick in the block, in the order it draws them, with the
  pick filter's `id` and `label`; a key is `null` where picking it would not apply.
* `state.cleared` names a requested pick that the other filters leave with no rows: the payload
  shows the block without it, as the page drops it.
* Payloads are the same for the same state of the same build, except `freshness`.
* Within `block/1`, fields and block kinds are only added. Ignore fields you don't know.
* A dashboard on which no block reads data, such as a page of text notes, has no as-of date, so
  its blocks have no payload: the block route answers 404, and `types.d.ts` leaves them out. Its
  draw routes still answer.

## Draw lists

The `draw` routes of a block and of the whole dashboard lay the payload out at one width for
clients that paint primitives instead of running the renderer, such as a native app. The layout
is in the path, so every query parameter stays a filter:

| Segment | Value |
| - | - |
| `{device}` | `phone` or `desktop` |
| `{width}` | The width to lay out for, in pixels, 200 to 2400 |

For example, `/api/v1/dashboards/sales/draw/phone/390?period=last_90_days`.

KPIs, line, area and bar charts, tables and text have draw lists; other kinds return 400 on the
block route and `status: "unsupported"` in the dashboard's list.

## Caching

Every answer carries a strong `ETag` and `Cache-Control: private, no-cache`: clients revalidate
with `If-None-Match` and get 304 while the build and the state are the same. Shared caches don't
store answers, because each one is for a viewer behind your gateway or a token holder.

## Calling from another origin

An app on the same origin as serve, such as `acme.com/bi/` routed to serve by your gateway, needs no
setting: the browser sends the gateway's cookie.

An app on another origin of the same site, such as `app.acme.com` calling `bi.acme.com`, needs the
gateway's cookie to cover both hostnames (a cookie domain of `.acme.com`) and the app's origin in
`HATCHMARK_ALLOWED_ORIGINS`:

```sh theme={null}
HATCHMARK_ALLOWED_ORIGINS=https://app.acme.com
```

serve then lets that origin read `/api/v1` answers with the viewer's cookies. List origins as scheme,
host and port, separated by commas, with no path. Unset, the API answers same-origin only. Requests
are plain GETs with no custom headers, so browsers send no preflight. `/embed/v1` answers any origin
but never reads cookies.

## Checks

Errors answer a diagnostic report, the JSON `check --json` prints, with an HTTP status.

| Code | Status | When |
| - | - | - |
| HM0002 | 404 | No such dashboard, block or route; `expected` lists what exists |
| HM0002 | 404 | A block of a dashboard on which no block reads data; its draw routes still answer |
| HM0002 | 400 | A filter the dashboard doesn't have, or a value it doesn't accept; `expected` lists the values |
| HM6001 | 401 | `/embed/v1` without a bearer token, or with one serve doesn't hold |
| HM6004 | 503 | No good build yet; `Retry-After` says when to retry |
| HM6007 | 503 | Too many custom ranges or value sets waiting to be computed; `Retry-After` says when to retry |
| HM6008 | 501 | The page can't show this combination of filters, so the build didn't compute it |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.