Skip to main content

asgi-ratelimiter

Modular rate limiting for ASGI apps — use Depends with FastAPI or middleware with Starlette.

Fixed-window limits backed by SQLite (stdlib). Redis extra is reserved for later.

Requirements

  • Python 3.14+

Install

# FastAPI (SQLite backend is built-in)
uv add "asgi-ratelimiter[fastapi]"

# Starlette
uv add "asgi-ratelimiter[starlette]"

Extras:

Extra Purpose
fastapi FastAPI Depends integration
starlette Starlette middleware
sqlite Marker only (stdlib sqlite3; no extra packages)
redis Declared for future Redis backend

Combine extras: uv add "asgi-ratelimiter[fastapi,sqlite]".

Concepts

from asgi_ratelimiter import Duration, Rate

# Allow 10 calls every 5 minutes
rate = Rate(limit=10, interval=Duration.MINUTE * 5)

Duration units: SECOND, MINUTE, HOUR, DAY, WEEK (multiply for longer windows).

FastAPI

App-wide or per-route via Depends(RateLimiter(...)):

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

configure_logging(level="INFO")

limiter = RateLimiter(
    rate=Rate(limit=5, interval=Duration.MINUTE),
    identifier=lambda request: request.client.host if request.client else "default",
    db_path="rate_limits.db",
)

app = FastAPI(dependencies=[Depends(limiter)])

@app.get("/ping")
async def ping() -> dict[str, str]:
    return {"status": "ok"}

Over limit → HTTP 429 with optional Retry-After.

Runnable example: examples/fastapi/fastapi_sqlite.py

Starlette

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

configure_logging(level="INFO")

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

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

Runnable example: examples/starlette/starlette_sqlite.py

Logging

from asgi_ratelimiter import configure_logging, set_level

configure_logging(level="DEBUG")  # enable library logs (loguru)
set_level("WARNING")              # change level later

Logging is off until configure_logging (or set_level) is called.

Development

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

See CONTRIBUTING.md.

License

AGPL-3.0-or-later

Links

Metadata

Release files for asgi-ratelimiter 0.1.0

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.1.0
File Size Uploaded Provenance
asgi_ratelimiter-0.1.0.tar.gz 41.4 kB With provenance Details

Built distribution (wheel)

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

Total release size: 64.7 kB

Release files / asgi_ratelimiter-0.1.0.tar.gz

Download URL asgi_ratelimiter-0.1.0.tar.gz
Size 41.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ca27af1c792d2180fa0892ead440f6c47892e7e1f9493373cb8d1e4c68cfbcb0
BLAKE2b-256 checksum
How to use checksums
083df521d56cf744bfb6c1bd920a3814ce8d9305cbd27ab22aa09f61594a790a
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 6, 2026.

Transparency log

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

Download URL asgi_ratelimiter-0.1.0-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8260457fe8d4457fce8a166e1d78d635c13d5966aeecc1d680c138a0a3dc01e
BLAKE2b-256 checksum
How to use checksums
52f0f7d6f8955f707bee34fd30643f54d893e715ee232796a8c71af3de38ca1f
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 6, 2026.

Transparency log

Security for asgi-ratelimiter 0.1.0

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.1.0.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 6, 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