{"id":5925,"library":"fastapi-limiter","title":"FastAPI Limiter","description":"fastapi-limiter is a Python library that provides robust request rate limiting capabilities for FastAPI applications. It integrates with Redis as a backend to store and manage rate limit counters. Currently at version 0.2.0, it's an early-stage project with releases driven by feature additions and bug fixes, and primarily supports Python 3.9 and above.","status":"active","version":"0.2.0","language":"en","source_language":"en","source_url":"https://github.com/long2ice/fastapi-limiter","tags":["fastapi","rate-limiting","redis","security","async"],"install":[{"cmd":"pip install fastapi-limiter redis","lang":"bash","label":"Install FastAPI Limiter with Redis client"}],"dependencies":[{"reason":"The `redis` library (specifically `redis>=4.0` for asyncio support) is required to provide the Redis client functionality, which serves as the backend for all rate limiting operations.","package":"redis","optional":false}],"imports":[{"symbol":"FastAPILimiter","correct":"from fastapi_limiter import FastAPILimiter"},{"symbol":"RateLimiter","correct":"from fastapi_limiter.depends import RateLimiter"},{"note":"For asynchronous FastAPI applications, ensure you import the asyncio-compatible Redis client (`redis.asyncio.Redis`) to prevent blocking the event loop. The synchronous client is only appropriate for blocking contexts.","wrong":"from redis import Redis","symbol":"Redis","correct":"from redis.asyncio import Redis"}],"quickstart":{"code":"import os\nfrom fastapi import FastAPI, Depends\nfrom fastapi_limiter import FastAPILimiter\nfrom fastapi_limiter.depends import RateLimiter\nfrom redis.asyncio import Redis # Use asyncio for FastAPI\n\napp = FastAPI()\n\n# Configure Redis URL via environment variable or default to localhost\nREDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379')\n\n@app.on_event('startup')\nasync def startup():\n    redis_client = Redis.from_url(REDIS_URL, encoding='utf8', decode_responses=True)\n    await FastAPILimiter.init(redis_client)\n    print(\"FastAPILimiter initialized.\")\n\n@app.get('/', dependencies=[Depends(RateLimiter(times=2, seconds=5))])\nasync def homepage():\n    return {'message': 'Hello world!'}\n\n@app.get('/limited', dependencies=[Depends(RateLimiter(times=1, seconds=10))])\nasync def limited_endpoint():\n    return {'message': 'This endpoint is more limited!'}\n\n# To run this example:\n# 1. Install uvicorn: pip install uvicorn\n# 2. Run Redis locally (e.g., via Docker: docker run --name some-redis -p 6379:6379 -d redis)\n# 3. Save the code as main.py\n# 4. Execute: uvicorn main:app --reload\n","lang":"python","description":"This quickstart demonstrates how to initialize `fastapi-limiter` with an async Redis client during FastAPI's application startup event. It then applies different rate limits to two distinct endpoints using the `RateLimiter` dependency. The Redis connection uses an environment variable for configuration."},"warnings":[{"fix":"Ensure `FastAPILimiter.init` is properly awaited within an asynchronous startup function linked to `@app.on_event('startup')`.","message":"You must explicitly call `await FastAPILimiter.init(redis_client)` during your FastAPI application's startup event (e.g., in `@app.on_event('startup')`). Failing to do so will result in the rate limiter not being initialized and will cause errors when endpoints attempt to use it.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Import `Redis` from `redis.asyncio` and ensure all Redis client operations are `await`ed.","message":"For async FastAPI applications, always use `redis.asyncio.Redis` (or methods like `Redis.from_url` from `redis.asyncio`) for your Redis client. Using the synchronous `redis.Redis` directly in an `async def` context will block the event loop, leading to performance degradation and concurrency issues.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"It is highly advisable to pin your dependency to an exact version (e.g., `fastapi-limiter==0.2.0`) and carefully review changelogs or GitHub releases before upgrading.","message":"As a 0.x.x version library, `fastapi-limiter` may introduce breaking changes in minor or patch releases without strictly adhering to semantic versioning. The API surface may evolve rapidly.","severity":"breaking","affected_versions":"All 0.x.x versions"},{"fix":"Ensure you include `redis` in your project's dependencies and install it (e.g., `pip install redis`).","message":"The `redis` library, which provides the Redis client, is a peer dependency and not automatically installed alongside `fastapi-limiter`. You must install it separately for the library to function.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}