Skip to main content

Simple FastAPI rate limiter

Project description

simplelimiter

A simple FastAPI rate limiter

Requirements

Redis

How to use

Install the package

pip install simplelimiter

Example

import redis

from fastapi import FastAPI, APIRouter, Request
from fastapi.params import Depends
from simplelimiter import Limiter


app = FastAPI()
router = APIRouter()

# We initialize the Limiter on the app startup event
@app.on_event("startup")
async def startup():
    r = redis.from_url("redis://localhost", encoding="utf-8", decode_responses=True)
    Limiter.init(redis_instance=r, debug=True)

    return app


# We pass the Limiter as a dependencie
@router.get("/", dependencies=[Depends(Limiter("5/minute"))])
def base_route(request: Request):
    return {"response": "ok"}


app.include_router(router)

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

simplelimiter-0.1.4.tar.gz (2.7 kB view hashes)

Uploaded Source

Built Distribution

simplelimiter-0.1.4-py3-none-any.whl (3.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page