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.2.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.2.0
File Size Uploaded Provenance
asgi_ratelimiter-0.2.0.tar.gz 45.5 kB With provenance Details

Built distribution (wheel)

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

Total release size: 68.9 kB

Release files / asgi_ratelimiter-0.2.0.tar.gz

Download URL asgi_ratelimiter-0.2.0.tar.gz
Size 45.5 kB
Tags Source
SHA-256 checksum
How to use checksums
16b11d94db38ddf175894bdec17c39134210eb5587c2f547fa0d80b868fc2af0
BLAKE2b-256 checksum
How to use checksums
669e05deb6763f046e901d7daa2855c24bd73dfded2ed342f391520f58d06dd0
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.2.0-py3-none-any.whl

Download URL asgi_ratelimiter-0.2.0-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f2b5423728ecf824fc9462842b43449cf020f4db6618b31080b0d32170cec50
BLAKE2b-256 checksum
How to use checksums
a9cc5dde40094fdb8288a8e9e0983692711093e2590b815bac6c291c8b6f2d20
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.2.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.2.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