← All notes

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

Sources

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

Permanent ID: iil:note:a8fyde73

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.},
}

Markdown

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.

All artifact files (JSON index)

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.