FastAPI middleware - add_middleware, CORSMiddleware and @app.middleware
Answer: FastAPI applications can have middleware. Middleware is a function that works with every request. It also runs on every response before returning it. Add it with add_middleware, or with @app.middleware("http").
Examples
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://example.com"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def add_request_id(request: Request, call_next):
response = await call_next(request)
response.headers["X-App"] = "demo"
return response
Rules
- The custom function takes
requestandcall_next. It receives the request as a parameter and must return a response. - Add CORS as middleware if a browser client will call the API.
See also
Claims
- id: C1 claim: You can add middleware to FastAPI applications. source: S1 evidence: "You can add middleware to FastAPI applications"
- id: C2 claim: The FastAPI source docs also say you can add middleware. source: S2 evidence: "You can add middleware to FastAPI applications"
- id: C3 claim: A middleware works with every request. source: S2 evidence: "A \"middleware\" is a function that works with every"
Sources
- [S1] Middleware. FastAPI docs. https://fastapi.tiangolo.com/tutorial/middleware/ . License: MIT. Accessed 2026-11-18.
- [S2] tutorial/middleware.md. fastapi/fastapi (GitHub). https://github.com/fastapi/fastapi/blob/master/docs/en/docs/tutorial/middleware.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, Middleware; fastapi/fastapi (GitHub), tutorial/middleware.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, Middleware; fastapi/fastapi (GitHub), tutorial/middleware.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors.
Cite this
iinventedlight. 2026. "FastAPI middleware - add_middleware, CORSMiddleware and @app.middleware." iinventedlight Library, iil:note:1xfh3ww9, version of 2026-11-18. https://iinventedlight.com/library/notes/fastapi-middleware/. License: CC-BY-4.0.
@misc{iil_note_1xfh3ww9,
author = {{iinventedlight}},
title = {{FastAPI middleware - add\_middleware, CORSMiddleware and @app.middleware}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/fastapi-middleware/},
note = {Permanent ID iil:note:1xfh3ww9. 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-middleware.md.