Skip to content

rate_limit

rate_limit

In-memory rate limiter for FastAPI.

RateLimiter

Simple in-memory rate limiter.

Tracks request timestamps per key and enforces a maximum number of requests within a sliding time window. Not suitable for multi-process deployments but sufficient for single-worker Uvicorn on Render free tier.

__init__

__init__(max_requests: int, window_seconds: int)

Initialize the rate limiter.

Parameters:

Name Type Description Default
max_requests int

Maximum requests allowed within the window.

required
window_seconds int

Sliding time window in seconds.

required

__call__

__call__(key: str) -> None

Check and enforce the rate limit for a given key.

Evicts expired timestamps from the sliding window, then checks whether the current request exceeds the limit.

Parameters:

Name Type Description Default
key str

Identifier to rate-limit against (e.g., client IP).

required

Raises:

Type Description
HTTPException

429 Too Many Requests if the limit is exceeded.

rate_limit_auth async

rate_limit_auth(request: Request)

Rate limit auth endpoints by client IP.

Limits requests to 5 per 60-second sliding window per IP address. Used as a FastAPI dependency on /auth/login and /auth/refresh.

Parameters:

Name Type Description Default
request Request

The incoming FastAPI Request.

required

Raises:

Type Description
HTTPException

429 if the rate limit is exceeded.