How do CORS configurations permit authorized web applications to execute client-side API requests against Salesforce?
Answer
Basics: Browsers block a page at https://app.example.com from reading responses from another origin (https://myorg.my.salesforce.com) unless that server replies with Access-Control-Allow-Origin for it. Salesforce lets admins whitelist origins so single-page apps and widgets can call its APIs from client-side code.
How it works:
- Setup, Security, CORS, New: enter the origin URL pattern (scheme, host, optional port;
https://*.example.comstyle wildcards for subdomains). - The browser sends a preflight
OPTIONSrequest for requests with custom headers such asAuthorization. - If the origin is whitelisted, Salesforce answers with the allow headers; the actual call proceeds.
- The call still needs a valid OAuth access token (obtained through a flow such as Web Server with PKCE, or User-Agent). Check also the setting for enabling CORS on OAuth endpoints if your SPA calls token or userinfo endpoints from the browser.
Advanced / deep dive: CORS is supported for Salesforce REST-based APIs (REST, Chatter REST, Apex REST, Bulk API 2.0 and OAuth endpoints depending on settings); SOAP API is not covered. It does not apply to Visualforce pages or Apex callouts. Security caveats: only the browser enforces it, so server-to-server clients ignore it; whitelisting a wildcard or origin you do not control exposes users' tokens to scripts there. Exposing an SPA that holds access tokens in the browser carries XSS risk; for sensitive apps use a backend-for-frontend that holds tokens server-side. Do not confuse with CSP Trusted Sites (controls what Salesforce pages may call outward) and with Remote Site Settings (Apex callouts).
Code / config example: