JSON endpoints
A Route Handler needs a structured HTTP payload for a browser or API client.
Next.js 16+ · Class
NextResponse extends the Web Response API with convenient helpers for JSON responses, redirects, rewrites, continued Proxy routing, and response cookies. It can be returned from Route Handlers or Proxy wherever a standard Response is accepted.
NextResponse extends the Web Response API with helpers for common App Router response tasks. In Route Handlers and Proxy, it can produce JSON, redirect or rewrite requests, continue routing, and manipulate response cookies; the returned object can be used anywhere a standard Response is expected.
import { NextResponse } from 'next/server'A NextResponse instance, which can be returned wherever a standard Web Response is expected.
import { NextResponse } from 'next/server'
export function GET() {
return NextResponse.json({ ok: true })
}A Route Handler can return a JSON response created by NextResponse.json.
A Route Handler needs a structured HTTP payload for a browser or API client.
A request should be served by another route without exposing that internal destination in the browser URL.
An authentication or locale check must send the client to a different destination.
A response must establish or update session, preference, or tracking state.
NextResponse is Next.js’s extension of the Web Response API. It adds helpers such as json(), redirect(), rewrite(), next(), and response-cookie operations, while remaining returnable wherever a Response is accepted.
Follow-up: How would you expose a modified request header upstream? Use NextResponse.next with an options object whose request property contains the new headers; putting headers at the top level sends response headers to the client.
NextResponse.json creates a response whose body contains the supplied JSON value. A Route Handler can return that result directly, for example from a GET function.
Follow-up: When would rewrite be preferable to redirect? Rewrite serves another route while keeping the requested URL, whereas redirect instructs the client to navigate to a different URL.
The cookies API reads or mutates the response’s Set-Cookie header. set(name, value) adds a cookie, get(name) reads one, and has(name) checks whether one exists.
Follow-up: What should code account for when reading cookies? get can return undefined when no cookie is found, and with duplicate names it returns the first match.
NextRequestRoute HandlersProxyResponsecookiesjson()redirect()rewrite()