🚦 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 fastapi-cap
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
- Quickstart Guide
- Strategy Overview
- Fixed Window
- Sliding Window (Approximated)
- Sliding Window (Log-based)
- Token Bucket
- Leaky Bucket
- GCRA
🛡️ License
MIT License
🤝 Contributing
Contributions, bug reports, and feature requests are welcome!
Please open an issue or submit a pull request.
Metadata
Release files for fastapi-cap 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_cap-0.2.0.tar.gz | 14.1 kB | Without provenance | Details for fastapi_cap-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_cap-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_cap-0.2.0-py3-none-any.whl |
Total release size: 36.5 kB
Release files / fastapi_cap-0.2.0.tar.gz
| Download URL | fastapi_cap-0.2.0.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8bf0d529a2d29ab955faacf63543fcbb96a6413a48c54237082ef66a34456b33
|
|
BLAKE2b-256 checksum How to use checksums |
050c14060d1fb38d99447ab5b772bef97e967eab5168f6d15598af6ba7065df0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.21
|
Release files / fastapi_cap-0.2.0-py3-none-any.whl
| Download URL | fastapi_cap-0.2.0-py3-none-any.whl |
|---|---|
| Size | 22.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fc1cd9b9a16c52970256456c7dc32c78859dfdf038844cd49a4ec6c94d03158f
|
|
BLAKE2b-256 checksum How to use checksums |
d8335d8376ef6755a4e9c5b2c840962ecd1534d5f541f7ed530641437467ad24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.21
|
Security for fastapi-cap 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.