Route Handlers let you create custom HTTP endpoints for a route using the Web Request and Response APIs.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/vercel/next.js/llms.txt
Use this file to discover all available pages before exploring further.
route.ts
HTTP methods
The following HTTP methods are supported:GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS.
route.ts
If
OPTIONS is not defined, Next.js automatically implements it and sets the Allow response header based on the other defined methods.Parameters
A NextRequest object, which extends the Web
Request API with convenience methods for cookies and a parsed nextUrl object.route.ts
An optional second parameter containing route context.
context.params — a promise that resolves to the dynamic route parameters for the current route.app/dashboard/[team]/route.ts
| Route | 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'] }> |
TypeScript helper
Use the globalRouteContext helper for strongly typed route params:
app/users/[id]/route.ts
Types are generated during
next dev, next build, or next typegen. RouteContext is globally available and does not need to be imported.Examples
Reading and setting cookies
route.ts
Reading request headers
route.ts
Reading the request body
app/items/route.ts
Reading form data
app/items/route.ts
URL query parameters
app/api/search/route.ts
Dynamic route segments
app/items/[slug]/route.ts
Redirects
app/api/route.ts
Streaming responses
app/api/route.ts
CORS headers
app/api/route.ts
Webhooks
app/api/webhook/route.ts
Non-UI responses
app/rss.xml/route.ts
Revalidating cached data
app/posts/route.ts
Version history
| Version | Changes |
|---|---|
v15.0.0-RC | context.params is now a promise |
v15.0.0-RC | Default caching for GET handlers changed from static to dynamic |
v13.2.0 | Route Handlers introduced |
