Skip to main content

High-performance async rate limiter for FastAPI with Redis or native in-memory backend

Project description

fastapi-easylimiter

Simple ASGI async rate-limiting middleware for FastAPI with Redis or in-memory caching.

Simple design

  • Async rate limiting
  • Cache
    • Redis
    • In-Memory (single worker dev)
  • Path Based Rules
  • Multi-rule prefix matching
    • Can do global ratelimits and per-route
  • Standard rate-limit headers
    • X-RateLimit-Limit
    • X-RateLimit-Remaining
    • X-RateLimit-Reset
    • Retry-After on 429 responses
  • Proxy Aware
    • Uses 'X-Forwarded-For' only when the sender is trusted
    • Rejects spoofed XFF headers

Installation

pip install fastapi-easylimiter

Usage

from fastapi import FastAPI
from fastapi_easylimiter import AsyncRedisBackend, InMemoryBackend, RateLimiterMiddleware
import redis.asyncio as redis_async

app = FastAPI()

REDIS_URL = "redis://localhost:6379/0"

# Redis backend (recommended for multi-instance deployments)
redis_client = redis_async.from_url(REDIS_URL, decode_responses=True)
backend = AsyncRedisBackend(redis_client)

# Or for single-instance/local development:
# backend = InMemoryBackend()

rules = {
    "/api/": {"limit": 60, "period": 60},
    "/api/users": {"limit": 1, "period": 2},
}

app.add_middleware(
    RateLimiterMiddleware,
    rules=rules,
    backend=backend,
    trusted_proxies=["127.0.0.1"]
)

Rules are automatically sorted longest-first as seen in the code example.

A request to /api/users/me will match:

  • /api/users
  • /api

Both rules count independently.

If ANY rule is exceeded → request becomes 429.

Contributing

Contributions and forks are always welcome! Feel free to adapt, improve, or extend this middleware for your own needs. This was purely made out of personal necessity.

Support

Buy Me a Coffee

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_easylimiter-0.2.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_easylimiter-0.2.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_easylimiter-0.2.2.tar.gz.

File metadata

  • Download URL: fastapi_easylimiter-0.2.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for fastapi_easylimiter-0.2.2.tar.gz
Algorithm Hash digest
SHA256 47f0789d60cadf794ed6481416c03a5822ee8e32f8caaa99bc379e3bcefab455
MD5 8372b9986508775264c753d891532bb6
BLAKE2b-256 1bf8603f087a3a824226db9e7dac804666e12ba4ed6efee852cd88619ba128ea

See more details on using hashes here.

File details

Details for the file fastapi_easylimiter-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_easylimiter-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e0fc214f6ee8c19252960650e5362e9e88a6c692af8893e46e06c1e15c71bd76
MD5 e32378266abcfd4b70f3d1b72425db9c
BLAKE2b-256 ced5bfff41295602827aa8d5a6286b4f9b60066be13b8bc258a0d7a163fb24b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page