Backend-for-frontend endpoints
Create JSON endpoints for browser, mobile, or server clients.
Next.js 16+ · Directive
Route Handlers define custom HTTP endpoints in the App Router with route.ts or route.js files. Export functions named for supported HTTP methods and return standard web Response objects.
A route.ts or route.js file defines HTTP handlers for a URL inside the app directory. Export functions named for supported HTTP methods; each receives the web Request and returns a web Response, letting the endpoint implement server-side behavior without a page component.
export async function GET(request: Request): Promise<Response> {}| Name | Type | Description |
|---|---|---|
request | Request | The incoming web request, including URL, headers, and body. |
A web Response object.
export async function GET(request: Request) {
const name = new URL(request.url).searchParams.get("name") ?? "world"
return Response.json({ message: `Hello, ${name}!` })
}Save this as app/api/hello/route.ts, then request /api/hello?name=Next.js.
Create JSON endpoints for browser, mobile, or server clients.
Receive data from forms or other clients through POST requests.
Expose server-side logic at a URL without rendering a page.
A Route Handler is a custom request handler defined by a route.js or route.ts file in the app directory. It uses the web Request and Response APIs and is the App Router counterpart to an API Route.
Follow-up: Where can a handler be placed? It can be nested anywhere in app, provided it does not share the same route segment as page.js.
Export an async function whose name is an HTTP method, such as GET, POST, PUT, PATCH, DELETE, HEAD, or OPTIONS. The function can inspect the Request and return a Response.
Follow-up: How would you support multiple verbs? Export one appropriately named handler for each method the endpoint needs.
Route Handlers are the lowest-level routing primitive and can be nested in the app directory similarly to page.js and layout.js. Their URL comes from the directory structure containing the route file.
Follow-up: What replaces a Pages Router API endpoint in the App Router? A route.js or route.ts Route Handler under app.
RouteContextNextResponsepage.jslayout.jsAPI Routes