Skip to main content
NextResponse extends the native Web Response API with additional convenience methods for use in Middleware and Route Handlers.

Static methods

NextResponse.json()

Creates a Response with a JSON body and sets the Content-Type header to application/json.
app/api/route.ts
any
required
The value to serialize as JSON.
ResponseInit
Optional response options: status, statusText, headers.

NextResponse.redirect()

Creates a response that redirects the user to a URL.
You can modify the URL before redirecting:
string | URL
required
The destination URL.
ResponseInit
Optional response options (e.g. { status: 308 } for permanent redirect).

NextResponse.rewrite()

Creates a response that rewrites (proxies) the request to a given URL while keeping the original URL in the browser address bar.
string | URL
required
The URL to proxy the request to.

NextResponse.next()

Continues to the next middleware or route handler without modifying the response. Useful in middleware when you want to pass through with optional header modifications.
To forward modified request headers upstream (to the page/route handler, not to the client):
Avoid using NextResponse.next({ headers }) (the shorthand form) to send response headers to the client — it can override framework expectations (e.g. Content-Type for Server Actions) and leak sensitive data. Use NextResponse.next({ request: { headers } }) to forward request headers upstream only.

cookies

Read or mutate the Set-Cookie header of the response.
{ name: string; value: string } | undefined
Returns the first cookie with the given name.
{ name: string; value: string }[]
Returns all cookies with the given name, or all cookies if no name is provided.
void
Sets a cookie on the response.
boolean
Returns true if the cookie exists on the response.
boolean
Deletes the cookie from the response. Returns true if deleted.

Example: middleware with auth check

middleware.ts