What is the Access-Control-Allow-Origin header?
The Access-Control-Allow-Origin response header tells the browser which origin is allowed to read the response of a cross-origin request. It is either a single origin, the wildcard *, or the specific origin echoed back from the request’s Origin header.
API Usage & Best Practices
- Echo, don’t hardcode
*blindly: For APIs serving multiple trusted apps, validate the incomingOriginagainst an allowlist and echo it back, addingVary: Originso caches stay correct. *blocks credentials: The wildcard cannot be combined withAccess-Control-Allow-Credentials: true; requests carrying cookies or auth headers require an explicit origin instead.- Preflight and actual response: Send it on both the preflight (
204) and the actual200response, or the browser rejects the result.
Examples
HTTP/1.1 200 OKAccess-Control-Allow-Origin: https://app.example.comVary: Origin