API docs (api-docs)
An interactive API reference at /docs, rendered by Scalar from the OpenAPI document your app already generates. Every resource, custom route, field rule and policy shows up without annotations.
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.enabledisfalsein 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 theSCALARconstant 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: trueare left out. Each operation's description says who may call it (Access: owner | admin).
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)
- OpenAPI Initiative: Learn OpenAPI
- Scalar API reference (GitHub)
- MDN: Content-Security-Policy
- JSON Schema
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 servesGET /docs(an HTML page) andGET /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.jsoptions. It is on whenconfig.mode !== 'production'orAPI_DOCS=on, and only whileconfig.openapi.enabledis 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-Policyheader on/docs. If you change where Scalar is loaded from (or self-host it), updatescript-src,style-srcandfont-srcin thecspstring 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
SCALARconstant (@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=onwhen the API is meant to be public. - Pitfall: the middleware handles GET only and lives outside
apiPrefix; do not register your own/docsroute at the root. - Check your work: open http://localhost:5001/docs and validate
/api/openapi.jsonagainst the OpenAPI specification.