Skip to main content

FastAPI Rate Limiter Middleware

Project Description

This project implements a Rate Limiting Middleware for a FastAPI application. The middleware restricts how many requests a client (identified by IP address) can make within a defined time window. It helps protect your API from abuse, excessive traffic, and denial-of-service (DoS) attacks by throttling requests at the middleware level before they reach the main application logic.


Key Features

  • 🔒 Per-IP Rate Limiting: Limits each unique IP address to a fixed number of requests within a given time window.
  • ⏱️ Configurable Limits: Easily set the number of allowed requests and time window (e.g., 5 requests per 60 seconds).
  • 🧠 Smart Storage with Redis: Uses Redis to efficiently store and manage request counters with TTL (time-to-live).
  • 📉 Fail-Open Strategy: If Redis is unavailable, the middleware gracefully lets requests through to avoid blocking legitimate users.
  • 🔄 Auto Reset: Counters reset automatically after the time window expires.
  • 📦 Reusable Component: Designed as a plug-and-play middleware class for any FastAPI project.

How It Works

  1. When a request comes in, the middleware checks the client’s IP address.
  2. It creates or updates a Redis key that tracks the number of requests from that IP.
  3. If the request count exceeds the allowed limit:
    • The request is rejected with HTTP 429 Too Many Requests.
    • A message is returned including the retry_after_seconds field indicating how long to wait before retrying.
  4. If under the limit, the request proceeds normally to the API endpoint.

Tech Stack

Technology Purpose
FastAPI Web framework for building APIs
Redis In-memory data store for counters
Uvicorn ASGI server to run the FastAPI app
Starlette Base for FastAPI and middleware

Example

# main.py
from fastapi import FastAPI
from fastapi_rate_limiter import RateLimiterMiddleware

app = FastAPI()

# Add middleware with Redis URL
app.add_middleware(
    RateLimiterMiddleware,
    redis_url="redis://localhost:6379",  # Adjust to your Redis config
    max_requests=5,
    window_seconds=60
)

@app.get("/")
async def root():
    return {"message": "Hello, World!"}

Metadata

Release files for fastapi_rlr 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi_rlr 0.1.1
File Size Uploaded Provenance Actions
fastapi_rlr-0.1.1.tar.gz 2.5 kB Without provenance Details for fastapi_rlr-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi_rlr 0.1.1
File Interpreter ABI Platform Actions
fastapi_rlr-0.1.1-py3-none-any.whl Python 3 none any Details for fastapi_rlr-0.1.1-py3-none-any.whl

Total release size: 5.7 kB

Release files / fastapi_rlr-0.1.1.tar.gz

Download URL fastapi_rlr-0.1.1.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ffdf839262516edcca01efb6b4210c19847212a5eed34bfc2fc3ef54db1afa63
BLAKE2b-256 checksum
How to use checksums
eb070a58f5200c640f802d3c731da60804a505360636c1926f29dbf25ba580c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.21

Release files / fastapi_rlr-0.1.1-py3-none-any.whl

Download URL fastapi_rlr-0.1.1-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99c0fd72ba6fde4fc7b41e8cf1bc0559d639333e676d2a7dc2f52b2014c2d1ac
BLAKE2b-256 checksum
How to use checksums
b8263b20a78c899880ef7ec781bd5552f6dcb5522c239202f20da35c9453c9fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.21

Security for fastapi_rlr 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page