Getting started with Routing Middleware using a coding agent
If you use a coding agent like Claude Code, Cursor, or Cline, you can set up Routing Middleware by describing what you need instead of writing the configuration by hand.
For step-by-step manual setup, see the manual quickstart.
Describe the behavior you want and let your agent create the middleware file. Here are example prompts for common use cases:
Add routing middleware that redirects /old-blog to /blog and
/legacy-docs/* to /docs/*. Use permanent redirects. Only run
on non-static paths.Add routing middleware that checks for an auth token cookie.
If the cookie is missing, redirect to /login. Skip the check
for /login, /api/auth/*, and static assets.Add routing middleware that reads the user's country from the
x-vercel-ip-country header and rewrites /pricing to
/pricing/eu for European countries or /pricing/us for the US.Next.js 16 users: Next.js 16 renamed the middleware file from
middleware.ts to proxy.ts and changed the function export from
middleware to proxy. When using Next.js 16 or later, use proxy.ts
instead of middleware.ts. The proxy function runs on Node.js only (Edge
runtime is not supported). See the Next.js proxy
documentation
for details.
| Detail | Value |
|---|---|
| File location | middleware.ts in project root (or proxy.ts for Next.js 16+) |
| Export | export default function middleware(request: Request) (or export function proxy for Next.js 16+) |
| Config export | export const config = { matcher: [...] } |
| Default runtime | edge (set runtime: 'nodejs' in config for Node.js) |
| Bun runtime | Set bunVersion in vercel.json and runtime: 'nodejs' in config |
| Request object | Standard Request API |
| Geo headers | x-vercel-ip-country, x-vercel-ip-country-region, x-vercel-ip-city |
| Path matching | Supports regex, named params, and wildcards in the matcher config |
Was this helpful?