API Scaffolder
Generates production-ready REST or GraphQL API boilerplate from an OpenAPI specification or a natural-language description of resources and operations. Output includes routes, controllers, models, validation schemas, and error handling.
When to Use
- User provides an OpenAPI 3.x spec and wants implementation scaffolded
- User describes API resources ("I need CRUD endpoints for users and posts")
- Starting a new microservice and need a consistent structure
- User asks to add a new resource to an existing API following current conventions
- Generating client SDKs or server stubs from a spec
Process
Identify the target framework from context or ask:
- Node.js: Express, Fastify, NestJS, Hono
- Python: FastAPI, Flask, Django REST Framework
- Go: net/http, Gin, Echo, Chi
- Java: Spring Boot
- Ruby: Rails API mode, Sinatra
Parse the input — OpenAPI spec or natural-language description:
- For OpenAPI: extract paths, methods, request/response schemas, security schemes
- For descriptions: infer resources, standard CRUD operations, and field types
Design the file structure following the detected or standard project layout:
src/
routes/ # route definitions
controllers/ # request handlers
services/ # business logic
models/ # DB models / entities
validators/ # request validation schemas
middleware/ # auth, logging, error handling
Generate each layer:
Routes — map HTTP methods + paths to controller functions:
GET /users → UserController.list
POST /users → UserController.create
GET /users/:id → UserController.getById
PUT /users/:id → UserController.update
DELETE /users/:id → UserController.delete
Controllers — thin handlers: validate input → call service → return response:
- Extract and validate path/query params and request body
- Call the appropriate service method
- Map service result to HTTP response (201 for create, 204 for delete, etc.)
- Catch and forward errors to the error middleware
Services — business logic, decoupled from HTTP:
- Implement actual CRUD operations against the model
- Throw typed errors (NotFoundError, ConflictError) rather than HTTP status codes
Models — database schema/entity definitions:
- Include all fields with types, constraints, and defaults
- Add timestamps (
createdAt, updatedAt) by default
- Define associations/relations if described
Validators — request body/param schemas:
- Use Zod, Joi, Pydantic, class-validator, or idiomatic framework validation
- Validate types, required fields, string lengths, enum values, formats
Generate error handling middleware that maps typed errors to HTTP status codes.
Add basic authentication middleware placeholder (or full implementation if auth type is specified).
Include a router index that mounts all generated routes with appropriate prefixes.
Output Format
Produce a set of files with clear filenames. For each file, show the complete content:
### src/routes/users.routes.ts
```ts
import { Router } from 'express';
import { UserController } from '../controllers/users.controller';
import { validateBody } from '../middleware/validate';
import { CreateUserSchema, UpdateUserSchema } from '../validators/users.schema';
const router = Router();
router.get('/', UserController.list);
router.post('/', validateBody(CreateUserSchema), UserController.create);
router.get('/:id', UserController.getById);
router.put('/:id', validateBody(UpdateUserSchema), UserController.update);
router.delete('/:id', UserController.delete);
export default router;
src/controllers/users.controller.ts
import { Request, Response, NextFunction } from 'express';
import { UserService } from '../services/users.service';
export class UserController {
static async list(req: Request, res: Response, next: NextFunction) {
try {
const users = await UserService.findAll();
res.json(users);
} catch (err) { next(err); }
}
// ... create, getById, update, delete
}
## Examples
### Example Input
Scaffold a REST API for a blog platform. Resources:
- Post: title (string, required), body (text, required), authorId (uuid), published (bool, default false)
- Comment: postId (uuid), authorId (uuid), content (string, 1-500 chars)
Framework: FastAPI (Python)
### Example Output (summary)
Files generated:
- app/routes/posts.py — GET /posts, POST /posts, GET/PUT/DELETE /posts/{id}
- app/routes/comments.py — GET /posts/{id}/comments, POST /posts/{id}/comments
- app/controllers/posts.py — list_posts, create_post, get_post, update_post, delete_post
- app/services/posts.py — business logic + DB queries
- app/models/post.py — SQLAlchemy model with id, title, body, author_id, published, created_at
- app/schemas/post.py — Pydantic: PostCreate, PostUpdate, PostResponse
- app/main.py — FastAPI app with routers mounted at /api/v1
## Boundaries
- Do NOT generate database migration files — note that migrations must be created separately using the ORM CLI.
- Do NOT hardcode database credentials or secrets — use environment variables with `.env` placeholders.
- Do NOT implement authentication logic unless explicitly requested — add a middleware stub with a TODO comment.
- Do NOT generate frontend code from this skill — use the appropriate frontend scaffolding approach.
- If the OpenAPI spec contains conflicting schemas, flag the conflict and use the most restrictive interpretation.
- Generate only the layers explicitly requested; do not add layers the user did not ask for unless they are minimal and required for the code to function.
1---2name: api-scaffolder3description: Generates REST or GraphQL API boilerplate — controllers, routes, models, and validation — from an OpenAPI spec or description. Invoke when asked to scaffold an API, generate CRUD endpoints, create route handlers, or bootstrap a new API service.4---56# API Scaffolder78Generates production-ready REST or GraphQL API boilerplate from an OpenAPI specification or a natural-language description of resources and operations. Output includes routes, controllers, models, validation schemas, and error handling.910## When to Use1112- User provides an OpenAPI 3.x spec and wants implementation scaffolded13- User describes API resources ("I need CRUD endpoints for users and posts")14- Starting a new microservice and need a consistent structure15- User asks to add a new resource to an existing API following current conventions16- Generating client SDKs or server stubs from a spec1718## Process19201. **Identify the target framework** from context or ask:21 - Node.js: Express, Fastify, NestJS, Hono22 - Python: FastAPI, Flask, Django REST Framework23 - Go: net/http, Gin, Echo, Chi24 - Java: Spring Boot25 - Ruby: Rails API mode, Sinatra26272. **Parse the input** — OpenAPI spec or natural-language description:28 - For OpenAPI: extract paths, methods, request/response schemas, security schemes29 - For descriptions: infer resources, standard CRUD operations, and field types30313. **Design the file structure** following the detected or standard project layout:32 ```33 src/34 routes/ # route definitions35 controllers/ # request handlers36 services/ # business logic37 models/ # DB models / entities38 validators/ # request validation schemas39 middleware/ # auth, logging, error handling40 ```41424. **Generate each layer**:4344 **Routes** — map HTTP methods + paths to controller functions:45 ```46 GET /users → UserController.list47 POST /users → UserController.create48 GET /users/:id → UserController.getById49 PUT /users/:id → UserController.update50 DELETE /users/:id → UserController.delete51 ```5253 **Controllers** — thin handlers: validate input → call service → return response:54 - Extract and validate path/query params and request body55 - Call the appropriate service method56 - Map service result to HTTP response (201 for create, 204 for delete, etc.)57 - Catch and forward errors to the error middleware5859 **Services** — business logic, decoupled from HTTP:60 - Implement actual CRUD operations against the model61 - Throw typed errors (NotFoundError, ConflictError) rather than HTTP status codes6263 **Models** — database schema/entity definitions:64 - Include all fields with types, constraints, and defaults65 - Add timestamps (`createdAt`, `updatedAt`) by default66 - Define associations/relations if described6768 **Validators** — request body/param schemas:69 - Use Zod, Joi, Pydantic, class-validator, or idiomatic framework validation70 - Validate types, required fields, string lengths, enum values, formats71725. **Generate error handling middleware** that maps typed errors to HTTP status codes.73746. **Add basic authentication middleware** placeholder (or full implementation if auth type is specified).75767. **Include a router index** that mounts all generated routes with appropriate prefixes.7778## Output Format7980Produce a set of files with clear filenames. For each file, show the complete content:8182```83### src/routes/users.routes.ts84```ts85import { Router } from 'express';86import { UserController } from '../controllers/users.controller';87import { validateBody } from '../middleware/validate';88import { CreateUserSchema, UpdateUserSchema } from '../validators/users.schema';8990const router = Router();9192router.get('/', UserController.list);93router.post('/', validateBody(CreateUserSchema), UserController.create);94router.get('/:id', UserController.getById);95router.put('/:id', validateBody(UpdateUserSchema), UserController.update);96router.delete('/:id', UserController.delete);9798export default router;99```100101### src/controllers/users.controller.ts102```ts103import { Request, Response, NextFunction } from 'express';104import { UserService } from '../services/users.service';105106export class UserController {107 static async list(req: Request, res: Response, next: NextFunction) {108 try {109 const users = await UserService.findAll();110 res.json(users);111 } catch (err) { next(err); }112 }113 // ... create, getById, update, delete114}115```116```117118## Examples119120### Example Input121```122Scaffold a REST API for a blog platform. Resources:123- Post: title (string, required), body (text, required), authorId (uuid), published (bool, default false)124- Comment: postId (uuid), authorId (uuid), content (string, 1-500 chars)125Framework: FastAPI (Python)126```127128### Example Output (summary)129```130Files generated:131- app/routes/posts.py — GET /posts, POST /posts, GET/PUT/DELETE /posts/{id}132- app/routes/comments.py — GET /posts/{id}/comments, POST /posts/{id}/comments133- app/controllers/posts.py — list_posts, create_post, get_post, update_post, delete_post134- app/services/posts.py — business logic + DB queries135- app/models/post.py — SQLAlchemy model with id, title, body, author_id, published, created_at136- app/schemas/post.py — Pydantic: PostCreate, PostUpdate, PostResponse137- app/main.py — FastAPI app with routers mounted at /api/v1138```139140## Boundaries141142- Do NOT generate database migration files — note that migrations must be created separately using the ORM CLI.143- Do NOT hardcode database credentials or secrets — use environment variables with `.env` placeholders.144- Do NOT implement authentication logic unless explicitly requested — add a middleware stub with a TODO comment.145- Do NOT generate frontend code from this skill — use the appropriate frontend scaffolding approach.146- If the OpenAPI spec contains conflicting schemas, flag the conflict and use the most restrictive interpretation.147- Generate only the layers explicitly requested; do not add layers the user did not ask for unless they are minimal and required for the code to function.