| name | nextjs-api-routes |
| description | Apply when building API endpoints in Next.js App Router using Route Handlers. |
| version | 1.0.0 |
| tokens | ~650 |
| confidence | high |
| sources | ["https://nextjs.org/docs/app/building-your-application/routing/route-handlers","https://nextjs.org/docs/app/api-reference/functions/next-request"] |
| last_validated | 2025-01-10T00:00:00.000Z |
| next_review | 2025-01-24T00:00:00.000Z |
| tags | ["nextjs","api","routes","backend"] |
When to Use
Apply when building API endpoints in Next.js App Router using Route Handlers.
Patterns
Pattern 1: Basic Route Handler
import { NextRequest, NextResponse } from 'next/server';
export async function GET(request: NextRequest) {
const users = await db.users.findMany();
return NextResponse.json(users);
}
export async function POST(request: NextRequest) {
const body = await request.json();
const user = await db.users.create({ data: body });
return NextResponse.json(user, { status: 201 });
}
Pattern 2: Dynamic Route Parameters
interface RouteParams {
: <{ : }>;
}
() {
{ id } = params;
user = db..({ : { id } });
(!user) {
.({ : }, { : });
}
.(user);
}
() {
{ id } = params;
db..({ : { id } });
(, { : });
}