Skip to main content

asgi-ratelimiter

PyPI Python License CI Ruff uv

Fixed-window rate limiting for ASGI apps. Use Depends with FastAPI or middleware with Starlette.

SQLite by default · Redis optional · Typed · Python 3.14+

Install

pip install "asgi-ratelimiter[fastapi]"           # FastAPI + SQLite
pip install "asgi-ratelimiter[fastapi,redis]"     # FastAPI + Redis
pip install "asgi-ratelimiter[starlette,redis]"   # Starlette + Redis

Or with uv: uv add "asgi-ratelimiter[fastapi]".

Extra Adds
fastapi FastAPI integration
starlette Starlette middleware
redis Redis backend
sqlite Marker only (stdlib)

Quick start

Define a rate:

from asgi_ratelimiter import Duration, Rate

rate = Rate(limit=5, interval=Duration.MINUTE)  # 5 / min
rate = Rate(limit=100, interval=Duration.HOUR * 2)  # 100 / 2h

FastAPI

from fastapi import Depends, FastAPI
from asgi_ratelimiter import Duration, Rate
from asgi_ratelimiter.fastapi import RateLimiter

app = FastAPI(
	dependencies=[Depends(RateLimiter(rate=Rate(limit=5, interval=Duration.MINUTE)))]
)


@app.get("/ping")
async def ping():
	return {"ok": True}

Over limit → 429 + Retry-After.

Starlette

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route
from asgi_ratelimiter import Duration, Rate
from asgi_ratelimiter.starlette import RateLimitMiddleware


async def homepage(request):
	return PlainTextResponse("ok")


app = Starlette(routes=[Route("/", homepage)])
app.add_middleware(
	RateLimitMiddleware,
	rate=Rate(limit=5, interval=Duration.MINUTE),
)

Redis

from redis.asyncio import Redis
from asgi_ratelimiter import Duration, Rate
from asgi_ratelimiter.backends.redis import RedisBackend
from asgi_ratelimiter.fastapi import RateLimiter

redis = Redis.from_url("redis://localhost:6379/0")
limiter = RateLimiter(
	rate=Rate(limit=5, interval=Duration.MINUTE),
	backend=RedisBackend(redis=redis),
)

Same backend= pattern works with Starlette middleware.

Logging

from asgi_ratelimiter import configure_logging, set_level

configure_logging(level="DEBUG")
set_level("WARNING")

Off until you call configure_logging / set_level (loguru).

Examples

File Stack
examples/fastapi/fastapi_sqlite.py FastAPI + SQLite
examples/fastapi/fastapi_redis.py FastAPI + Redis
examples/starlette/starlette_sqlite.py Starlette + SQLite
examples/starlette/starlette_redis.py Starlette + Redis
uv sync --group dev
uv run --with "uvicorn[standard]" python examples/fastapi/fastapi_sqlite.py

Development

uv sync --group dev
uv run pytest
uv run ruff check .
uv run ruff format .

See CONTRIBUTING.md and AGENTS.md.

License

AGPL-3.0-or-later · Changelog · Issues

Metadata

Release files for asgi-ratelimiter 0.3.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 asgi-ratelimiter 0.3.1
File Size Uploaded Provenance
asgi_ratelimiter-0.3.1.tar.gz 48.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for asgi-ratelimiter 0.3.1
File Interpreter ABI Platform
asgi_ratelimiter-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 73.4 kB

Release files / asgi_ratelimiter-0.3.1.tar.gz

Download URL asgi_ratelimiter-0.3.1.tar.gz
Size 48.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bf77b2786d4ac9e75e0a992c91cea3fd7f5caf23af7b8cd8665f3aaa99963f64
BLAKE2b-256 checksum
How to use checksums
33463b60ed1faff77ca2ed88fe88f8b86d63b71fb693b2ddd1ef0ac32a2e2ed2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

Release files / asgi_ratelimiter-0.3.1-py3-none-any.whl

Download URL asgi_ratelimiter-0.3.1-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fd432503beeb57864397eb292bcf01ecc19a4cb95799fb6dca5476214643810
BLAKE2b-256 checksum
How to use checksums
e88a9e847c68c62c566f9205a8ee3e2aa8f04817cedb9020964ca1970b90fa16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

Security for asgi-ratelimiter 0.3.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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for asgi_ratelimiter-0.3.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

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.

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