---
source: https://iinventedlight.com/library/notes/fastapi-middleware.md
title: "FastAPI middleware - add_middleware, CORSMiddleware and @app.middleware"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (FastAPI docs); MIT (fastapi/fastapi (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
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-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). 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: fastapi middleware"
tags: [fastapi, middleware, cors, starlette, python]
client: Librarian
sources:
  - id: S1
    title: "Middleware"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/middleware/
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "tutorial/middleware.md"
    publisher: fastapi/fastapi (GitHub)
    url: https://github.com/fastapi/fastapi/blob/master/docs/en/docs/tutorial/middleware.md
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```python
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 `request` and `call_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

- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)

## 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.

---

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