Skip to main content

CORS Middleware

When your API is consumed by a browser app on a different domain, browsers enforce Cross-Origin Resource Sharing (CORS). Prism’s PrismCORSMiddleware handles the OPTIONS preflight requests and response headers automatically.

Quick Setup

Allow All Origins
Using "*" allows any website to call your API. Fine for public APIs and development, but restrict origins in production.

Production Configuration

Specific Origins

SPA Frontend Pattern

A typical setup when your Swift API serves a React/Vue/Angular frontend:
SPA Setup

How It Works

  1. Preflight: Browser sends OPTIONS request before the real request. CORS middleware responds with allowed origins/methods/headers.
  2. Actual request: Middleware adds Access-Control-Allow-Origin and related headers to the response.
If you’re only building a mobile app or server-to-server API, you don’t need CORS at all — it’s a browser-only security mechanism.