Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_ratelimit Discord

Swarmauri Middleware Ratelimit

Flexible rate limiting middleware for Swarmauri applications.

This package provides a drop-in FastAPI middleware that throttles requests based on the client IP address or an authentication token. It is useful for preventing abuse of public APIs or protecting upstream services from bursts of traffic.

Features

  • Configurable request limit and time window that defaults to 100 requests per 60 seconds.
  • Tracks callers by IP address or a custom authentication token header.
  • Maintains in-memory counters that reset automatically after the configured time window.
  • Returns FastAPI Response objects with HTTP 429 status codes when the limit is exceeded.

Installation

Choose the tool that matches your workflow:

# pip
pip install swarmauri_middleware_ratelimit

# Poetry
poetry add swarmauri_middleware_ratelimit

# uv
uv add swarmauri_middleware_ratelimit

Usage

Basic IP-based limiting

from fastapi import FastAPI
from fastapi.testclient import TestClient
from swarmauri_middleware_ratelimit import RateLimitMiddleware

app = FastAPI()
rate_limiter = RateLimitMiddleware(rate_limit=2, time_window=60)


@app.middleware("http")
async def limit_requests(request, call_next):
    return await rate_limiter.dispatch(request, call_next)


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


client = TestClient(app)

assert client.get("/ping").status_code == 200
assert client.get("/ping").status_code == 200
assert client.get("/ping").status_code == 429

In this example the first two requests succeed because they fall inside the allowed limit. A third request within the same 60-second window exceeds the threshold and the middleware immediately returns a 429 response with the body "Rate limit exceeded". The middleware instance keeps request counters in memory, so you should create one instance per application process.

Token-based rate limiting

# Use the value of the `X-Api-Key` header to track clients instead of IP
rate_limiter = RateLimitMiddleware(
    rate_limit=100,
    time_window=60,
    use_token=True,
    token_header="X-Api-Key",
)


@app.middleware("http")
async def limit_requests(request, call_next):
    return await rate_limiter.dispatch(request, call_next)

When use_token is True the middleware looks for the configured header on every request and raises a ValueError if it is missing. This makes it safe to enforce API key or bearer token quotas even when requests are routed through shared proxies. Reuse the same middleware instance across requests to ensure counters continue to accumulate across calls.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

Metadata

Release files for swarmauri_middleware_ratelimit 0.11.0.dev1

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

Source distribution (sdist)

Source distribution for swarmauri_middleware_ratelimit 0.11.0.dev1
File Size Uploaded Provenance Actions
swarmauri_middleware_ratelimit-0.11.0.dev1.tar.gz 8.5 kB Without provenance Details for swarmauri_middleware_ratelimit-0.11.0.dev1.tar.gz

Built distribution (wheel)

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

Total release size: 18.2 kB

Release files / swarmauri_middleware_ratelimit-0.11.0.dev1.tar.gz

Download URL swarmauri_middleware_ratelimit-0.11.0.dev1.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
23f9d728e4f2cd16ceed493bc6277d60d90399fbe6fb408f5f78f5b9ee3b920e
BLAKE2b-256 checksum
How to use checksums
062b8ef24bd00628e7909a7d38f7b210fd102f6f0d281d547187e67bef42ef2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / swarmauri_middleware_ratelimit-0.11.0.dev1-py3-none-any.whl

Download URL swarmauri_middleware_ratelimit-0.11.0.dev1-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e51645d06dfbdbe6ec90711875bd0c5aac48333f1a356a16a05250598dcf70d
BLAKE2b-256 checksum
How to use checksums
828067362a4a02b130364de56bf6a1d772ecab9c0bb35bc83cca347722d4f1da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for swarmauri_middleware_ratelimit 0.11.0.dev1

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.

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