Route Handlers define HTTP endpoints in route.ts files—App Router’s replacement for Pages Router API routes. They run on the server and can return JSON, streams, webhooks, and more.
Basic GET handler
app/api/posts/route.ts
import { NextResponse } from 'next/server';
export async function GET() {
const posts = await db.post.findMany();
return NextResponse.json(posts);
}
export async function POST(request: Request) {
const body = await request.json();
const created = await db.post.create({ data: body });
return NextResponse.json(created, { status: 201 });
}
When to use
- REST/JSON APIs for mobile apps or external consumers
- Webhooks (Stripe, GitHub)
- Operations that must not ship logic to the client
Server Actions vs Route Handlers
Prefer Server Actions for form mutations from your own UI. Use Route Handlers when you need standard HTTP semantics or non-React clients.
Self-check
- What file name defines an API route in App Router?
- When would you pick a Route Handler over a Server Action?
Interview prep
- Where do Route Handlers live?
route.tsin anapp/segment—export GET, POST, etc. returningNextResponse.