How to Fix CORS Errors with API Gateway

Your API must return CORS headers on every response, including OPTIONS preflights and errors, for browsers to accept it.

The classic browser console error:

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

CORS is enforced by browsers only. If the same request works in curl or Postman, your API is fine. It's just missing the headers browsers need.

HTTP APIs: use the built-in CORS settings

In your HTTP API's CORS configuration, set:

  • Allowed origins: your front-end, e.g. https://app.example.com
  • Allowed methods: e.g. GET, POST, OPTIONS
  • Allowed headers: include content-type and authorization if you send them

API Gateway then answers preflight OPTIONS requests and adds headers for you.

REST APIs with Lambda proxy integration: your function must return the headers

"Enable CORS" in the console only covers the OPTIONS method. With a proxy integration, your Lambda's responses must include the header themselves:

Python
return {
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": "https://app.example.com",
        "Content-Type": "application/json",
    },
    "body": json.dumps(data),
}

Errors hide CORS headers

If your API returns a 401, 403, or 500 without CORS headers, the browser reports a CORS error and hides the real problem. On REST APIs, add CORS headers to Gateway Responses (e.g. DEFAULT_4XX and DEFAULT_5XX). Also make sure your function returns them on its own error paths.

Sending cookies or credentials?

You can't use Access-Control-Allow-Origin: * with credentials. Return the exact origin and Access-Control-Allow-Credentials: true.

Tip

Open the browser's Network tab and inspect the preflight OPTIONS request first. Its status code and headers usually point straight at the problem.

Learn more: API Gateway auth, CORS & limits and Writing a Lambda backend.

Key takeaways

  • CORS is a browser rule: curl working doesn't mean CORS is configured.
  • HTTP APIs have built-in CORS settings, but REST proxy integrations need headers from your code.
  • Error responses need CORS headers too, or the browser hides the real error.

Learn the details