# API docs (api-docs)

An interactive API reference at `/docs`, rendered by [Scalar](https://scalar.com) from the OpenAPI document your app already generates. Every resource, custom route, field rule and policy shows up without annotations.

```bash
npx calm add api-docs
```

| Adds | |
|---|---|
| Files | `app/blocks/api-docs/index.js` |
| Environment | `API_DOCS=on` |
| Routes | `GET /docs` (HTML page), `GET /docs/init.js` |

Open `http://localhost:5001/docs`. The page loads `/api/openapi.json` and lets you browse and try every endpoint, with a session token as bearer authentication.

## When it is served

- In **development** and **test** mode: always.
- In **production**: only when the environment has `API_DOCS=on`.
- Never when `openapi.enabled` is `false` in the config.

`calm add` writes `API_DOCS=on` to `.env` and `.env.example`. If your production environment is built from `.env.example`, remove that line there (or set `API_DOCS=off`) unless you want a public reference in production.

## Notes

- The Scalar script is pinned to an exact version and loaded from jsDelivr (`@scalar/api-reference@1.72.1`). Change the `SCALAR` constant in the block to upgrade it, or self-host the file.
- The page has its own Content-Security-Policy, which allows that script and Scalar's fonts and nothing else.
- What the reference shows is what OpenAPI shows: hidden and write-only fields are not in output schemas, PUT aliases are not listed, and routes with `hidden: true` are left out. Each operation's description says who may call it (`Access: owner | admin`).

<!-- block-guide:start (generated from block.json by scripts/sync-block-guides.mjs) -->

## Official documentation

Read these before changing the block; they are also in `app/blocks/api-docs/AGENTS.md` and in the devtools Blocks panel.

- [OpenAPI Specification (latest)](https://spec.openapis.org/oas/latest.html)
- [OpenAPI Initiative: Learn OpenAPI](https://learn.openapis.org/)
- [Scalar API reference (GitHub)](https://github.com/scalar/scalar)
- [MDN: Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy)
- [JSON Schema](https://json-schema.org/)

## For AI agents

Coding agents read these notes in `app/blocks/api-docs/AGENTS.md`:

- Read first: `app/blocks/api-docs/index.js` (about 50 lines). It only serves `GET /docs` (an HTML page) and `GET /docs/init.js`; the OpenAPI document itself is generated by the core from your routes and resources at `${config.apiPrefix}${config.openapi.path}` (by default `/api/openapi.json`).
- Configuration: no `calm.config.js` options. It is on when `config.mode !== 'production'` or `API_DOCS=on`, and only while `config.openapi.enabled` is true.
- To improve what the reference shows, edit route options (`summary`, `description`, `tags`, `input`) and field `.describe()` in your resources, not this block.
- Invariant: keep the `Content-Security-Policy` header on `/docs`. If you change where Scalar is loaded from (or self-host it), update `script-src`, `style-src` and `font-src` in the `csp` string in the same change.
- Invariant: everything interpolated into the HTML goes through `escapeHtml` (app name, spec URL). Keep the init code in `/docs/init.js`: the CSP forbids inline scripts.
- The Scalar script is pinned in the `SCALAR` constant (`@scalar/api-reference@1.72.1`). Bump it on purpose, never use an unversioned URL.
- Production: the reference lists every route and schema. Only set `API_DOCS=on` when the API is meant to be public.
- Pitfall: the middleware handles GET only and lives outside `apiPrefix`; do not register your own `/docs` route at the root.
- Check your work: open http://localhost:5001/docs and validate `/api/openapi.json` against the OpenAPI specification.

<!-- block-guide:end -->
