Skip to main content

🚦 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


🛡️ License

MIT License


🤝 Contributing

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


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.2.0.tar.gz (14.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.2.0-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fastapi_cap-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8bf0d529a2d29ab955faacf63543fcbb96a6413a48c54237082ef66a34456b33
MD5 82e4b800f72a814b90300ba7f7c0c495
BLAKE2b-256 050c14060d1fb38d99447ab5b772bef97e967eab5168f6d15598af6ba7065df0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_cap-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc1cd9b9a16c52970256456c7dc32c78859dfdf038844cd49a4ec6c94d03158f
MD5 7cea7d026042f84edc5065fe3cea16b4
BLAKE2b-256 d8335d8376ef6755a4e9c5b2c840962ecd1534d5f541f7ed530641437467ad24

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.0

2 files

Supported by

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