Read or modify request cookies
A Route Handler needs to inspect a session, preference, or feature cookie before producing a response.
Next.js 16+ · Class
NextRequest extends the Web Request API with Next.js conveniences for incoming requests. Its cookies API handles request cookies, while nextUrl provides an extended parsed URL.
NextRequest is Next.js's extension of the Web Request API for incoming requests. It preserves standard Request capabilities while adding request-cookie access and nextUrl, which extends URL with Next.js-specific properties and convenient parsed URL handling.
import type { NextRequest } from 'next/server'| Name | Type | Description |
|---|---|---|
request | NextRequest | Incoming HTTP request represented by the NextRequest instance. |
No class return value; NextRequest is an object representing an incoming HTTP request.
import type { NextRequest } from 'next/server'
export function GET(request: NextRequest) {
const theme = request.cookies.get('theme')?.value
const category = request.nextUrl.searchParams.get('category')
return Response.json({ theme, category })
}A Route Handler can use NextRequest to read a cookie and a parsed query parameter before returning a Web Response.
A Route Handler needs to inspect a session, preference, or feature cookie before producing a response.
A handler needs typed, convenient access to pathname and query-string data from the incoming URL.
A server-side request handler needs standard Request methods together with Next.js-specific request helpers.
NextRequest extends the standard Web Request API, so it can be used where normal request behavior is needed. It additionally provides cookies and nextUrl for Next.js-specific request handling.
Follow-up: How would you extract a query parameter? Use request.nextUrl.searchParams.get('name'), which applies the URL API's search-parameter interface.
The cookies property lets code read or mutate the request's Set-Cookie-related cookie data. Its get(name) operation returns the cookie value, returns undefined when absent, and returns the first matching value when multiple cookies exist.
Follow-up: How should absent cookies be handled? Treat the result as optional and branch or use optional chaining before accessing the value.
nextUrl extends the native URL API with additional convenience methods and Next.js-specific properties. It is useful for inspecting the parsed incoming URL, including query parameters through searchParams.
Follow-up: What should replace removed request metadata? Do not use ip or geo from the current NextRequest API; obtain deployment-specific information through supported platform mechanisms instead.
RequestURLcookiesnextUrlRoute HandlersNextResponse