This release is a pre-release and may not be stable for production use.
HTTPX Limiter
| Package | |
| Meta | |
| Automation |
A lightweight package that provides rate-limited httpx and httpx2 transports.
Installation
The package is published on PyPI. Plain
pip install httpx-limiter installs only the shared core, which contains no HTTP
client. Select the HTTP client you use with an extra, httpx or httpx2, and normally
also a rate limiter backend, aiolimiter or pyrate.
pip install 'httpx-limiter[httpx,aiolimiter]'
pip install 'httpx-limiter[httpx2,pyrate]'
To use both HTTP clients in the same process, install both extras.
pip install 'httpx-limiter[httpx,httpx2,aiolimiter]'
There is no automatic fallback: you choose the integration explicitly by importing from
httpx_limiter.httpx or from httpx_limiter.httpx2.
Migrating from <0.7
The HTTP-specific classes moved out of the package root and httpx is no longer a
required dependency. Install httpx-limiter[httpx] instead of httpx-limiter and
change your imports. Rate, AbstractAsyncLimiter, and the limiter implementations
keep their import paths.
-from httpx_limiter import AsyncRateLimitedTransport, Rate
+from httpx_limiter import Rate
+from httpx_limiter.httpx import AsyncRateLimitedTransport
The same applies to AsyncMultiRateLimitedTransport, AbstractRateLimiterRepository,
and HTTPXAsyncHTTPTransportKeywordArguments. For HTTPX2, import them from
httpx_limiter.httpx2 instead.
Verifying a release
Each release attests that its wheel and source distribution were built by this
repository's release workflow from a commit on main. Download a distribution from
PyPI and check it with the GitHub CLI, with VERSION set to
the release you want to check:
pip download --no-deps --only-binary :all: "httpx-limiter==${VERSION}"
gh attestation verify "httpx_limiter-${VERSION}-py3-none-any.whl" \
--repo Midnighter/httpx-limiter \
--signer-workflow Midnighter/httpx-limiter/.github/workflows/release.yml
The command exits with status 0 when the file verifies. Add --format json to see the
tag and commit it was built from. Releases up to 0.7.0 have no such attestation.
Rate Limiter Backends
This package provides two different asynchronous rate limiter implementations to choose from:
1. aiolimiter
- Single rate limit only: Supports only one rate limit at a time
- Lightweight: Minimal dependencies and simpler configuration
- Linear token refresh rate: As an example, if you set a rate of 2 per second, roughly one token will be added every 500 milliseconds
from httpx_limiter import Rate
from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
limiter = AiolimiterAsyncLimiter.create(Rate.create(magnitude=20))
2. pyrate-limiter
- Multiple rate limits: Support for multiple concurrent rate limits, for example, 10 requests per second and 100 requests per minute
- Flexible configuration: Comprehensive configuration options
- Multiprocessing dependency: Current implementation depends on
multiprocessingwhich may not be available in all environments, such as pyodide - Stepwise token refresh rate: As an example, if you set a rate of 2 per second, two token will be added every second
from httpx_limiter import Rate
from httpx_limiter.pyrate import PyrateAsyncLimiter
async def main():
# Single rate limit
limiter = PyrateAsyncLimiter.create(Rate.create(magnitude=20))
# Multiple rate limits
limiter = PyrateAsyncLimiter.create(
Rate.create(magnitude=10), # 10 per second
Rate.create(magnitude=100, duration=60), # 100 per minute
)
Tutorial
You can limit the number of requests made by an HTTPX or HTTPX2 client using the transports provided in this package. That is useful in situations when you need to make a large number of asynchronous requests against endpoints that implement a rate limit.
The examples below use HTTPX. For HTTPX2, import httpx2 instead of httpx, and import
the transports and repositories from httpx_limiter.httpx2 instead of
httpx_limiter.httpx; everything else stays the same. The
documentation shows every example for both
clients. Transports, repositories, requests, and responses must all come from the same
client: httpx_limiter.httpx works with httpx and httpx_limiter.httpx2 works with
httpx2.
Single Rate Limit
The simplest use case is to apply a single rate limit to all requests. If you want to be able to make twenty requests per second, for example, use the following code:
import httpx
from httpx_limiter import Rate
from httpx_limiter.httpx import AsyncRateLimitedTransport
from httpx_limiter.pyrate import PyrateAsyncLimiter
async def main():
limiter = PyrateAsyncLimiter.create(Rate.create(magnitude=20))
async with httpx.AsyncClient(
transport=AsyncRateLimitedTransport.create(limiter=limiter),
) as client:
response = await client.get("https://httpbin.org")
Multiple Rate Limits
For more advanced use cases, you can apply different rate limits based on a concrete
implementation of the AbstractRateLimiterRepository of your HTTP client, either
for HTTPX
or
for HTTPX2.
There are two relevant methods that both get passed the current request. One method
needs to identify which rate limit to apply, and the other method sets the rate limit
itself. See the following example:
import httpx
from httpx_limiter import Rate
from httpx_limiter.httpx import (
AbstractRateLimiterRepository,
AsyncMultiRateLimitedTransport,
)
from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
class DomainBasedRateLimiterRepository(AbstractRateLimiterRepository):
"""Apply different rate limits based on the domain being requested."""
def get_identifier(self, request: httpx.Request) -> str:
"""Return the domain as the identifier for rate limiting."""
return request.url.host
def create(self, request: httpx.Request) -> AiolimiterAsyncLimiter:
"""Create a rate limiter for the domain."""
return AiolimiterAsyncLimiter.create(Rate.create(magnitude=25))
async def main():
async with httpx.AsyncClient(
transport=AsyncMultiRateLimitedTransport.create(
repository=DomainBasedRateLimiterRepository(),
),
) as client:
response = await client.get("https://httpbin.org")
from datetime import datetime, timezone
import httpx
from httpx_limiter import Rate
from httpx_limiter.httpx import (
AbstractRateLimiterRepository,
AsyncMultiRateLimitedTransport,
)
from httpx_limiter.pyrate import PyrateAsyncLimiter
class DayNightRateLimiterRepository(AbstractRateLimiterRepository):
"""Apply different rate limits based on the time of day."""
def get_identifier(self, request: httpx.Request) -> str:
"""Identify whether it is currently day or night."""
if 6 <= datetime.now(tz=timezone.utc).hour < 18:
return "day"
return "night"
def create(self, request: httpx.Request) -> PyrateAsyncLimiter:
"""Create a rate limiter based on the time of day."""
if self.get_identifier(request) == "day":
return PyrateAsyncLimiter.create(Rate.create(magnitude=10))
return PyrateAsyncLimiter.create(Rate.create(magnitude=100))
async def main():
async with httpx.AsyncClient(
transport=AsyncMultiRateLimitedTransport.create(
repository=DayNightRateLimiterRepository(),
),
) as client:
response = await client.get("https://httpbin.org")
Transport Options
Both AsyncRateLimitedTransport.create and AsyncMultiRateLimitedTransport.create
forward additional keyword arguments to the AsyncHTTPTransport of the selected client.
The accepted keywords are described by the HTTPXAsyncHTTPTransportKeywordArguments
type of httpx_limiter.httpx and of httpx_limiter.httpx2. Types such as Limits and
Proxy must come from the selected client.
import httpx
from httpx_limiter import Rate
from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
from httpx_limiter.httpx import AsyncRateLimitedTransport
async def main():
limiter = AiolimiterAsyncLimiter.create(Rate.create(magnitude=20))
transport = AsyncRateLimitedTransport.create(
limiter=limiter,
limits=httpx.Limits(max_connections=10),
retries=2,
)
async with httpx.AsyncClient(transport=transport) as client:
response = await client.get("https://httpbin.org")
Using Both Clients Together
You can install both extras, httpx-limiter[httpx,httpx2], and use both integrations in
the same process. The limiters are independent of the HTTP client, so you may even share
one limiter between them. The HTTP objects, however, are not interchangeable: an
httpx_limiter.httpx transport cannot be used with an httpx2.AsyncClient, and a
repository for httpx.Request cannot serve httpx2.Request objects.
import httpx
import httpx2
from httpx_limiter import Rate
from httpx_limiter.aiolimiter import AiolimiterAsyncLimiter
from httpx_limiter.httpx import AsyncRateLimitedTransport as HTTPXTransport
from httpx_limiter.httpx2 import AsyncRateLimitedTransport as HTTPX2Transport
async def main():
limiter = AiolimiterAsyncLimiter.create(Rate.create(magnitude=20))
async with (
httpx.AsyncClient(transport=HTTPXTransport.create(limiter=limiter)) as old,
httpx2.AsyncClient(transport=HTTPX2Transport.create(limiter=limiter)) as new,
):
first = await old.get("https://httpbin.org") # an httpx.Response
second = await new.get("https://httpbin.org") # an httpx2.Response
Copyright
- Copyright © 2024, 2025 Moritz E. Beber.
- Free software distributed under the Apache Software License 2.0.
Metadata
Release files for httpx-limiter 0.7.1rc1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| httpx_limiter-0.7.1rc1.tar.gz | 17.9 kB | With provenance | Details for httpx_limiter-0.7.1rc1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| httpx_limiter-0.7.1rc1-py3-none-any.whl | Python 3 | none | any | Details for httpx_limiter-0.7.1rc1-py3-none-any.whl |
Total release size: 48.2 kB
Release files / httpx_limiter-0.7.1rc1.tar.gz
| Download URL | httpx_limiter-0.7.1rc1.tar.gz |
|---|---|
| Size | 17.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35c22dadb4c09db88914b6bbbb038c9d6d5a592d747c59591ce844b103cd246f
|
|
BLAKE2b-256 checksum How to use checksums |
70158483e10a09b92f590e733bd5a1aa608489d9d8191dfe0bbbdab9c077cda6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 20, 2026.
Transparency logRelease files / httpx_limiter-0.7.1rc1-py3-none-any.whl
| Download URL | httpx_limiter-0.7.1rc1-py3-none-any.whl |
|---|---|
| Size | 30.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a27a8c0f3afd764fde8b68cfd3ca2811a423569650aa1a0c5322297aa20b39f2
|
|
BLAKE2b-256 checksum How to use checksums |
af2ee2dd0a40eed50f08ab4fa2517444b9dc18bcc8ea116a5a9b48cc93fc7318
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 20, 2026.
Transparency logSecurity for httpx-limiter 0.7.1rc1
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
Both files in this release have been uploaded with provenance metadata.
Attestation for httpx_limiter-0.7.1rc1.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 Nov 20, 2026.
Transparency logA 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.