November 22, 2026 · 9 min read · Thomas Osayi
How to fix a CORS error on a Next.js API route
Blocked by CORS policy calling your Next.js API? Read the exact error, handle the OPTIONS preflight, set headers in route handlers or next.config, or proxy instead.
In Chrome the error usually reads: "Access to fetch at 'https://api.yourapp.com/api/orders' from origin 'https://www.yourapp.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource." In Firefox it is "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ... (Reason: CORS header 'Access-Control-Allow-Origin' missing)."
Either way, the request may have reached your server, but the browser refused to hand the response to your code. CORS errors look scary because the console message is long, but the fix is a small set of headers in the right place. Here is how to find which case you are in and what to change.
What does a CORS error on a Next.js API route actually mean?
It means a page on one origin called your API on another origin, and the response did not include headers telling the browser that this origin is allowed to read it.
An origin is the protocol, domain and port together. https://www.yourapp.com and https://yourapp.com are different origins. So are http://localhost:5173 and http://localhost:3000. If your Next.js app calls its own API routes from its own pages on the same domain, there is no CORS at all, which is why this error often appears only after you split the frontend and backend, or start calling the API from a second app.
CORS is enforced by the browser, not the server. A curl request or a server-side fetch from another backend will succeed, which confuses people. MDN explains that the response must include Access-Control-Allow-Origin with the requesting site's origin, or the browser blocks the read.
The common setups that trigger it:
- A Lovable or Bolt frontend (Vite) on one domain calling a Next.js API on another.
- A mobile web view or browser extension calling your API.
- Your marketing site on www calling an app on app.yourdomain.com.
- Your own pages calling an absolute URL that points at a different host than the one the user is on.
What is the preflight OPTIONS request and why does it fail?
Before many cross-origin requests, the browser sends an OPTIONS request to ask permission, and if that response lacks the CORS headers, the real request never runs.
The Vercel CORS guide notes that browsers send a preflight before methods other than GET, HEAD or POST, and before requests with custom headers. In practice, a POST with Content-Type: application/json, or any request with an Authorization header, triggers one. That covers almost every API call an app makes.
The Next.js route.js reference says that if you do not define OPTIONS in a route handler, Next.js implements it automatically and sets the Allow header based on the other methods you defined. That automatic response does not include your CORS headers. So the preflight returns 204 or 200 with no Access-Control-Allow-Origin, and the browser stops there. That is the exact "Response to preflight request doesn't pass access control check" message.
In the Network tab of DevTools you will see two rows for the same URL: one OPTIONS and one POST. If the OPTIONS row is red or the POST row never appears, the preflight is the problem.
How do I add CORS headers in a Next.js route handler?
Return the headers from every method in the route, and export an OPTIONS function that returns them too.
The Next.js docs show the basic shape: return a new Response with headers for Access-Control-Allow-Origin, Access-Control-Allow-Methods (for example GET, POST, PUT, DELETE, OPTIONS) and Access-Control-Allow-Headers (for example Content-Type, Authorization). Their sample uses a wildcard origin, which is fine for a fully public API and wrong for most apps.
What I put in a real app/api/orders/route.ts:
- A list of allowed origins, read from an environment variable such as ALLOWED_ORIGINS, so production, preview and local development each work.
- A helper that reads the Origin header from the request, checks it against the list, and returns the CORS headers with that exact origin echoed back.
- Vary: Origin in those headers, so a CDN never serves one origin's response to another.
- An exported OPTIONS function that returns status 204 with the CORS headers and Access-Control-Max-Age, so the browser can cache the preflight.
- The same headers added to the GET and POST responses, including error responses.
That last point is the one people miss. If your POST throws and returns a 500 without CORS headers, the browser reports a CORS error rather than the 500, and you go hunting in the wrong place.
Can I set CORS headers for every API route at once?
Yes, using the headers function in next.config or the proxy.ts file, and the Next.js docs recommend those when more than one route handler needs CORS.
next.config headers
In next.config.js or next.config.ts, an async headers function returns a list of rules. A rule with source set to /api/:path* and a headers array of key and value pairs applies those headers to every matching response, including the automatic OPTIONS response. The Vercel guide notes this is static: you can allow one origin, but you cannot check the request against a list.
Best for: one known frontend origin and no per-request logic.
proxy.ts
Starting with Next.js 16, Middleware is called Proxy, and the file is proxy.ts at the project root or inside src, next to the app folder. The functionality is the same as the old middleware.ts. A proxy with a matcher on /api/:path* can read the Origin header, check an allowlist, answer OPTIONS requests directly with a 204, and add headers to everything else.
Best for: several allowed origins, preview URLs, or credentials.
What are the other causes of CORS errors in Next.js?
Redirects, credentials with a wildcard, deployment protection and server errors cause a large share of the CORS errors I see, even when headers are set.
Ranked roughly by how often they catch people:
- A redirect on the API URL. Calling yourapp.com when Vercel redirects to www.yourapp.com, or a trailing slash redirect, sends a 308 to the preflight. Chrome reports "Redirect is not allowed for a preflight request." Call the final URL directly.
- Credentials with a wildcard. If your fetch uses credentials: "include" for cookies, the browser rejects a wildcard origin with "The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'." Echo the exact origin and add Access-Control-Allow-Credentials: true.
- A missing header in Access-Control-Allow-Headers. If the request sends Authorization or a custom header like x-api-key that is not listed, the preflight fails.
- Vercel Deployment Protection on preview URLs. The Vercel guide notes it can return 401 to unauthenticated preflights before your code runs, and that the OPTIONS Allowlist setting fixes it for paths such as /api.
- A crash in the handler that returns a response without CORS headers.
Should I proxy the request instead of enabling CORS?
Often yes, because if the browser only ever talks to its own origin, there is no CORS to configure.
Rewrites in the frontend app
If the frontend is a Next.js app, a rewrites rule in its next.config can map /api/:path* to https://api.yourapp.com/:path*. The browser calls /api/orders on its own domain, the Next.js server forwards it, and the response comes back as same-origin. The Vercel CORS guide describes the same approach in vercel.json, noting the browser sees a same-origin response and sends no preflight.
The Vite dev server proxy
For a Vite frontend in development, the server.proxy option in vite.config does the same thing locally. It does not exist in production, so pair it with a rewrite on your host.
Server components and server actions
If you control both sides and they are both Next.js, the simplest fix is to stop calling the API from the browser. Server components and server actions run on the server, where CORS does not apply.
A worked example: a Lovable frontend calling a Next.js API
Here is an illustration with a made-up business. A tutoring company has a Lovable booking frontend on book.tutorco.com and a Next.js API on api.tutorco.com that creates Stripe checkout sessions. Booking fails with the preflight error above.
What I find and change:
- The route handler has POST only, so the automatic OPTIONS response has no CORS headers. I add an OPTIONS export returning 204 with the headers.
- The frontend sends Authorization with a Supabase token, which is not in Access-Control-Allow-Headers. I add it.
- The frontend calls tutorco.com/api, which redirects to api.tutorco.com. I point it at the final URL.
- Previews fail with 401 on OPTIONS. I check the OPTIONS Allowlist in Deployment Protection settings.
- I move the allowed origins into an environment variable so production and previews both pass.
I verify with curl -i -X OPTIONS against the endpoint, checking for a 2xx status and the Access-Control-Allow-Origin header, which is the check the Vercel guide recommends.
How do I test a CORS fix without guessing?
Test the preflight and the real request separately, from the command line and from the browser, before you redeploy anything else.
Browsers cache preflight results for as long as Access-Control-Max-Age allows, and CDNs can cache responses too, so a fix can look like it failed when you are really seeing an old answer. I use this sequence:
- Send the preflight by hand with curl -i -X OPTIONS, adding an Origin header with your frontend's origin and an Access-Control-Request-Method header of POST. The response should be a 2xx with Access-Control-Allow-Origin set to that exact origin.
- Send the real request with curl -i, the same Origin header and your normal body, and check that the CORS headers appear on that response too, including when it errors.
- In Chrome DevTools, open the Network tab, tick Disable cache, and reload. Click the OPTIONS row and read the response headers, not just the status.
- Try once from an origin that should be refused, so you know the allowlist actually restricts something.
If curl shows the right headers and the browser still complains, look for a redirect, Deployment Protection, or a service worker answering the request before it reaches your code.
When should I get help with CORS?
Get help when the headers look correct in curl and the browser still blocks the request, because that usually points to redirects, caching or auth in front of your code.
If the API also handles payments, check the related flow in "Stripe payment went through but your app did not update? Fix the webhook," since webhooks are server-to-server and never need CORS. A single CORS fix fits my Quick Fix from $60. If the frontend and backend need to be consolidated into one Next.js app, that is a bigger change and fits Fix Plus Features from $200 or Finish and Launch from $600, depending on scope.
The bottom line
A CORS error on a Next.js API route means the browser did not get permission headers it trusts. Confirm the page and API really are on different origins, then make sure both the OPTIONS preflight and the real response return Access-Control-Allow-Origin with the exact calling origin, the methods you use and every header you send. Use next.config headers for one static origin, proxy.ts for allowlists and credentials, and rewrites or server components when you can avoid cross-origin calls entirely. Rule out redirects and server errors before touching headers again.
Questions, answered in one line
Why do I get a CORS error calling my own Next.js API route?
Usually the page and the API are on different origins, such as a Vite frontend calling a Next.js backend, or www calling the bare domain, and the API does not return a matching Access-Control-Allow-Origin header.
How do I handle the OPTIONS preflight in a Next.js route handler?
Export an OPTIONS function from the same route.ts file that returns a 204 response with Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers.
Can I set CORS headers for all API routes at once in Next.js?
Yes, the Next.js docs point to the headers function in next.config.js or to proxy.ts, which was called middleware.ts before Next.js 16.
Is it safe to use a wildcard Access-Control-Allow-Origin?
Only for public data with no cookies or auth headers, because browsers reject a wildcard origin when credentials are included.
Does a CORS error happen with server-side requests?
No, CORS is enforced by browsers, so server components, server actions and server-to-server fetches are not affected.
Sources
- route.js (Route Handlers), CORS Next.js Docs
- How can I enable CORS on Vercel? Vercel
- Reason: CORS header 'Access-Control-Allow-Origin' missing MDN Web Docs
- Proxy Next.js Docs
Related articles
- Supabase auth redirect URL not working? Fix localhost, preview and production redirects
- Environment variables undefined on Vercel? Every cause and fix
- Firestore "Missing or insufficient permissions": what triggers it and how to fix it
- Firebase auth/unauthorized-domain: how to fix it on Vercel, previews and custom domains
Fix, finish and launch your AI-built app
Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.
See the service