Skip to content

First steps

This page puts one limit on one route and shows what a caller sees. It takes about five minutes.

Install

pip install "paceapi>=0.1.0a1"

With uv, uv add "paceapi>=0.1.0a1". Naming the alpha version is what lets pip and uv choose a pre-release.

paceapi needs Python 3.12 or newer. Its only dependency is Starlette, 0.44 or newer. The limits on this page use the FastAPI your app already has, which needs to be 0.115.7 or newer.

If that version cannot be found

0.1.0a1 is the first release with rate limiting in it. Until it is published, install from the repository: pip install git+https://github.com/rushilsrivastava/paceapi.

One limit on one route

from fastapi import Depends, FastAPI

from paceapi import Limiter

limiter = Limiter()
app = FastAPI()


@app.get("/search", dependencies=[Depends(limiter.limit("5/minute"))])
async def search(q: str = "") -> dict[str, str]:
    return {"query": q}

Three things happen here:

  1. Limiter() holds the settings and keeps the counts. Make one for your app and use it for every limit.
  2. limiter.limit("5/minute") makes a FastAPI dependency out of a rate.
  3. dependencies=[Depends(...)] attaches it to the route. It runs before the endpoint, and the endpoint does not run for a request that is over the limit.

No key was given, so callers are told apart by their network address. Keys covers what to do behind a proxy or a Unix socket, and Limiting by user counts by account instead.

What the caller sees

A request within the limit gets its normal response, with three headers that say where the caller stands:

HTTP/1.1 200 OK
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 4
X-RateLimit-Reset: 93

The sixth request in a minute is turned away:

HTTP/1.1 429 Too Many Requests
Retry-After: 45
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 93

{"detail": "Rate limit exceeded. Try again in 45 seconds."}

Headers and errors explains each header and how to change the response.

Writing a rate

You write It means
"5/minute" 5 every minute
"100 per hour" 100 every hour
"10/30 seconds" 10 every 30 seconds
Rate(10, 30 * SECOND) The same, as an object

The units are second, minute, hour and day. A rate that cannot be read raises InvalidRateError at the line that calls limit(), so a typo stops the app from starting. It does not wait for the first request.

How requests are counted

By default a limit uses a sliding window: at any moment it allows about the limit over the period that ends now. Fixed windows and a token bucket are also available through Limiter(algorithm=...). Algorithms compares the three.

Where the counts are kept

The counts are kept in the memory of the process. That is the right choice for development, for tests and for an app that runs as one process. It has two consequences when you run more than one:

  • Each process keeps its own counts. With four workers a caller can get up to four times the limit.
  • The counts are gone when the process restarts.

When that matters, keep the counts in Redis, which every process shares. Stores shows how; it is one line.