In web frameworks and scraping tools, middleware is a chain of components through which every request and response passes. Each middleware component can inspect or modify the request before it is sent, and the response after it is received. Middleware is ideal for cross-cutting concerns that apply uniformly across all requests: adding authentication headers, logging request metadata, measuring latency, rotating proxies, decompressing responses, or applying retry logic.
Scrapy's spider middleware and downloader middleware pipelines are a canonical example. Scrapy passes each request through the downloader middleware chain before sending it and passes each response through the same chain in reverse on the way back, allowing middleware to transform both requests and responses declaratively.
In FastAPI (used by AlterLab's API layer), middleware intercepts HTTP requests and responses, enabling authentication checking, request ID injection, CORS headers, and timing instrumentation without modifying individual route handlers.