Build an OpenAPI 3.1 description from real route handlers: framework generators first, the bundled extract.ts route finder for Express and Next.js, then lint.
---
name: api-docs
description: Documents an HTTP API as an OpenAPI 3.1 description built from the actual route handlers. Uses the framework's own generator when it has one (FastAPI, NestJS, Fastify, Flask-smorest) and the bundled extract.ts to list Express/Fastify-style and Next.js App Router routes otherwise, then fills schemas from code and lints the result. Use when asked for API docs, an OpenAPI or Swagger file, or an endpoint reference.
compatibility: The bundled extract.ts needs Node.js 22.18+ or 23.6+ (or npx tsx on older Node). Linting uses npx @redocly/cli, which downloads the package.
metadata:
version: "1.5.0"
---
# API Documentation Generator
Produces an OpenAPI description that matches the code, not a guess at it.
## Files in this skill
- `SKILL.md` (this file)
- `extract.ts` — static route finder. Prints JSON routes with `file:line`, or an OpenAPI 3.1
skeleton with `--openapi`. Finds `<router>.get/post/put/patch/delete/options/head("/path")`
calls (skips `axios.`/`fetch`-style client calls, tests, `node_modules`, build output) and
Next.js `app/**/route.(ts|js)` exports (`GET`, `POST`, ...; route groups `(name)` removed).
It does not apply prefixes mounted with `app.use("/v1", router)`; it prints them as notes.
## Workflow
1. **Use the framework's generator first** if the project has one:
- FastAPI: the app already serves `/openapi.json`; export it and refine with
`response_model`, `summary` and examples in code.
- NestJS: `@nestjs/swagger` (`SwaggerModule.createDocument`).
- Fastify: `@fastify/swagger` with route `schema` objects.
- Flask: flask-smorest (or apispec) blueprints and schemas.
Generating from code annotations keeps the docs in sync; only fall back to step 2 when no
generator exists (typically Express, Koa-style routers or Next.js route handlers).
2. **List routes** from the repository root:
```bash
node <skill-dir>/extract.ts src --openapi --title "Orders API" --version 1.0.0 > openapi.json
node <skill-dir>/extract.ts app --openapi > next-routes.json # Next.js App Router
```
Read every `note:` on stderr. Apply mount prefixes by hand (for example `/users` mounted at
`/v1` becomes `/v1/users`). Search for routes the regex cannot see (routes registered in
loops or from config) and add them.
3. **Fill each operation from the handler code**, replacing every `TODO`:
- `summary`/`description` from what the handler does.
- Path, query and header parameters from how the handler reads them. Path parameters are
always `required: true` in OpenAPI; optional Express segments (`:id?`) and Next.js
catch-all segments (`[...path]`) need one documented path per real variant or a note.
- `requestBody` and response schemas from validation code or types (zod, TypeScript
interfaces, DTOs). Put shared shapes in `components/schemas`.
- Every status code the handler can return, including 4xx/5xx error bodies.
- `security` and `components/securitySchemes` from the auth middleware actually used.
- `servers` only with URLs the user confirms.
Do not document behaviour you did not see in code. Mark unknowns as questions for the user.
4. **Lint** and fix until clean:
```bash
npx @redocly/cli lint openapi.json
```
5. Optionally render for reading (`npx @redocly/cli build-docs openapi.json`) or produce a
Markdown reference from the same file. Do not start long-running servers without asking.
## Output
- `openapi.json` (or YAML) using `openapi: 3.1.0`, one operation per method and path.
- A short list of gaps: undocumented status codes, routes found only by hand, assumptions.
## Limitations
- `extract.ts` is text matching, not a type checker: it cannot read schemas, resolve dynamic
paths or follow mounted routers. It is a starting inventory, not finished documentation.
- Generated docs are only as accurate as the handler code read in step 3; re-run after API
changes.
## Example
Input: an Express app with `router.get("/users/:id", show)` in `src/routes/users.ts` mounted
via `app.use("/v1", users)`, `show` returning `404` when the user is missing, and a zod schema
`User = { id: string, email: string }`.
Expected result excerpt after step 3:
```yaml
paths:
/v1/users/{id}:
get:
summary: Get a user by ID
operationId: getUser
parameters:
- { name: id, in: path, required: true, schema: { type: string } }
responses:
'200':
description: The user
content:
application/json:
schema: { $ref: '#/components/schemas/User' }
'404':
description: No user with this ID
```
Write or update a README from facts in the repository's manifest, scripts and docs, leaving TODOs instead of invented badges, features or install commands.
Create operational runbooks
Maintain project changelogs
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
An Express service has no API documentation. The users router is mounted under /v1, and one handler returns 404 for unknown IDs.
Generate OpenAPI docs for this Express app. src/server.ts has `app.use("/v1", users)`; src/routes/users.ts has `router.get("/users/:id", show)`; `show` returns 404 when the user is missing and otherwise a zod `User = { id: string, email: string }`. Auth is a bearer-token middleware on all routes.1) `node extract.ts src --openapi` lists GET /users/{id} with src/routes/users.ts:1 and prints a note that the /v1 mount prefix was not applied.
2) The finished openapi.json/yaml documents GET /v1/users/{id} with a required string path parameter id, a 200 response referencing components/schemas/User (id and email required), a 404 response, a bearer securityScheme applied at root level, and no TODO left.
3) `npx @redocly/cli lint` reports the description as valid; any remaining warnings are listed with a reason (for example a localhost server URL).AI Agent Skills are reusable workflow packages that extend your AI coding assistant with multi-step capabilities. Install api-docs by following these steps.
SKILL.md inside, in the location documented by your client. Do not assume every AI application reads the same directory.Requires an Agent Skills-compatible client; supported locations and activation vary by version. Read the SKILL.md setup guide. MCP connects tools and data and does not, by itself, install a skill.