An open API service indexing awesome lists of open source software.

https://github.com/glef1x/fastapi-ratelimiter

🏄🏼‍♂️ Redis-based rate-limiting for FastAPI
https://github.com/glef1x/fastapi-ratelimiter

asgi asgi-middleware asyncio fastapi ratelimit redis

Last synced: 10 days ago
JSON representation

🏄🏼‍♂️ Redis-based rate-limiting for FastAPI

Awesome Lists containing this project

README

        

# FastAPI Ratelimiter

[![PyPI version](https://img.shields.io/pypi/v/fastapi-ratelimiter.svg)]

**Documentation**: https://fastapi-ratelimit.readthedocs.io/en/latest/

## Quick start:

```python

import asyncio

import aioredis
import uvicorn
from fastapi import FastAPI, Depends
from starlette.responses import JSONResponse

from fastapi_ratelimiter import RateLimited, RedisDependencyMarker
from fastapi_ratelimiter.strategies import BucketingRateLimitStrategy

app = FastAPI()
redis = aioredis.from_url("redis://localhost")

@app.get(
"/some_expensive_call", response_class=JSONResponse,
dependencies=[
Depends(RateLimited(BucketingRateLimitStrategy(rate="10/60s")))
]
)
async def handle_test_endpoint():
await asyncio.sleep(5)
return {"hello": "world"}

app.dependency_overrides[RedisDependencyMarker] = lambda: redis

if __name__ == '__main__':
uvicorn.run(app)
```