Middleware in FastAPI is code that runs for every request and response — wrapping your endpoints to handle cross-cutting concerns like logging, timing, CORS, and adding headers. FastAPI uses Starlette's middleware system (it's ASGI-based).
Writing custom middleware
time
():
start = time.time()
response = call_next(request)
response.headers[] = (time.time() - start)
response
