route.js
API reference for the route.js special file.
Route Handlers allow you to create custom request handlers for a given route using the Web Request and Response APIs.
Reference
HTTP Methods
A route file allows you to create custom request handlers for a given route. The following HTTP methods are supported: GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS.
Parameters
request (optional)
The request object is a NextRequest object, which is an extension of the Web Request API. NextRequest gives you further control over the incoming request, including easily accessing cookies and an extended, parsed, URL object nextUrl.
context (optional)
params: a promise that resolves to an object containing the dynamic route parameters for the current route.
| Example | URL | params |
|---|---|---|
app/dashboard/[team]/route.js | /dashboard/1 | Promise<{ team: '1' }> |
app/shop/[tag]/[item]/route.js | /shop/1/2 | Promise<{ tag: '1', item: '2' }> |
app/blog/[...slug]/route.js | /blog/1/2 | Promise<{ slug: ['1', '2'] }> |
Route Context Helper
You can type the Route Handler context using RouteContext to get strongly typed params from a route literal. RouteContext is a globally available helper.
Good to know
- Types are generated during
next dev,next buildornext typegen. - After type generation, the
RouteContexthelper is globally available. It doesn't need to be imported.
Examples
Cookies
You can read or set cookies with cookies from next/headers.
Alternatively, you can return a new Response using the Set-Cookie header.
You can also use the underlying Web APIs to read cookies from the request (NextRequest):
Headers
You can read headers with headers from next/headers.
This headers instance is read-only. To set headers, you need to return a new Response with new headers.
You can also use the underlying Web APIs to read headers from the request (NextRequest):
Revalidating Cached Data
You can revalidate cached data using the revalidate route segment config option.
Redirects
Dynamic Route Segments
Route Handlers can use Dynamic Segments to create request handlers from dynamic data.
| Route | Example URL | params |
|---|---|---|
app/items/[slug]/route.js | /items/a | Promise<{ slug: 'a' }> |
app/items/[slug]/route.js | /items/b | Promise<{ slug: 'b' }> |
app/items/[slug]/route.js | /items/c | Promise<{ slug: 'c' }> |
URL Query Parameters
The request object passed to the Route Handler is a NextRequest instance, which includes some additional convenience methods, such as those for more easily handling query parameters.
Streaming
Streaming is commonly used in combination with Large Language Models (LLMs), such as OpenAI, for AI-generated content. Learn more about the AI SDK.
These abstractions use the Web APIs to create a stream. You can also use the underlying Web APIs directly.
Request Body
You can read the Request body using the standard Web API methods:
Request Body FormData
You can read the FormData using the request.formData() function:
Since formData data are all strings, you may want to use zod-form-data to validate the request and retrieve data in the format you prefer (e.g. number).
CORS
You can set CORS headers for a specific Route Handler using the standard Web API methods:
Good to know:
- To add CORS headers to multiple Route Handlers, you can use Proxy or the
next.config.jsfile.
Webhooks
You can use a Route Handler to receive webhooks from third-party services:
Notably, unlike API Routes with the Pages Router, you do not need to use bodyParser to use any additional configuration.
Non-UI Responses
You can use Route Handlers to return non-UI content. Note that sitemap.xml, robots.txt, app icons, and open graph images all have built-in support.
Segment Config Options
Route Handlers use the same route segment configuration as pages and layouts.
See the API reference for more details.
Version History
| Version | Changes |
|---|---|
v15.0.0-RC | context.params is now a promise. A codemod is available |
v15.0.0-RC | The default caching for GET handlers was changed from static to dynamic |
v13.2.0 | Route Handlers are introduced. |