Back
✍️Promptclaude_code

REST API Endpoint Generator

Generate complete REST API endpoints with validation, error handling, and OpenAPI docs. Works with Express, Fastify, or Next.js Route Handlers.

by APIBuilder·40 days ago·
RESTAPIbackendExpressNext.js
Generate a complete REST API endpoint with the following requirements:

## Input
- Framework: [express / fastify / next.js-route-handler]
- HTTP Method: [GET/POST/PUT/DELETE]
- Path: [/api/resource]
- Database: [prisma / drizzle / raw-sql]
- Auth: [none / bearer / api-key / session]

## Output Requirements

For each endpoint, generate:

1. **Route Handler** with:
   - Input validation using Zod schema
   - Proper error handling with typed errors
   - Request/response TypeScript types
   - Pagination support for list endpoints
   - Field selection (sparse fieldsets)
   - Filtering and sorting support

2. **Validation Schema**:
```typescript
import { z } from "zod"

export const CreateItemSchema = z.object({
  name: z.string().min(1).max(100),
  description: z.string().max(500).optional(),
  tags: z.array(z.string()).max(10).default([]),
})
```

3. **Error Responses**: Consistent error format with proper HTTP status codes

4. **OpenAPI Documentation**: Auto-generated from the Zod schemas

## Example for: POST /api/users

```typescript
import { z } from "zod"
import { prisma } from "@/lib/db"
import { hash } from "bcryptjs"

const CreateUserSchema = z.object({
  email: z.string().email(),
  name: z.string().min(2).max(100),
  password: z.string().min(8),
})

export async function POST(request: Request) {
  try {
    const body = await request.json()
    const data = CreateUserSchema.parse(body)
    
    const existing = await prisma.user.findUnique({ where: { email: data.email } })
    if (existing) {
      return Response.json({ error: "Email already registered" }, { status: 409 })
    }
    
    const hashedPassword = await hash(data.password, 12)
    const user = await prisma.user.create({
      data: { email: data.email, name: data.name, password: hashedPassword },
      select: { id: true, email: true, name: true, createdAt: true },
    })
    
    return Response.json({ data: user }, { status: 201 })
  } catch (error) {
    if (error instanceof z.ZodError) {
      return Response.json({ error: "Validation failed", details: error.errors }, { status: 400 })
    }
    console.error("Create user error:", error)
    return Response.json({ error: "Internal server error" }, { status: 500 })
  }
}
```

Now generate an endpoint for: [DESCRIBE_YOUR_ENDPOINT]