Several limits¶
One rate on one route is rarely the whole story. This page covers rates that apply together, limits on many routes at once, and requests that should count for more than one.
Two rates at once¶
Give limit() several rates and a request has to fit all of them:
# Both rates apply: no more than 2 in a second, and no more than 4 in a minute.
@api.get("/items", dependencies=[Depends(limiter.limit("2/second", "4/minute"))])
async def items() -> dict[str, list[str]]:
return {"items": []}
A request that does not fit every rate is counted against none of them. The caller is told about the rate they have to wait longest for.
The whole app is in
examples/several_limits.py.
A whole router, or the whole app¶
A limit is a dependency, so it goes wherever FastAPI takes dependencies: on a
route, on an APIRouter, on include_router() or on FastAPI() itself.
# A limit on a router covers every route on it. The scope makes those routes
# draw on one allowance; without it each route would get an allowance of its own.
everything = limiter.limit("10/minute", scope="api")
api = APIRouter(prefix="/api", dependencies=[Depends(everything)])
@api.get("/status")
async def status() -> dict[str, str]:
return {"status": "ok"}
Each route counts on its own, unless you give the limit a scope¶
A limit keeps a separate count for each route it is attached to. The route is
identified by its method and its path as you declared it, with the prefix of
every router it is included in. So /items/1 and /items/2 share a count,
while GET /items and POST /items do not, and neither do /users/ and
/orders/ when two routers both declare /.
This means "10/minute" on a router gives every route ten a minute each. Give
the limit a scope when the routes should draw on one allowance, as the router
above does. A scope also lets you pick the routes yourself:
# Without a scope, each route would keep its own count. The scope makes
# every route this is attached to draw on one allowance per tenant.
quota = limiter.limit("4/minute", key=tenant_key, scope="tenant-quota")
@app.get("/invoices", dependencies=[Depends(quota)])
async def invoices() -> dict[str, list[str]]:
return {"invoices": []}
@app.get("/customers", dependencies=[Depends(quota)])
async def customers() -> dict[str, list[str]]:
return {"customers": []}
When several limits meet¶
A request can pass through more than one limit: one on the app, one on the router and one on the route. They are checked in that order.
Each limit() is all or nothing across its own rates, but separate limits are
separate steps. A request that an earlier limit allows and a later one turns
away has still been counted by the earlier one. Put rates that must stand or
fall together in one limit().
Two limits with the same rate that meet on one route are the same count, and
the request is added to it twice. Give one of them a scope, or drop one.
The response headers describe the limit the caller is closest to using up.
Requests that count for more¶
Some requests are heavier than others. cost says how much one counts for:
# An export is heavy, so each one counts as three requests: two a minute.
@api.post("/exports", dependencies=[Depends(limiter.limit("6/minute", cost=3))])
async def export() -> dict[str, str]:
return {"export": "started"}
Like the key and the rate, the cost can be a dependency: one that returns the number of items in a batch, for example.