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.
import { NextResponse } from 'next/server'
export async function GET(request: Request) {
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
}
The value to serialize as JSON.
Optional response options: status, statusText, headers.
NextResponse.redirect()
Creates a response that redirects the user to a URL.
import { NextResponse } from 'next/server'
return NextResponse.redirect(new URL('/new', request.url))
You can modify the URL before redirecting:
import { NextResponse } from 'next/server'
const loginUrl = new URL('/login', request.url)
loginUrl.searchParams.set('from', request.nextUrl.pathname)
return NextResponse.redirect(loginUrl)
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.
import { NextResponse } from 'next/server'
// Browser shows /about, but content is served from /proxy
return NextResponse.rewrite(new URL('/proxy', request.url))
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.
import { NextResponse } from 'next/server'
return NextResponse.next()
To forward modified request headers upstream (to the page/route handler, not to the client):
import { NextResponse } from 'next/server'
const newHeaders = new Headers(request.headers)
newHeaders.set('x-version', '123')
return NextResponse.next({
request: { headers: newHeaders },
})
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.
get(name)
{ name: string; value: string } | undefined
Returns the first cookie with the given name.response.cookies.get('show-banner')
getAll(name?)
{ name: string; value: string }[]
Returns all cookies with the given name, or all cookies if no name is provided.response.cookies.getAll('experiments')
Sets a cookie on the response.response.cookies.set('show-banner', 'false')
Returns true if the cookie exists on the response.response.cookies.has('experiments')
Deletes the cookie from the response. Returns true if deleted.response.cookies.delete('experiments')
Example: middleware with auth check
import { type NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
const session = request.cookies.get('session')
if (!session && request.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/login', request.url))
}
const response = NextResponse.next()
// Set a cookie on the response
response.cookies.set('last-visited', request.nextUrl.pathname)
return response
}