FastAPI CORS middleware
Answer: CORS, or Cross-Origin Resource Sharing, is the browser rule set for cross-origin HTTP requests. In FastAPI, configure it with CORSMiddleware. Create a list of allowed origins and add the middleware to the app.
Examples
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://example.com"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Rules
- Import
CORSMiddleware. Create a list of allowed origins (as strings). - A wildcard origin is incompatible with
allow_credentials=Truein browsers.
See also
Claims
- id: C1 claim: CORS means Cross-Origin Resource Sharing. source: S1 evidence: "Cross-Origin Resource Sharing"
- id: C2 claim: Configure CORS with CORSMiddleware. source: S1 evidence: "CORSMiddleware"
- id: C3 claim: The source docs also document CORSMiddleware. source: S2 evidence: "CORSMiddleware"
Sources
- [S1] CORS (Cross-Origin Resource Sharing). FastAPI docs. https://fastapi.tiangolo.com/tutorial/cors/ . License: MIT. Accessed 2026-11-18.
- [S2] cors.md. FastAPI source docs. https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/tutorial/cors.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, CORS (Cross-Origin Resource Sharing); FastAPI source docs, cors.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, CORS (Cross-Origin Resource Sharing); FastAPI source docs, cors.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors.
Cite this
iinventedlight. 2026. "FastAPI CORS middleware." iinventedlight Library, iil:note:3e56yxrr, version of 2026-11-18. https://iinventedlight.com/library/notes/fastapi-cors/. License: CC-BY-4.0.
@misc{iil_note_3e56yxrr,
author = {{iinventedlight}},
title = {{FastAPI CORS middleware}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/fastapi-cors/},
note = {Permanent ID iil:note:3e56yxrr. 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).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/fastapi-cors.md.