Skip to main content

A simple rate limiting library for FastAPI using in-memory or Redis storage

Project description

fastapi-rate-limiter

License: MIT

A simple and flexible rate limiting library for FastAPI with support for in-memory and Redis storage.


Why Use fastapi-rate-limiter?

Rate limiting is highly essential for cyber-security and protecting your FastAPI application from abuse, brute-force attacks, and high traffic spikes. This library provides:

  • Easy global and per-route limits
  • IP-based isolation
  • Async-safe implementation
  • Option to use Redis for multi-instance deployments
  • Lightweight, easy to integrate

Features

  • Global rate limiting middleware
  • Per-route rate limiting via dependency
  • IP-based rate isolation
  • Configurable time windows and request limits
  • In-memory or Redis storage backend
  • Async-safe for FastAPI endpoints

Installation

Install from PyPI:

pip install fastapi-rate-limiter

Or install directly from GitHub:

pip install git+https://github.com/yourusername/fastapi-rate-limiter.git

How to Use

1. Setup the Rate Limiter

from fastapi import FastAPI
from fastapi_rate_limiter import RateLimiter
from fastapi_rate_limiter.storage import MemoryStorage

app = FastAPI()

# Initialize limiter with MemoryStorage
app.state.limiter = RateLimiter(storage=MemoryStorage(), default_rate="3/min")

Why: Provides the core limiter logic and a storage backend for counting requests. Attaching it to app.state.limiter makes it accessible everywhere in the app.


2. Apply Global Middleware

from fastapi_rate_limiter import rate_limit_middleware

rate_limit_middleware(app, rate="10/min")

Why: Protects the app with a global limit to prevent traffic spikes or abuse, independent of per-route settings.


3. Per-Route Rate Limiting

from fastapi import Depends
from fastapi_rate_limiter import rate_limit_dependency

@app.get("/login", dependencies=[Depends(rate_limit_dependency("2/min"))])
async def login():
    return {"msg": "login"}

Why: Some routes (e.g. login endpoints) need stricter limits than the global middleware. Dependencies allow fine-grained control. These can help prevent brute force attacks.


4. IP-Based Isolation

  • Each client IP has independent counters.

Why: Prevents one client from exhausting the rate limit for everyone else.


5. Using Redis for Multi-Instance Apps

from fastapi_rate_limiter.storage import RedisStorage
from redis.asyncio import Redis

redis_client = Redis(host="localhost", port=6379, db=0)
app.state.limiter = RateLimiter(storage=RedisStorage(redis_client), default_rate="3/min")

Why: Memory storage only works per instance. Redis ensures limits are shared across multiple servers.

Note: This uses Redis to replace local memory and can be ignored if you don't use multiple servers.


Testing Your Setup

pytest -v

Tests ensure:

  • Global and per-route limits
  • IP isolation
  • Concurrency handling
  • Window resets

Contributing

Contributions are welcome! Steps:

  1. Fork the repo
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Commit changes (git commit -m 'Add feature')
  4. Push to the branch (git push origin feature/your-feature)
  5. Open a pull request

Or email me at whitb20@yahoo.com.au for suggestions.


License

This project is licensed under the MIT License.


Planned Improvements

  • API key-based rate limiting
  • Detailed examples and tutorials
  • Extended test coverage and edge cases

Links

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_rate_limiter_benw17-0.1.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

fastapi_rate_limiter_benw17-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_rate_limiter_benw17-0.1.0.tar.gz.

File metadata

File hashes

Hashes for fastapi_rate_limiter_benw17-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5b830d61d4f12ed00839788d985895c562210601bbdaeee64c05e821ab298d64
MD5 92d1898ae7a74ea3d8c13ab080943baa
BLAKE2b-256 37e17084d47907c54b70f8414d8030cc717c663fef66959363ae7222ccea604d

See more details on using hashes here.

File details

Details for the file fastapi_rate_limiter_benw17-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_rate_limiter_benw17-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3f26c54b0e9ee400c999cf6fe0cbd8e2ba17a0252b8d6e1f3bac4d2feb0e4e4
MD5 adee88d6a66c0326abd52c26c5633344
BLAKE2b-256 0486e6fbfbf569d1a55432b5cda356c0d8f3f580eaa814fe3347f93f4eefb3d0

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