Skip to content

/api-gen

Generate API endpoints, documentation, or client code from specifications. Accelerates API development by creating boilerplate code, OpenAPI specs, and client SDKs.

Terminal window
/api-gen [resource-name-or-spec]
  • resource-name-or-spec:
    • Resource name: User, Order, Product - Generate CRUD API
    • OpenAPI spec path: openapi.yaml - Generate from specification
    • Description: "User management API" - Generate from description
  1. Identify resource properties

    • Field names and types
    • Required vs optional
    • Validation rules
  2. Define relationships

    • One-to-many
    • Many-to-many
    • Foreign keys
  3. Determine operations

    • CRUD operations needed
    • Custom endpoints
    • Business logic
  1. Create model/schema

    • Database model
    • Validation schema
    • Type definitions
  2. Create routes/endpoints

    • HTTP methods
    • Path parameters
    • Request/response handling
  3. Add validation

    • Input validation
    • Type checking
    • Error handling
  4. Generate tests

    • Endpoint tests
    • Validation tests
    • Error case tests
  1. Create OpenAPI spec

    • Endpoint descriptions
    • Request/response schemas
    • Authentication
  2. Add examples

    • Request examples
    • Response examples
    • cURL commands
  3. Document errors

    • Error codes
    • Error messages
    • Resolution steps
Terminal window
/api-gen User

Creates:

  • src/models/user.ts - Data model
  • src/routes/user.ts - API routes
  • src/controllers/user.ts - Business logic
  • tests/user.test.ts - Test suite
  • docs/api/user.md - API documentation
Terminal window
/api-gen openapi.yaml

Generates code matching the specification.

Terminal window
/api-gen "Product catalog API with categories and tags"

Creates API based on natural language description.

## API Generated: User Management
### Endpoints Created
| Method | Path | Description |
|--------|------|-------------|
| GET | /api/users | List all users |
| POST | /api/users | Create new user |
| GET | /api/users/:id | Get user by ID |
| PUT | /api/users/:id | Update user |
| DELETE | /api/users/:id | Delete user |
| GET | /api/users/:id/posts | Get user's posts |
### Files Created
#### Model
- `src/models/user.ts`
```typescript
export interface User {
id: string;
email: string;
name: string;
createdAt: Date;
updatedAt: Date;
}
export const UserSchema = z.object({
email: z.string().email(),
name: z.string().min(1).max(100),
});
  • src/routes/user.ts
    router.get('/users', listUsers);
    router.post('/users', validateBody(UserSchema), createUser);
    router.get('/users/:id', getUser);
    router.put('/users/:id', validateBody(UserSchema), updateUser);
    router.delete('/users/:id', deleteUser);
  • src/controllers/user.ts
    export async function createUser(req, res) {
    const user = await db.user.create({
    data: req.body,
    });
    res.status(201).json(user);
    }
    // ... other handlers
  • tests/user.test.ts
    describe('User API', () => {
    it('should create user', async () => {
    const res = await request(app)
    .post('/api/users')
    .send({ email: 'test@example.com', name: 'Test' });
    expect(res.status).toBe(201);
    expect(res.body).toHaveProperty('id');
    });
    });
  • docs/api/user.md - Full API documentation

Generated openapi.yaml:

openapi: 3.0.0
info:
title: User API
version: 1.0.0
paths:
/api/users:
get:
summary: List all users
responses:
'200':
description: Success
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
post:
summary: Create new user
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreateUser'
responses:
'201':
description: Created
  1. ✅ API endpoints generated
  2. 🔧 Add authentication middleware
  3. 🔧 Implement business validation
  4. 🔧 Add pagination to list endpoint
  5. 📝 Review and customize as needed
## Generation Modes
### CRUD Generation
```bash
/api-gen Product

Full Create, Read, Update, Delete operations

Terminal window
/api-gen swagger.json

Generate from existing OpenAPI/Swagger spec

Terminal window
/api-gen --client openapi.yaml

Generate TypeScript/Python client SDK

Terminal window
/api-gen --docs-only api/

Generate documentation for existing endpoints

FlagDescription
--framework=[express|fastapi|nestjs]Target framework
--db=[postgres|mongodb|prisma]Database/ORM
--authInclude authentication
--clientGenerate client SDK
--docs-onlyDocumentation only
--test-framework=[jest|vitest|pytest]Testing framework
Terminal window
/api-gen Order \
--framework=express \
--db=postgres \
--auth
Terminal window
/api-gen Product \
--framework=fastapi \
--db=mongodb
Terminal window
/api-gen --client openapi.yaml

Creates TypeScript client:

import { UserAPI } from './generated/client';
const api = new UserAPI({ baseUrl: 'http://localhost:3000' });
const users = await api.getUsers();
const user = await api.createUser({
email: 'test@example.com',
name: 'Test User'
});

When generating from resource name, you’ll be asked:

### Define Resource: User
**Fields:**
1. email (string, required, unique)
2. name (string, required)
3. role (enum: user|admin, optional, default: user)
4. createdAt (datetime, auto)
**Relationships:**
- Has many: Post
- Belongs to: Organization (optional)
**Custom Endpoints:**
- POST /users/:id/verify-email
- POST /users/:id/reset-password
**Validation:**
- Email must be valid format
- Name: 1-100 characters
- Password: min 8 characters
Proceed with generation? (y/n)
/api/orders:
post:
summary: Create new order
tags:
- Orders
security:
- bearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreateOrder'
examples:
basic:
value:
items:
- productId: "123"
quantity: 2
shippingAddress:
street: "123 Main St"
city: "New York"
responses:
'201':
description: Order created
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
'400':
description: Invalid input
'401':
description: Unauthorized
  1. Start with Design: Define API before generating
  2. Review Generated Code: Don’t blindly accept all generated code
  3. Customize: Generated code is a starting point
  4. Add Business Logic: Implement domain-specific validation
  5. Security: Add authentication and authorization
  6. Testing: Extend generated tests with edge cases
Terminal window
# Quickly scaffold multiple resources
/api-gen User
/api-gen Post
/api-gen Comment
Terminal window
# Design spec, then generate
# 1. Create openapi.yaml
# 2. Generate code
/api-gen openapi.yaml
Terminal window
# Generate client for API consumers
/api-gen --client openapi.yaml
Terminal window
# Document existing API
/api-gen --docs-only src/api/
  • /doc - Generate documentation
  • /feature - Full feature development
  • /test - Generate tests
  • /review - Review generated code