Skip to main content

A robust, extensible, and production-ready rate limiting library for FastAPI, supporting multiple algorithms and Redis backend.

Project description

🚦 FastAPI-Cap

FastAPI-Cap is a robust, extensible, and high-performance rate limiting library for FastAPI applications.
It leverages Redis and optimized Lua scripts to provide a suite of industry-standard algorithms for controlling API traffic, preventing abuse, and ensuring reliable service delivery.


✨ Features

  • Multiple Algorithms: Fixed Window, Sliding Window (approximated & log-based), Token Bucket, Leaky Bucket, and GCRA.
  • High Performance: Atomic operations via Redis Lua scripts.
  • Distributed: Consistent limits across multiple API instances.
  • Easy Integration: Use as FastAPI dependencies or decorators.
  • Customizable: Plug in your own key extraction and limit handling logic.
  • Battle-tested: Designed for real-world, production-grade FastAPI services.

📦 Installation

pip install fastapicap

You also need a running Redis instance.
For local development, you can use Docker:

docker run -p 6379:6379 redis

🚀 Quick Start

1. Initialize Redis Connection

from fastapi import FastAPI
from fastapicap import Cap

app = FastAPI()
Cap.init_app("redis://localhost:6379/0")

2. Apply a Rate Limiter to a Route

from fastapicap import RateLimiter
from fastapi import Depends

limiter = RateLimiter(limit=5, minutes=1)

@app.get("/limited", dependencies=[Depends(limiter)])
async def limited_endpoint():
    return {"message": "You are within the rate limit!"}

3. Combine Multiple Limiters

limiter_1s = RateLimiter(limit=1, seconds=1)
limiter_30m = RateLimiter(limit=30, minutes=1)

@app.get("/strict", dependencies=[Depends(limiter_1s), Depends(limiter_30m)])
async def strict_endpoint():
    return {"message": "You passed both rate limits!"}

🧩 Supported Algorithms

  • Fixed Window: Simple, aggressive limits.
  • Sliding Window (Approximated): Smoother than fixed window, more efficient than log-based.
  • Sliding Window (Log-based): Most accurate and fair, eliminates burst issues.
  • Token Bucket: Allows bursts, enforces average rate.
  • Leaky Bucket: Smooths out bursts, enforces constant output rate.
  • GCRA: Precise, fair, and burstable rate limiting.

See the strategies overview for details and usage examples.


⚙️ Customization

  • Custom Key Function: Rate limit by user ID, API key, etc.
  • Custom on_limit Handler: Return custom responses, log events, etc.

See Quickstart for details.


📚 Documentation


🛡️ License

MIT License


🤝 Contributing

Contributions, bug reports, and feature requests are welcome!
Please open an issue or submit a pull request.


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

Uploaded Source

Built Distribution

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

fastapi_cap-0.1.0-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_cap-0.1.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.15

File hashes

Hashes for fastapi_cap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e1e1d3e825d40154fbbb34ad99a7f1ac5f27443971c49928723cd7f278dc1629
MD5 e19e5c11b841a5bf215e6cbf426b40e3
BLAKE2b-256 5ff328a4c0fbb62ea8624904a3dfa6d013bff89915a3447f4361da83fa9a4da7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_cap-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7432396777a064453d9416ea6eba88d66135f747b780c71a5285858e0eded89
MD5 a274afdc0657a8008a5be3c49c730a10
BLAKE2b-256 0edd2e898c07a23c53d28d716606464464199b62bb3bbfb6e01498781b3bc857

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