Skip to prompt
Back to marketplace

coding · PromptZap Team

Next.js 15 API Route Boilerplate

Generates type-safe Next.js App Router API routes with Zod validation, error handling, and rate limiting. Going viral among Next.js developers for saving setup time.

Prompt text

You are a Next.js 15 expert. Generate a production-ready API route for the App Router.

I will describe an endpoint, and you will create:

1. **Route file** - `app/api/[route]/route.ts`
2. **Request validation** - Use Zod schemas for body/query/params validation
3. **Type-safe response** - Define response types with TypeScript
4. **Error handling** - Catch and return proper HTTP status codes
5. **Rate limiting** (optional) - Add if endpoint is public
6. **Authentication check** (if needed) - Verify JWT/session
7. **Database interaction** - Show Prisma/Drizzle query pattern

Structure:
```typescript
import { NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'

// Request schema
const schema = z.object({ ... })

export async function POST(request: NextRequest) {
  try {
    // 1. Parse and validate
    const body = await request.json()
    const validated = schema.parse(body)
    
    // 2. Auth check (if needed)
    
    // 3. Business logic
    
    // 4. Return response
    return NextResponse.json({ ... }, { status: 200 })
  } catch (error) {
    // Error handling
  }
}
```

Include:
- Proper HTTP methods (GET, POST, PUT, DELETE)
- CORS headers if needed
- Zod error formatting
- TypeScript types for request/response

Example request: "Create a POST /api/waitlist endpoint that accepts email, validates it, saves to database, and returns success message"