CORS Explained: Why Your API Call Fails in the Browser
You paste an API into your code, and the browser blocks it with a CORS error. Here's what CORS is, why it exists, and how to get around it.
CORS (Cross-Origin Resource Sharing) is a browser security rule. By default, JavaScript on site-a.com may not read responses from api-b.com. CORS is the mechanism by which api-b.com can opt in and say ‘requests from site-a.com are allowed to read my responses.’
It is a browser thing, not an API thing
- CORS is enforced only by browsers.
curl, Postman, and your backend server ignore it entirely. - The request usually still reaches the server — the browser just blocks your JavaScript from reading the response if the headers aren’t right.
- So ‘CORS error’ never means the API is down; it means the API hasn’t allow-listed your web origin.
The headers involved
| Access-Control-Allow-Origin | The origin(s) allowed, or * for any. The main one. |
| Access-Control-Allow-Methods | Which HTTP methods are permitted cross-origin. |
| Access-Control-Allow-Headers | Which request headers the client may send (e.g. Authorization, Content-Type). |
| Access-Control-Allow-Credentials | true if cookies/auth may be sent. Cannot be combined with Allow-Origin: *. |
| Access-Control-Max-Age | How long the browser may cache the preflight result. |
The preflight request
For anything beyond a ‘simple’ request (a custom header like Authorization, a PUT/DELETE, or a JSON content type), the browser first sends an OPTIONS request — the preflight — asking ‘may I do this?’. The real request only goes out if the preflight response approves it. A failed preflight is the classic silent CORS failure.
How to fix a CORS error
- If you control the API: add the
Access-Control-Allow-*headers, and handleOPTIONSrequests, for your front-end’s origin. - If you don’t control the API: call it from your own backend (a proxy endpoint) and have your front-end talk to your backend. This also keeps your API key off the client.
- For local development: a dev-server proxy (Vite, Next.js rewrites,
http-proxy) routes/api/*to the third party so the browser sees a same-origin request. - Do not rely on public ‘CORS proxy’ services in production — they see all your traffic and disappear without notice.
Things CORS is often blamed for but isn’t
- A 401/403 that also lacks CORS headers looks like a CORS error in the console — check the Network tab for the real status.
- Mixed content (an HTTPS page calling an
http://API) is a different block. - A wrong URL returning an HTML 404 page — the browser can’t read it, so it reports CORS.
FAQ
Why do I get a CORS error when Postman works fine?
Postman is not a browser, so it doesn’t enforce CORS. The API simply hasn’t sent Access-Control-Allow-Origin for your web page’s origin. Call it from your backend instead.
Can I disable CORS?
Not for other people’s visitors. You can disable enforcement in your own browser with a flag or extension for testing, but the real fix is server headers or a proxy.
Is CORS a security feature for the API or the user?
For the user. It stops a malicious site from using the visitor’s browser (and logged-in cookies) to read data from other sites on their behalf.
What is a preflight request?
An automatic OPTIONS request the browser sends before certain cross-origin requests, to check the server permits the method and headers you are about to use.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ