Skip to main content

Configure CORS

Let web pages served from another site call the API. Browsers refuse such calls unless the API says they are allowed.

How it works​

Browsers refuse a page from one origin calling an API on another unless the API allows it. The gateway answers the preflight OPTIONS request and adds the CORS headers, so the service does not have to. This matters only for calls made from a browser; server-to-server calls ignore CORS.

CORS is not access control

It tells browsers what to allow. A client that is not a browser ignores it: authenticate the API as well.

Every parameter and its default: reference.

Use cases​

When the API is called from JavaScript running in a browser, on a page that is not served from the same address as the API. Calls from servers and scripts are not concerned.

Prerequisites​

Procedure​

  1. In API Manager, go to API Instances and open the API. To cover everything published on a gateway instead, open the gateway.
  2. Open its Security policies and add the rule CORS.
  3. In Allowed Origins, list the sites allowed to call the API, such as https://app.example.com.
  4. Adjust Allowed Methods and Allowed Headers to what those pages send.
  5. Turn Allow Credentials on only if the pages send cookies or an authorization header.
  6. Deploy.

Parameters​

FieldWhat it isDefault
Allowed OriginsThe sites allowed to call the API.*: every site
Allowed MethodsThe HTTP methods those sites may use.GET, POST, PUT, DELETE, OPTIONS
Allowed HeadersThe headers those sites may send.Content-Type, Authorization
Exposed HeadersThe response headers the calling page may read.—
Cache DurationHow long a browser remembers these rules before asking again.3600s
Allow CredentialsAllow cookies and authorization headers in cross-origin calls.Off

Verification​

What a browser asks before calling
curl -i -X OPTIONS https://orders.example.com/orders \
-H "Origin: https://app.example.com" \
-H "Access-Control-Request-Method: GET"
Answer
HTTP/1.1 200 OK
access-control-allow-origin: https://app.example.com

Behavior and limits​

  • Allow Credentials requires named origins: it does not work with *.
  • CORS is not a protection: it tells browsers what is allowed. Combine it with an authentication rule.

See also​