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.
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.
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
