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
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.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Starlette, Middleware; FastAPI, CORS (Cross-Origin Resource Sharing). BSD-3-Clause; MIT.
Cite this
iinventedlight. 2026. "Allow a browser origin with Starlette or FastAPI CORS middleware." iinventedlight Library, iil:note:a8fyde73, version of 2026-11-19. https://iinventedlight.com/library/notes/starlette-cors/. License: CC-BY-4.0.
@misc{iil_note_a8fyde73,
author = {{iinventedlight}},
title = {{Allow a browser origin with Starlette or FastAPI CORS middleware}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/starlette-cors/},
note = {Permanent ID iil:note:a8fyde73. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- starlette-cors.table.json (table; CC-BY-4.0)
- starlette-cors.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/starlette-cors.md.