NextResponse in Next.js

Last Updated : 1 Jul, 2026

NextResponse is a built-in Next.js API used to create and modify HTTP responses in Middleware and API route handlers.

  • Creates and customizes HTTP responses.
  • Supports cookies, headers, and JSON responses.
  • Enables redirects, rewrites, and request forwarding.

Features of NextResponse

NextResponse provides built-in methods to create and modify HTTP responses, manage cookies, return JSON data, and control request flow in Next.js.

1. Cookies

Cookies are a way to store small amounts of data on the client side, allowing for persistent state across requests. NextResponse provides several methods to interact with cookies easily.

set(name, value)

This method sets a cookie in the response.

JavaScript
import { NextResponse } from 'next/server';

export function middleware(req) {
    const response = NextResponse.next();
    response.cookies.set('userToken', 'abc123');
    return response;
}

Output:

Screenshot-2024-08-15-210426
set(name, value)

delete(name)

Deletes a cookie from the response.

JavaScript
import { NextResponse } from 'next/server';

export function middleware(req) {
    const response = NextResponse.next();
    response.cookies.delete('userToken');
    return response;
}

Output:

Screenshot-2024-08-15-205933
delete(name)

Note: To read cookies using get(name) or getAll(), use the cookies API of NextRequest.

2. JSON Response

The json() method creates and returns a JSON response with the specified data.

JavaScript
import { NextResponse } from 'next/server';

export function GET() {
    return NextResponse.json({
        message: 'Hello, world!',
    });
}

Output:

{
"message": "Hello, world!"
}

3. Redirect

The redirect() method allows you to redirect the user to a different URL.

JavaScript
import { NextResponse } from "next/server";

export function middleware(request: Request) {
  return NextResponse.redirect(new URL("/new-path", request.url));
}

export const config = {
  matcher: ["/old-path"],
};

Output:

Screenshot-2024-08-20-181939

4. Rewrite

The rewrite() method rewrites the request to a different URL without changing the URL displayed in the browser.

JavaScript
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export function middleware(request: NextRequest) {
  return NextResponse.rewrite(
    new URL("/another-path", request.url)
  );
}

export const config = {
  matcher: ["/old-path"],
};

Output:

Screenshot-2026-07-01-121319

5. Next

The next() method passes the request to the next middleware or route handler without modifying the response.

JavaScript
import { NextResponse } from 'next/server';

export function middleware(request) {
    // Middleware logic

    return NextResponse.next();
}

Note: Since NextResponse.next() simply forwards the request, there is no visible change in the browser. The expected output is the normal rendering of the requested page.

Steps to Implement NextResponse in Next.js

Follow the steps given below:

Step 1: Create a New Next.js Application

Create a new Next.js project using the following commands:

npx create-next-app@latest next-response-example
cd next-response-example

Step 2: Create the Middleware File

Create a middleware.js (or middleware.ts) file in the project root.

middleware.js

Step 3: Add the NextResponse Code

  • app/page.js
  • app/new-path/page.js
  • middleware.js
app/page.js
export default function Home() {
    return (
        <div>
            <h1>Welcome to Next.js!</h1>
        </div>
    );
}
app/new-path/page.js
export default function NewPath() {
    return (
        <div>
            <h1>Welcome to New Path</h1>
        </div>
    );
}
middleware.js
// middleware.js
import { NextResponse } from "next/server";

export function middleware(request) {
    const response = NextResponse.redirect(
        new URL("/new-path", request.url)
    );

    response.cookies.set("user", "nikunj sonigara");
    response.headers.set("X-Custom-Header", "example-value");

    return response;
}

export const config = {
    matcher: ["/old-path"],
};

Step 4: Run the Application

Start the development server using:

npm run dev

Visit:

http://localhost:3000/old-path

Output:

The browser is redirected to:

http://localhost:3000/new-path

and displays:

img
Comment