Next.js Route Handlers
Learn how to build custom HTTP request handlers and REST API endpoints in the Next.js App Router using route.ts files.
Overview
Route Handlers allow you to create custom request handlers for a given route using the Web Request and Response APIs. Route Handlers are defined in a route.ts (or route.js) file inside the app directory.
Supported HTTP Methods
Next.js supports standard HTTP methods: GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS. If an unsupported method is called, Next.js automatically returns a 405 Method Not Allowed response.
// app/api/items/route.ts
import { NextResponse } from 'next/server';
export async function GET() {
const data = [{ id: 1, name: 'Sample Item' }];
return NextResponse.json(data);
}
export async function POST(request: Request) {
const body = await request.json();
return NextResponse.json({ success: true, item: body }, { status: 201 });
}Reading Request Headers and Cookies
Next.js extends the standard Web Request with NextRequest, providing helper methods for reading cookies and search parameters easily:
// app/api/user/route.ts
import { NextRequest, NextResponse } from 'next/server';
export async function GET(request: NextRequest) {
// Read search parameters (?search=test)
const searchParams = request.nextUrl.searchParams;
const query = searchParams.get('search');
// Read request headers
const authHeader = request.headers.get('authorization');
// Read cookies
const token = request.cookies.get('session-token');
return NextResponse.json({
query,
hasAuth: Boolean(authHeader),
hasSession: Boolean(token),
});
}Dynamic Route Handlers
Route Handlers can be placed inside dynamic folder segments (e.g. app/api/posts/[id]/route.ts).
[!NOTE] In modern Next.js (version 15+ and 16+), dynamic
paramsin Route Handlers is an asynchronous Promise that must be awaited.
// app/api/posts/[id]/route.ts
import { NextResponse } from 'next/server';
interface RouteContext {
params: Promise<{ id: string }>;
}
export async function GET(
_request: Request,
context: RouteContext
) {
const { id } = await context.params;
return NextResponse.json({
id,
title: `Post details for ${id}`,
});
}
export async function DELETE(
_request: Request,
context: RouteContext
) {
const { id } = await context.params;
// Perform deletion logic...
return new NextResponse(null, { status: 204 });
}Caching Behavior
- Static Caching:
GEThandlers that do not accessrequestheaders/cookies or search parameters can be statically cached by default during build. - Dynamic Execution: Handlers that access dynamic request information (
request.headers,request.cookies,request.nextUrl.searchParams) or use methods other thanGETare evaluated dynamically on each incoming request.
Best Practices
- Keep Route Handlers for External APIs & Webhooks: If you are handling UI form submissions within the same Next.js application, prefer Server Actions over Route Handlers.
- Do Not Nest
page.tsxandroute.tsat the Same Path: A route segment cannot have both apage.tsxand aroute.tsfile. - Always Validate Input: Validate JSON payloads using libraries like Zod before database operations.
Not Found (404) Pages in Next.js
Custom 404 pages in Next.js: not-found.tsx conventions and programmatically triggering 404s using notFound() function.
Next.js Forms & Mutations
Handle form submissions, optimistic updates, and validation in Next.js using Server Actions, useActionState, and useFormStatus.