Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Next.js Edge Middleware example showing how to add, update, and delete HTTP request headers inside middleware.ts.

Next.js example that wires GrowthBook feature flags and experiments into the Flags SDK with the @flags-sdk/growthbook adapter.

Next.js example that streams structured object generation to the client using the Vercel AI SDK useObject hook.

Open-source example wiring AnyCable-Go WebSockets into a Next.js app on Vercel for serverless real-time messaging and JWT-authenticated clients.

Vercel-maintained HTML5 starter template preloaded with Vercel Analytics and Edge Middleware routing.
Modifying Request Headers in Middleware is a Vercel Edge Middleware example for Next.js that shows how to add, update, and delete incoming HTTP request headers inside a middleware.ts file before the request continues to a route handler.
It is a single-file Next.js middleware example, published in the vercel/examples repository, that intercepts an incoming request, clones its headers, mutates that copy, and forwards the modified set downstream. The input is the client's NextRequest; the output is a NextResponse.next() call whose request option carries the rewritten headers. Header mutation through the request object has been available since Next.js v13.0.0, and the demo runs at edge-middleware-modify-request-header.vercel.app.
x-hello-from-middleware1 to hello and x-hello-from-middleware2 to world!, using the standard Headers.set() API.user-agent header with a middleware-supplied value, replacing whatever the client sent.x-from-client header with Headers.delete() before the request reaches the application.new Headers(request.headers) so the original header set is preserved and only the intended keys change.NextResponse and the NextRequest type from next/server.pnpm create next-app --example pulls the same example into a local project, then pnpm dev starts the dev server.x-hello-from-middleware1 to every request so logs and downstream services can correlate them.user-agent (or similar) to a controlled value before the request hits your application code.x-from-client that a browser or proxy could otherwise spoof.NextResponse.rewrite, so the same mutation pattern applies when routing to a different path.NextRequest and clones request.headers into a new Headers object.set() twice to add new headers, set() again to update user-agent, and delete() to drop x-from-client.NextResponse.next with the mutated headers attached to the outgoing request, so the route sees the edited set.Modifying request headers in middleware is available since Next.js v13.0.0, so the example targets that version or later. Older releases cannot attach a modified header set to the forwarded request the way this example does.
Use pnpm create next-app --example with the vercel/examples URL for this template, then run pnpm dev. The repository also supports one-click deployment to Vercel through its clone URL.
Yes. The example's comments state that request headers can also be set when returning NextResponse.rewrite, using the same Headers API shown for the pass-through case.
The page lists edge-functions-add-header as a related template, which covers header addition without the update and delete cases demonstrated here.
