fastapi-cap


Namefastapi-cap JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryA robust, extensible, and production-ready rate limiting library for FastAPI, supporting multiple algorithms and Redis backend.
upload_time2025-07-16 06:08:05
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords fastapi rate-limiter redis api throttling limiter web asgi
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 🚦 FastAPI-Cap

**FastAPI-Cap** is a robust, extensible, and high-performance rate limiting library for [FastAPI](https://fastapi.tiangolo.com/) 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

```bash
pip install fastapi-cap
```

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

```bash
docker run -p 6379:6379 redis
```

---

## 🚀 Quick Start

### 1. Initialize Redis Connection

```python
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

```python
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

```python
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](https://devbijay.github.io/FastAPI-Cap/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](https://devbijay.github.io/FastAPI-Cap/quickstart/) for details.

---

## 📚 Documentation

- [Quickstart Guide](https://devbijay.github.io/FastAPI-Cap/quickstart/)
- [Strategy Overview](https://devbijay.github.io/FastAPI-Cap/strategies/overview/)
- [Fixed Window](https://devbijay.github.io/FastAPI-Cap/strategies/fixed_window/)
- [Sliding Window (Approximated)](https://devbijay.github.io/FastAPI-Cap/strategies/sliding_window/)
- [Sliding Window (Log-based)](https://devbijay.github.io/FastAPI-Cap/strategies/sliding_window_log/)
- [Token Bucket](https://devbijay.github.io/FastAPI-Cap/strategies/token_bucket/)
- [Leaky Bucket](https://devbijay.github.io/FastAPI-Cap/strategies/leaky_bucket/)
- [GCRA](https://devbijay.github.io/FastAPI-Cap/strategies/gcra/)

---

## 🛡️ License

**MIT License**

---

## 🤝 Contributing

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

---



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fastapi-cap",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "fastapi, rate-limiter, redis, api, throttling, limiter, web, asgi",
    "author": null,
    "author_email": "Bijay Nayak <bijay6779@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/05/0c/14060d1fb38d99447ab5b772bef97e967eab5168f6d15598af6ba7065df0/fastapi_cap-0.2.0.tar.gz",
    "platform": null,
    "description": "# \ud83d\udea6 FastAPI-Cap\n\n**FastAPI-Cap** is a robust, extensible, and high-performance rate limiting library for [FastAPI](https://fastapi.tiangolo.com/) applications.  \nIt 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.\n\n---\n\n## \u2728 Features\n\n- **Multiple Algorithms:** Fixed Window, Sliding Window (approximated & log-based), Token Bucket, Leaky Bucket, and GCRA.\n- **High Performance:** Atomic operations via Redis Lua scripts.\n- **Distributed:** Consistent limits across multiple API instances.\n- **Easy Integration:** Use as FastAPI dependencies or decorators.\n- **Customizable:** Plug in your own key extraction and limit handling logic.\n- **Battle-tested:** Designed for real-world, production-grade FastAPI services.\n\n---\n\n## \ud83d\udce6 Installation\n\n```bash\npip install fastapi-cap\n```\n\nYou also need a running Redis instance.  \nFor local development, you can use Docker:\n\n```bash\ndocker run -p 6379:6379 redis\n```\n\n---\n\n## \ud83d\ude80 Quick Start\n\n### 1. Initialize Redis Connection\n\n```python\nfrom fastapi import FastAPI\nfrom fastapicap import Cap\n\napp = FastAPI()\nCap.init_app(\"redis://localhost:6379/0\")\n```\n\n### 2. Apply a Rate Limiter to a Route\n\n```python\nfrom fastapicap import RateLimiter\nfrom fastapi import Depends\n\nlimiter = RateLimiter(limit=5, minutes=1)\n\n@app.get(\"/limited\", dependencies=[Depends(limiter)])\nasync def limited_endpoint():\n    return {\"message\": \"You are within the rate limit!\"}\n```\n\n### 3. Combine Multiple Limiters\n\n```python\nlimiter_1s = RateLimiter(limit=1, seconds=1)\nlimiter_30m = RateLimiter(limit=30, minutes=1)\n\n@app.get(\"/strict\", dependencies=[Depends(limiter_1s), Depends(limiter_30m)])\nasync def strict_endpoint():\n    return {\"message\": \"You passed both rate limits!\"}\n```\n\n---\n\n## \ud83e\udde9 Supported Algorithms\n\n- **Fixed Window:** Simple, aggressive limits.\n- **Sliding Window (Approximated):** Smoother than fixed window, more efficient than log-based.\n- **Sliding Window (Log-based):** Most accurate and fair, eliminates burst issues.\n- **Token Bucket:** Allows bursts, enforces average rate.\n- **Leaky Bucket:** Smooths out bursts, enforces constant output rate.\n- **GCRA:** Precise, fair, and burstable rate limiting.\n\nSee the [strategies overview](https://devbijay.github.io/FastAPI-Cap/strategies/overview/) for details and usage examples.\n\n---\n\n## \u2699\ufe0f Customization\n\n- **Custom Key Function:** Rate limit by user ID, API key, etc.\n- **Custom on_limit Handler:** Return custom responses, log events, etc.\n\nSee [Quickstart](https://devbijay.github.io/FastAPI-Cap/quickstart/) for details.\n\n---\n\n## \ud83d\udcda Documentation\n\n- [Quickstart Guide](https://devbijay.github.io/FastAPI-Cap/quickstart/)\n- [Strategy Overview](https://devbijay.github.io/FastAPI-Cap/strategies/overview/)\n- [Fixed Window](https://devbijay.github.io/FastAPI-Cap/strategies/fixed_window/)\n- [Sliding Window (Approximated)](https://devbijay.github.io/FastAPI-Cap/strategies/sliding_window/)\n- [Sliding Window (Log-based)](https://devbijay.github.io/FastAPI-Cap/strategies/sliding_window_log/)\n- [Token Bucket](https://devbijay.github.io/FastAPI-Cap/strategies/token_bucket/)\n- [Leaky Bucket](https://devbijay.github.io/FastAPI-Cap/strategies/leaky_bucket/)\n- [GCRA](https://devbijay.github.io/FastAPI-Cap/strategies/gcra/)\n\n---\n\n## \ud83d\udee1\ufe0f License\n\n**MIT License**\n\n---\n\n## \ud83e\udd1d Contributing\n\nContributions, bug reports, and feature requests are welcome!  \nPlease open an issue or submit a pull request.\n\n---\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A robust, extensible, and production-ready rate limiting library for FastAPI, supporting multiple algorithms and Redis backend.",
    "version": "0.2.0",
    "project_urls": {
        "Documentation": "https://devbijay.github.io/FastAPI-Cap/",
        "Homepage": "https://github.com/devbijay/FastAPI-Cap",
        "Issues": "https://github.com/devbijay/FastAPI-Cap/issues",
        "Repository": "https://github.com/devbijay/FastAPI-Cap.git"
    },
    "split_keywords": [
        "fastapi",
        " rate-limiter",
        " redis",
        " api",
        " throttling",
        " limiter",
        " web",
        " asgi"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "d8335d8376ef6755a4e9c5b2c840962ecd1534d5f541f7ed530641437467ad24",
                "md5": "7cea7d026042f84edc5065fe3cea16b4",
                "sha256": "fc1cd9b9a16c52970256456c7dc32c78859dfdf038844cd49a4ec6c94d03158f"
            },
            "downloads": -1,
            "filename": "fastapi_cap-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7cea7d026042f84edc5065fe3cea16b4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 22435,
            "upload_time": "2025-07-16T06:08:04",
            "upload_time_iso_8601": "2025-07-16T06:08:04.412678Z",
            "url": "https://files.pythonhosted.org/packages/d8/33/5d8376ef6755a4e9c5b2c840962ecd1534d5f541f7ed530641437467ad24/fastapi_cap-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "050c14060d1fb38d99447ab5b772bef97e967eab5168f6d15598af6ba7065df0",
                "md5": "82e4b800f72a814b90300ba7f7c0c495",
                "sha256": "8bf0d529a2d29ab955faacf63543fcbb96a6413a48c54237082ef66a34456b33"
            },
            "downloads": -1,
            "filename": "fastapi_cap-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "82e4b800f72a814b90300ba7f7c0c495",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 14079,
            "upload_time": "2025-07-16T06:08:05",
            "upload_time_iso_8601": "2025-07-16T06:08:05.417850Z",
            "url": "https://files.pythonhosted.org/packages/05/0c/14060d1fb38d99447ab5b772bef97e967eab5168f6d15598af6ba7065df0/fastapi_cap-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-16 06:08:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "devbijay",
    "github_project": "FastAPI-Cap",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "fastapi-cap"
}
        
Elapsed time: 1.35825s