😀How to Query Parameters in Next.js API GET Request | Next.js 14 | REST API (ok)
https://www.youtube.com/watch?v=qiF3Ketkhas
Last updated
Was this helpful?
https://www.youtube.com/watch?v=qiF3Ketkhas
Last updated
Was this helpful?
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
Hoặc
Hoặc
Hoặc
app\api\todos\route.ts
Route Handlers allow you to create custom request handlers for a given route using the Web Request and Response APIs.
Good to know: Route Handlers are only available inside the
app
directory. They are the equivalent of API Routes inside thepages
directory meaning you do not need to use API Routes and Route Handlers together.
Route Handlers are defined in a route.js|ts
file inside the app
directory:
app/api/route.tsTypeScriptJavaScriptTypeScript
Route Handlers can be nested anywhere inside the app
directory, similar to page.js
and layout.js
. But there cannot be a route.js
file at the same route segment level as page.js
.
The following HTTP methods are supported: GET
, POST
, PUT
, PATCH
, DELETE
, HEAD
, and OPTIONS
. If an unsupported method is called, Next.js will return a 405 Method Not Allowed
response.
In addition to supporting the native Request and Response APIs, Next.js extends them with NextRequest
and NextResponse
to provide convenient helpers for advanced use cases.
Route Handlers are not cached by default. You can, however, opt into caching for GET
methods. Other supported HTTP methods are not cached. To cache a GET
method, use a route config option such as export const dynamic = 'force-static'
in your Route Handler file.
app/items/route.tsTypeScriptJavaScriptTypeScript
Good to know: Other supported HTTP methods are not cached, even if they are placed alongside a
GET
method that is cached, in the same file.
Special Route Handlers like sitemap.ts
, opengraph-image.tsx
, and icon.tsx
, and other metadata files remain static by default unless they use Dynamic APIs or dynamic config options.
You can consider a route
the lowest level routing primitive.
They do not participate in layouts or client-side navigations like page
.
There cannot be a route.js
file at the same route as page.js
.
app/page.js
app/route.js
Conflict
app/page.js
app/api/route.js
Valid
app/[user]/page.js
app/api/route.js
Valid
Each route.js
or page.js
file takes over all HTTP verbs for that route.
app/page.tsTypeScriptJavaScriptTypeScript
The following examples show how to combine Route Handlers with other Next.js APIs and features.
You can revalidate cached data using Incremental Static Regeneration (ISR):
app/posts/route.tsTypeScriptJavaScriptTypeScript
You can read or set cookies with cookies
from next/headers
. This server function can be called directly in a Route Handler, or nested inside of another function.
Alternatively, you can return a new Response
using the Set-Cookie
header.
app/api/route.tsTypeScriptJavaScriptTypeScript
You can also use the underlying Web APIs to read cookies from the request (NextRequest
):
app/api/route.tsTypeScriptJavaScriptTypeScript
You can read headers with headers
from next/headers
. This server function can be called directly in a Route Handler, or nested inside of another function.
This headers
instance is read-only. To set headers, you need to return a new Response
with new headers
.
app/api/route.tsTypeScriptJavaScriptTypeScript
You can also use the underlying Web APIs to read headers from the request (NextRequest
):
app/api/route.tsTypeScriptJavaScriptTypeScript
app/api/route.tsTypeScriptJavaScriptTypeScript
Route Handlers can use Dynamic Segments to create request handlers from dynamic data.
app/items/[slug]/route.tsTypeScriptJavaScriptTypeScript
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' }>
The request object passed to the Route Handler is a NextRequest
instance, which has some additional convenience methods, including for more easily handling query parameters.
app/api/search/route.tsTypeScriptJavaScriptTypeScript
Streaming is commonly used in combination with Large Language Models (LLMs), such as OpenAI, for AI-generated content. Learn more about the AI SDK.
app/api/chat/route.tsTypeScriptJavaScriptTypeScript
These abstractions use the Web APIs to create a stream. You can also use the underlying Web APIs directly.
app/api/route.tsTypeScriptJavaScriptTypeScript
You can read the Request
body using the standard Web API methods:
app/items/route.tsTypeScriptJavaScriptTypeScript
You can read the FormData
using the request.formData()
function:
app/items/route.tsTypeScriptJavaScriptTypeScript
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
).
You can set CORS headers for a specific Route Handler using the standard Web API methods:
app/api/route.tsTypeScriptJavaScriptTypeScript
Good to know:
To add CORS headers to multiple Route Handlers, you can use Middleware or the
next.config.js
file.Alternatively, see our CORS example package.
You can use a Route Handler to receive webhooks from third-party services:
app/api/route.tsTypeScriptJavaScriptTypeScript
Notably, unlike API Routes with the Pages Router, you do not need to use bodyParser
to use any additional configuration.
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.
app/rss.xml/route.tsTypeScriptJavaScriptTypeScript
Route Handlers use the same route segment configuration as pages and layouts.
app/items/route.tsTypeScriptJavaScriptTypeScript
See the API reference for more details.
Learn more about the route.js file.route.jsAPI reference for the route.js special file.