A fully automated API documentation generator that creates OpenAPI 3.0 specifications, Swagger UI, and Markdown docs from entity definitions. This proves that comprehensive, production-ready API documentation can be generated with ZERO manual writing.
const User = defineEntity('User', {
fields: {
email: text().required().email(),
name: text().required().min(2).max(100),
age: number().optional().min(0).max(150).integer(),
role: enumField('admin', 'user', 'guest').default('user'),
},
behaviors: {
timestamps: true,
softDelete: true,
},
protected: 'write', // list/get public, create/update/remove protected
})npx archetype generateGenerates:
generated/docs/openapi.json- 924 lines - Complete OpenAPI 3.0 specgenerated/docs/swagger.html- 37 lines - Interactive Swagger UIgenerated/docs/API.md- 136 lines - Markdown documentation
Complete spec including:
- API metadata (title, version, description)
- Server configurations (dev/prod)
- All CRUD endpoints for each entity
- Request/response schemas
- Authentication/security definitions
- Validation constraints (min/max, patterns, enums)
- Pagination and filtering parameters
- Error responses (400, 401, 404)
Example Schema:
{
"User": {
"type": "object",
"properties": {
"id": { "type": "string", "description": "Unique identifier" },
"email": { "type": "string", "format": "email" },
"name": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"age": {
"type": "integer",
"format": "int32",
"minimum": 0,
"maximum": 150
},
"role": {
"type": "string",
"enum": ["admin", "user", "guest"],
"default": "user"
},
"isActive": { "type": "boolean", "default": true },
"createdAt": { "type": "string", "format": "date-time" },
"updatedAt": { "type": "string", "format": "date-time" },
"deletedAt": {
"type": "string",
"format": "date-time",
"nullable": true,
"description": "Soft delete timestamp"
}
}
}
}Example Endpoint:
{
"/api/trpc/user.create": {
"post": {
"summary": "Create User",
"description": "Create a new User record",
"operationId": "userCreate",
"tags": ["User"],
"security": [{ "bearerAuth": [] }],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UserCreateInput" }
}
}
},
"responses": {
"201": {
"description": "Successfully created",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/User" }
}
}
},
"400": { "description": "Validation error" },
"401": { "description": "Unauthorized" }
}
}
}
}Features:
- Beautiful web interface for API exploration
- Interactive request/response testing
- Auto-populated request bodies
- Authentication token input
- Schema visualization
- Try-it-out functionality
- Export to Postman/cURL
Usage:
# Just open the file in a browser
open generated/docs/swagger.html
# Or serve with a local server
npx serve generated/docs
# Visit http://localhost:3000/swagger.htmlHuman-readable docs with:
- Overview and authentication instructions
- Entity field tables
- Endpoint listings with methods
- Example requests/responses
- Security indicators (🔒 Protected / 🌐 Public)
- Copy-paste ready cURL examples
Example:
### User
#### Fields
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| email | string | Yes | - |
| name | string | Yes | - |
| age | integer | No | - |
| role | string | No | - |
| isActive | boolean | No | - |
#### Endpoints
**Create User** 🔒 Protected
POST /api/trpc/user.create Content-Type: application/json
{ "email": "example email", "name": "example name" }
- Full Model Schema - Complete entity with all fields, timestamps, soft delete
- Create Input Schema - Required fields for creation
- Update Input Schema - All fields optional for updates
- List Endpoint - Paginated list with search/filter
- Get Endpoint - Retrieve single record by ID
- Create Endpoint - Create new record
- Update Endpoint - Modify existing record
- Remove Endpoint - Delete (or soft delete) record
- Batch Endpoints - createMany, updateMany, removeMany
| Archetype | OpenAPI Type | OpenAPI Format |
|---|---|---|
text() |
string |
- |
text().email() |
string |
email |
text().url() |
string |
uri |
number() |
number |
double |
number().integer() |
integer |
int32 |
boolean() |
boolean |
- |
date() |
string |
date-time |
enumField(...) |
string + enum |
- |
| Archetype | OpenAPI |
|---|---|
.min(5) |
minLength: 5 (text) or minimum: 5 (number) |
.max(100) |
maxLength: 100 (text) or maximum: 100 (number) |
.required() |
Added to required array |
.default('value') |
default: 'value' |
.email() |
format: 'email' |
.url() |
format: 'uri' |
enumField('a', 'b') |
enum: ['a', 'b'] |
| Archetype | OpenAPI |
|---|---|
protected: false |
security: [] (public) |
protected: 'write' |
List/get public, mutations protected |
protected: 'all' |
All operations require bearerAuth |
For 2 entities (User, Post):
generated/docs/
├── openapi.json # 924 lines - Full OpenAPI 3.0 spec
├── swagger.html # 37 lines - Interactive Swagger UI
└── API.md # 136 lines - Markdown documentation
Total: 1,097 lines of documentation from ~40 lines of entity definitions.
Developers can browse the Markdown docs or Swagger UI to understand:
- Available endpoints
- Required fields
- Validation rules
- Authentication requirements
- Response formats
Swagger UI allows:
- Interactive endpoint testing
- Request body auto-completion
- Response inspection
- Authentication token management
OpenAPI spec can generate client SDKs:
# Generate TypeScript client
npx @openapitools/openapi-generator-cli generate \
-i generated/docs/openapi.json \
-g typescript-fetch \
-o sdk/typescript
# Generate Python client
npx @openapitools/openapi-generator-cli generate \
-i generated/docs/openapi.json \
-g python \
-o sdk/pythonUse spec for API contract validation:
# Validate API responses match spec
npm install --save-dev jest-openapiShare openapi.json with:
- Postman (import collection)
- Insomnia (import workspace)
- API Gateway tools
- External developers
- Write API endpoints
- Manually write OpenAPI spec
- Manually update Swagger UI
- Manually write Markdown docs
- Keep all 4 in sync when API changes 😰
- Define entities
- Run
npx archetype generate - Get everything automatically
- When entities change → regenerate → perfect sync ✨
# 1. Add a new field to entity
const User = defineEntity('User', {
fields: {
email: text().required().email(),
name: text().required().min(2).max(100),
phone: text().optional().regex(/^\+?[1-9]\d{1,14}$/), // NEW!
},
})
# 2. Regenerate
npx archetype generate
# 3. Documentation automatically includes:
# - Phone field in User schema
# - Regex validation constraint
# - Updated CreateInput schema
# - Updated examples
# - No manual edits needed!Complete automation is possible:
- ✅ Database schemas
- ✅ API endpoints
- ✅ Validation
- ✅ React hooks
- ✅ Tests
- ✅ API Documentation (NEW!)
Still to come:
- 🔜 Seed data generators
- 🔜 E2E test generators
- 🔜 Admin UI generators
// app/api/docs/route.ts
import { readFileSync } from 'fs'
import { join } from 'path'
export async function GET() {
const html = readFileSync(
join(process.cwd(), 'generated/docs/swagger.html'),
'utf-8'
)
return new Response(html, {
headers: { 'Content-Type': 'text/html' }
})
}// app/api/docs/openapi.json/route.ts
import { readFileSync } from 'fs'
import { join } from 'path'
export async function GET() {
const spec = readFileSync(
join(process.cwd(), 'generated/docs/openapi.json'),
'utf-8'
)
return new Response(spec, {
headers: { 'Content-Type': 'application/json' }
})
}Now visit: http://localhost:3000/api/docs
"Write entities, not infrastructure"
Developers define:
- ✅ Entity structure
- ✅ Field validations
- ✅ Relationships
- ✅ Behaviors
- ✅ Security rules
Archetype generates:
- ✅ Database schemas
- ✅ Type-safe APIs
- ✅ Validation logic
- ✅ React hooks
- ✅ Comprehensive tests
- ✅ Interactive API docs
All perfectly synchronized. All production-ready. All from a single source of truth.
This is the milestone. Everything CAN be generated from compilation. 🎉