Registry / devops / upstash-ratelimit

upstash-ratelimit

JSON →
library1.1.0pypypi✓ verified 85d ago

Serverless rate limiting library for Python, backed by Upstash Redis. Version 1.1.0 supports sliding window, fixed window, and token bucket algorithms. Requires Python 3.8+.

pip install upstash-ratelimit
INSTALL
IMPORT
SIG · UPSTASH-RATELIMIT
U
upstash-ratelimit
devopspythonv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Ratelimit
from upstash_ratelimit import Ratelimit
from upstash_ratelimit import Ratelimiter
Class name is Ratelimit, not Ratelimiter.
SlidingWindow
from upstash_ratelimit import SlidingWindow
Correct import path.
FixedWindow
from upstash_ratelimit import FixedWindow
Correct import path.
TokenBucket
from upstash_ratelimit import TokenBucket
Correct import path.

Initialize Upstash Redis client and create a rate limiter using sliding window algorithm.

import os from upstash_ratelimit import Ratelimit, SlidingWindow from upstash_redis import Redis # Initialize Redis client redis = Redis(url=os.environ.get('UPSTASH_REDIS_REST_URL', ''), token=os.environ.get('UPSTASH_REDIS_REST_TOKEN', '')) # Create ratelimit instance ratelimit = Ratelimit( redis=redis, limiter=SlidingWindow(max_requests=10, window=10), # 10 requests per 10 seconds prefix="ratelimit" ) # Check limit result = ratelimit.limit("user_id_123") print(result.allowed) # True or False print(result.remaining) # remaining requests
Debug
Known issues
gotchaThe `limit` method returns a `RatelimitResponse` object, not a boolean. Access `.allowed` or `.remaining`.
fix
Use `result.allowed` instead of assuming `result` is boolean.
affects: all
gotchaUpstash Redis REST API requires both `url` and `token`. Using only URL will raise authentication error.
fix
Set both UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN environment variables.
affects: all
gotchaThe async client requires installing the optional dependency: `pip install upstash-ratelimit[async]`
fix
Install async extra to use `AsyncRatelimit` or `async` methods.
affects: all
gotchaThe `prefix` argument in Ratelimit constructor does not automatically add a trailing colon. Ensure consistent prefix usage.
fix
Include a colon in prefix if desired, e.g. `prefix='myapp:'`
affects: all
Errors
Common errors & fixes
upstash_ratelimit.exceptions.RatelimitError: No redis client provided
Ratelimit instantiated without a redis argument.
fix
Pass a Redis instance to the Ratelimit constructor: `Ratelimit(redis=redis, ...)`
ModuleNotFoundError: No module named 'upstash_redis'
upstash-redis is not installed.
fix
Install it: `pip install upstash-redis`
AttributeError: 'RatelimitResponse' object has no attribute 'passed'
Using incorrect attribute name for limit result.
fix
Use `.allowed` or `.remaining` instead of `.passed`.
Upgrade
Version history
1.1.0latest on PyPI · released May 16, 2024
Audit
Dependencies
upstash-redisrequiredRequired for Redis client. Both sync and async are supported.
Agent activity
29 hits · last 30 days
node
28
OpenAI (training)
1
Resources