API Directory โ€บ Guides โ€บ CORS Explained: Why Your API Call Fails in the Browser

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

The headers involved

Access-Control-Allow-OriginThe origin(s) allowed, or * for any. The main one.
Access-Control-Allow-MethodsWhich HTTP methods are permitted cross-origin.
Access-Control-Allow-HeadersWhich request headers the client may send (e.g. Authorization, Content-Type).
Access-Control-Allow-Credentialstrue if cookies/auth may be sent. Cannot be combined with Allow-Origin: *.
Access-Control-Max-AgeHow 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

Things CORS is often blamed for but isn’t

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

Browse all 196 free APIs โ†’

๐Ÿ”Œ Explore free public APIs

Hundreds of free APIs, searchable by category, with auth & CORS at a glance.

Browse the directory โ†’