---
source: https://iinventedlight.com/library/notes/starlette-cors.md
title: "Allow a browser origin with Starlette or FastAPI CORS middleware"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Starlette); MIT (FastAPI); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Starlette, Middleware; FastAPI, CORS (Cross-Origin Resource Sharing). BSD-3-Clause; MIT."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: Starlette CORSMiddleware and FastAPI CORS"
tags: [starlette, fastapi, cors, middleware, http]
client: Librarian
sources:
  - id: S1
    title: "Middleware"
    publisher: Starlette
    url: https://starlette.dev/middleware/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "CORS (Cross-Origin Resource Sharing)"
    publisher: FastAPI
    url: https://fastapi.tiangolo.com/tutorial/cors/
    accessed: 2026-11-19
    license: "MIT"
---
# Allow a browser origin with Starlette or FastAPI CORS middleware
**Answer:** `CORSMiddleware` adds appropriate CORS headers to outgoing responses, which allows cross-origin requests from browsers. Defaults are restrictive, so enable particular origins, methods, or headers explicitly.

## Starlette settings

Attach the class with `Middleware(CORSMiddleware, allow_origins=['*'])` when every origin should be permitted. `allow_origins` is a list of origins that should be permitted to make cross-origin requests, such as `https://example.org`. `allow_origin_regex` is a regex string to match against origins. 

## Origins in FastAPI

CORS refers to a frontend in a browser whose JavaScript communicates with a backend in a different origin. An origin is the combination of protocol (`http`, `https`), domain, and port. `http://localhost` and `http://localhost:8080` are different origins, even if they are all in localhost. The browser will send an HTTP `OPTIONS` request to the backend.

| CORSMiddleware | origin |
|---|---|
| CORSMiddleware | Adds CORS headers to outgoing responses |
| allow_origins | List of origins permitted to make cross-origin requests |
| allow_origin_regex | Regex string to match against origins |
| origin | Combination of protocol, domain, and port |

## See also

- [django-csrf](https://iinventedlight.com/library/notes/django-csrf/)
- [http-status-codes](https://iinventedlight.com/library/notes/http-status-codes/)
- [fastapi-error-handling](https://iinventedlight.com/library/notes/fastapi-error-handling/)

## Claims

- id: C1
  claim: Starlette's CORSMiddleware adds CORS headers to outgoing responses.
  source: S1
  evidence: "Adds appropriate CORS headers to outgoing responses"

- id: C2
  claim: Its default parameters are restrictive, so origins, methods, or headers must be enabled explicitly.
  source: S1
  evidence: "restrictive by default, so you'll need to explicitly enable particular origins"

- id: C3
  claim: FastAPI treats an origin as a protocol, domain, and port combination.
  source: S2
  evidence: "An origin is the combination of protocol"

## Sources

- [S1] Middleware. Starlette. https://starlette.dev/middleware/ . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] CORS (Cross-Origin Resource Sharing). FastAPI. https://fastapi.tiangolo.com/tutorial/cors/ . License: MIT. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Starlette, Middleware; FastAPI, CORS (Cross-Origin Resource Sharing). BSD-3-Clause; MIT. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
