api-docs-generator
npx machina-cli add skill Nembie/claude-code-skills/api-docs-generator --openclawAPI Docs Generator
Before generating any output, read config/defaults.md and adapt all patterns, imports, and code examples to the user's configured stack.
Process
- Scan the
app/api/directory for allroute.tsfiles. - For each route file, extract: exported HTTP method handlers, URL path (from file path), request/response types, Zod schemas, middleware/auth patterns.
- Generate a valid OpenAPI 3.1 specification.
- Output as YAML (default) or JSON if requested.
Route Scanning
Path Extraction
Convert Next.js App Router file paths to OpenAPI paths:
| File path | OpenAPI path |
|---|---|
app/api/users/route.ts | /api/users |
app/api/users/[id]/route.ts | /api/users/{id} |
app/api/posts/[postId]/comments/route.ts | /api/posts/{postId}/comments |
app/api/[...slug]/route.ts | /api/{slug} (note: catch-all) |
Method Extraction
Detect exported functions: GET, POST, PUT, PATCH, DELETE. Each becomes an operation in the spec.
Schema Extraction
From Zod Schemas
If the route uses Zod for validation, extract the schema structure directly:
// Detect this pattern:
const createSchema = z.object({
name: z.string().min(1).max(100),
email: z.string().email(),
});
// Convert to OpenAPI:
// type: object
// properties:
// name: { type: string, minLength: 1, maxLength: 100 }
// email: { type: string, format: email }
// required: [name, email]
From TypeScript Types
If no Zod schema exists, infer from TypeScript type annotations on the request/response.
Auth Detection
Detect common auth patterns and add security schemes:
// Pattern: session check → Bearer auth or cookie session
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
// Pattern: API key header → API key auth
const apiKey = request.headers.get("x-api-key");
Response Detection
Scan NextResponse.json(...) calls and their status codes to build response schemas:
// 200 with data shape
return NextResponse.json({ data: items, pagination: {...} });
// 400 with error shape
return NextResponse.json({ error: "...", details: ... }, { status: 400 });
Common Patterns
Pagination Parameters
Detect page, limit, offset, cursor in query params and document as standard pagination:
parameters:
- name: page
in: query
schema: { type: integer, minimum: 1, default: 1 }
- name: limit
in: query
schema: { type: integer, minimum: 1, maximum: 100, default: 20 }
File Uploads
Detect request.formData() and document as multipart/form-data.
Consistent Error Schema
Extract the error response format used across routes and define as a reusable component:
components:
schemas:
ErrorResponse:
type: object
properties:
error: { type: string }
details: {}
required: [error]
Output Structure
openapi: "3.1.0"
info:
title: "{project name} API"
version: "1.0.0"
paths:
/api/users:
get:
summary: List users
parameters: [...]
responses:
"200":
description: Success
content:
application/json:
schema: { $ref: "#/components/schemas/UserListResponse" }
post:
summary: Create user
requestBody:
content:
application/json:
schema: { $ref: "#/components/schemas/CreateUserInput" }
responses:
"201": ...
"400": ...
components:
schemas:
User: ...
CreateUserInput: ...
securitySchemes:
bearerAuth:
type: http
scheme: bearer
Output Format
## Generated API Documentation
**File**: `openapi.yaml` (or `openapi.json`)
**Endpoints discovered**: X
**Schemas extracted**: X
[Generated spec]
### Endpoints Summary
| Method | Path | Summary | Auth |
|--------|------|---------|------|
| GET | /api/users | List users | Yes |
| POST | /api/users | Create user | Yes |
Reference
See references/openapi-patterns.md for OpenAPI 3.1 structure, Zod-to-OpenAPI mapping, and security scheme patterns.
Source
git clone https://github.com/Nembie/claude-code-skills/blob/main/skills/api-docs-generator/SKILL.mdView on GitHub Overview
api-docs-generator builds a complete OpenAPI 3.1 specification by scanning Next.js App Router API routes. It maps file paths to /api endpoints, extracts HTTP handlers, and derives schemas from Zod or TypeScript types, including auth and response shapes. The output is ready for publication in YAML (default) or JSON when requested.
How This Skill Works
The tool scans app/api/**/route.ts files to identify exported HTTP methods (GET, POST, PUT, PATCH, DELETE). It converts file paths into OpenAPI paths, infers request/response schemas from Zod or TS types, detects common auth patterns, and aggregates response codes. Finally, it renders a valid OpenAPI 3.1 document in YAML by default, with JSON as an option.
When to Use It
- You need a fresh OpenAPI 3.1 specification for a Next.js project with API routes.
- You want to document endpoints including path parameters, query params, and request/response schemas.
- You require a machine-readable Swagger/OpenAPI file for client generation or docs tooling.
- You want security schemes inferred from auth patterns used in routes (Bearer, API keys, etc.).
- You need to validate and publish API documentation in a viewer or gateway that consumes OpenAPI.
Quick Start
- Step 1: Ensure your Next.js app uses the App Router and has route.ts files under app/api.
- Step 2: Run the api-docs-generator to produce an OpenAPI 3.1 document (YAML by default).
- Step 3: If needed, request JSON output and validate the spec before publishing.
Best Practices
- Prefer routes that use Zod schemas to maximize OpenAPI accuracy.
- Keep route files under app/api and follow the standard Next.js route.ts conventions.
- Declare explicit request/response schemas and a consistent error schema across routes.
- Document pagination and file uploads using standard OpenAPI patterns when detected.
- Validate the generated OpenAPI doc with a validator and integrate into your docs UI.
Example Use Cases
- Document a user management API with /api/users (GET, POST) and /api/users/{id} (GET, PUT, DELETE).
- Document a blog API with /api/posts/{postId} (GET, PUT) and /api/posts/{postId}/comments (GET, POST).
- Handle a catch-all route /api/{slug} mapped to /api/{slug} for dynamic paths.
- Annotate and document file upload endpoints using multipart/form-data when detected.
- Export the spec as YAML for a docs site or JSON for Swagger UI integration.