DEV Community

Applied Efficiency
Applied Efficiency

Posted on

CORS blocked? Your browser is protecting you — your API call took the wrong road

You asked your AI to wire up an API. It wrote a fetch call, hit run, and the console says:

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

Nothing is wrong with your fetch code. This is the browser doing its job.

What CORS actually is

Your page lives on one domain. The API lives on another. The browser asks the API, "Do you allow calls from this page?" If the API doesn't answer yes, the browser refuses — before the API ever sees the request. It's a permission handshake, not a bug in your app.

Why it happens in AI-built apps

  • Your AI called a third-party API straight from the browser. Most APIs don't allow that.
  • Your own backend doesn't send the Access-Control-Allow-Origin header.
  • The API simply never intended browser calls.

The fix

Don't fight the browser. Move the call to your own backend route — a server-side function your frontend calls instead. The API key stays on the server where it belongs, and the browser never talks to the third party directly.

If it's your own API, add the CORS header allowing your site's origin, then restart and hard-refresh.

Paste this into your AI:

"I'm hitting a CORS error calling this API from the browser. Refactor it so the call happens in a server-side route, my frontend calls that route instead, and my API key stays on the server."

This is pattern 3 of 5 in the Debug Clinic at Last20 Academy — we pattern-match the five errors AI-built apps hit most, so you get a real diagnosis instead of another guess.

Top comments (0)