Route Handlers
Files in a Next.js app, named route.ts, that answer HTTP requests for a path with functions named after methods such as GET and POST.
In more detail
They are Next.js's way of building API endpoints next to the pages. Their request bodies come from outside and need validating like any other input.
Example
export async function GET() {
return Response.json({ status: 'ok', time: new Date().toISOString() });
}Related terms
- NextRequest — Next.js' extension of the Web Request object, adding a cookies helper and nextUrl, a parsed URL with Next.js routing details.
- NextResponse — Next.js' extension of the Web Response object, with helpers for JSON responses, cookies, redirects and rewrites.
- HTTP method — The verb at the start of a web request that says what the client wants, such as GET to read or POST to create.
- API routes — The Pages Router way of writing back-end endpoints: files in pages/api that export a handler taking req and res.
- Next.js — A React framework for building web applications that can render pages on the server and supports React Server Components.
- Server Functions — Async functions marked with "use server" that run on the server but can be called from client components, for example as a form action.
- endpoint — An address that a program sends requests to, such as a web path like /health or a network socket.
Taught in
- Not Found Handling: not-found.tsx & Programmatic Exits — Next.js 15 & Modern App Router Architecture
- Revalidation Strategies: revalidatePath & Cache Life — Next.js 15 & Modern App Router Architecture
- Route Handlers: RESTful Endpoints with NextRequest — Next.js 15 & Modern App Router Architecture
- Streaming Endpoints: Server-Sent Events & NDJSON Feeds — Next.js 15 & Modern App Router Architecture
- Edge Middleware: Auth Verification & Route Matchers — Next.js 15 & Modern App Router Architecture
- Runtime Environments: Node.js vs Edge Runtime Choice — Next.js 15 & Modern App Router Architecture
- The Spine Artifact: Generating Typed Models From OpenAPI — Production TypeScript