Fix guides · Lovable, Bolt, Cursor, Replit ·

Fix CORS error: No Access-Control-Allow-Origin header

Access to fetch at 'https://api.example.com' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present

The browser sent your request, but the API's response didn't include an Access-Control-Allow-Origin header naming your site, so the browser refused to hand the data to your JavaScript. This is a browser security rule (CORS, short for Cross-Origin Resource Sharing). The fix is on the server: either make the API you own send the right CORS headers, or move calls to third-party APIs into your own server route.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. Your own API doesn't send CORS headers for your origin

How to tell: In DevTools → Network, click the failed request: the response headers have no Access-Control-Allow-Origin, or it lists a different origin (for example only your production URL, not http://localhost:3000, or the reverse).

Fix: Add CORS handling on the API server, such as the cors middleware in Express, that allows your exact origins, both http://localhost:3000 and your deployed URL. Redeploy the API.

2. Preflight (OPTIONS) request isn't handled

How to tell: The Network tab shows an OPTIONS request to the same URL that fails or returns 404/405. Browsers send this "preflight" check first when you add headers like Authorization or send Content-Type: application/json.

Fix: Make the server answer OPTIONS requests with a 2xx status and include Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers (listing headers like Content-Type and Authorization).

3. Third-party API that isn't meant to be called from browsers

How to tell: You don't control the API, its docs say to call it from a backend, or it needs a secret API key that is currently in your frontend code.

Fix: Create a server-side route or serverless function that calls the API using the key from an environment variable, and have the frontend call your route instead. If a secret key was ever in client code, rotate it.

4. Wrong URL, or the server errored

How to tell: The request shows 404, 500 or a redirect. Or the URL is a placeholder the AI made up, such as api.example.com, or an environment variable for the API base URL is empty. Error pages and redirects often don't carry CORS headers, so a crash looks like a CORS error.

Fix: Point the request at the real endpoint and make sure the base URL environment variable is set. Fix the server error, and make sure CORS headers are also added to error responses. For example, register CORS middleware before routes and error handlers.

5. Supabase Edge Function missing CORS handling

How to tell: The URL contains .supabase.co/functions/v1/. The function either has no OPTIONS branch or returns some responses (often errors) without the CORS headers.

Fix: Following Supabase's CORS pattern, return the CORS headers when req.method is 'OPTIONS' and spread them into every response, including error responses. Then redeploy the function.

How to fix it

  1. Open your browser's DevTools, go to the Network tab, and reload. Click the failing request, plus any OPTIONS request to the same URL, and note the status code and response headers.
  2. Check that the URL is real and correct, not a placeholder like api.example.com and not built from an empty environment variable. Check that the endpoint returns 2xx, not 404 or 500.
  3. Decide whether you own the API. If it's third-party or needs a secret key, create a server route (for example /api/proxy) that calls it with the key from an environment variable, point the frontend at that route — that path is same-origin, so no Access-Control-Allow-Origin header will appear.
  4. If you own the API, add CORS on the server and allow your exact origins: http://localhost:3000 and your production URL. In Express, for example, use the cors package with an origin list.
  5. Make sure OPTIONS preflight requests get a 2xx response that allows the methods and headers you send, such as Content-Type and Authorization. If you send cookies, set Access-Control-Allow-Credentials: true and use an exact origin, not *.
  6. For the own-API path: make sure CORS headers are also present on error responses, redeploy the server, then reload the page and confirm Access-Control-Allow-Origin appears in the response and the data loads. For the server-route path, confirm the request goes through the server route (status 2xx) and the data loads.

Using Lovable, Bolt or v0?

In Lovable or Bolt, tell the tool: "My fetch to [URL] is blocked by CORS. Don't call third-party APIs with secret keys from the browser. Move the call to a backend function and keep the key in secrets." If the call goes to a Supabase Edge Function, ask it to handle the OPTIONS preflight and add CORS headers to every response, including errors. Then check the function's logs in the Supabase dashboard for crashes. After you publish, make sure the API allows your live domain, not just the preview or localhost URL.

Using Cursor, Claude Code or Windsurf?

Ask the agent to first find out whether the failing URL is your own API, a third-party API, or a placeholder, and whether an OPTIONS preflight is involved. Have it make the change on the server side, either CORS config or a proxy route, not in the fetch call. To verify, have it run curl -i -X OPTIONS with an Origin: http://localhost:3000 header and an Access-Control-Request-Method header against the endpoint and show you the Access-Control-Allow-* headers in the response. Then reload the page and confirm in the Network tab.

The “fix” to avoid

Agents often add mode: 'no-cors' to fetch. That hides the error but gives you an "opaque" response your code can't read, so the data is still missing. Other common bad fixes are routing through a public CORS proxy, which leaks your requests and keys to a stranger, or setting Access-Control-Allow-Origin: * on an API that uses cookies, which browsers reject when credentials are included. Adding Access-Control-Allow-Origin to the request headers in your frontend does nothing, because the header has to come from the server's response.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

My browser fetch fails with: "Access to fetch at 'https://api.example.com' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present". Check in this order: 1) Is the URL correct and not a placeholder, and does the endpoint return 2xx (a 404/500 often lacks CORS headers)? 2) Is this API ours or third-party? 3) If ours, add CORS on the server: allow our exact origins (localhost:3000 and the production URL), answer OPTIONS preflight, allow the headers we send (Content-Type, Authorization), and include CORS headers on error responses too. 4) If third-party or it needs a secret key, create a server route that calls it with the key from an environment variable, and have the frontend call that route. Don't use mode: 'no-cors', public CORS proxies, or '*' with credentials. Verify in DevTools Network that the response includes Access-Control-Allow-Origin and the data loads.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Why does the request work in Postman or curl but not in my browser?

CORS is enforced only by browsers, to protect users. Postman and curl ignore it, so a working request there just proves the server responds. It doesn't prove the server allows your site's origin.

Can I fix a CORS error from my frontend code?

No. The Access-Control-Allow-Origin header must come from the server's response. From the frontend side, your only real option is to call your own backend route, which then calls the API server-to-server where CORS doesn't apply.

Why did it work on localhost but break after deploying (or the other way round)?

The API's allowed-origins list probably includes only one of them. Origins must match exactly, including http vs https and the port, so add every origin your app runs on.

Official documentation

Related errors

Get fixes like this every week