Overview
The Upstash Rate Limit provider implements distributed rate limiting for your Contract Kit applications using Upstash Redis and the @upstash/ratelimit library. It provides a serverless-friendly solution for protecting your API from abuse.Features
- ✅ Implements the standard
RateLimitPortinterface - ✅ Uses Upstash Redis REST API (serverless-friendly, no connection pooling needed)
- ✅ Fixed window rate limiting algorithm
- ✅ Dynamic rate limit configuration per request
- ✅ Configurable key prefix to avoid collisions
- ✅ Type-safe configuration with Zod validation
- ✅ Works seamlessly with Contract Kit framework and middleware
Installation
Configuration
Set these environment variables:| Variable | Required | Description | Example |
|---|---|---|---|
UPSTASH_REDIS_REST_URL | Yes | Your Upstash Redis REST URL | https://us1-properly-ancient-12345.upstash.io |
UPSTASH_REDIS_REST_TOKEN | Yes | Your Upstash Redis REST token | AXXXeyJpZCI6IjEy... |
UPSTASH_PREFIX | No | Key prefix for rate limit keys (default: ck:ratelimit) | myapp:ratelimit |
Getting Upstash Credentials
- Sign up at Upstash
- Create a new Redis database
- Navigate to the database details page
- Copy the REST URL and REST token from the “REST API” section
Example .env
Setup
Basic Setup
Type Your Ports
To get proper type inference for the rate limit port, extend your ports type:API Reference
The provider extends your ports with arateLimit property:
hit(options)
Check if a request should be rate limited.options.key: Unique identifier for the rate limit (e.g., IP address, user ID)options.limit: Maximum number of requests allowedoptions.windowSec: Time window in seconds
Promise<RateLimitResult>
Usage Examples
Rate Limiting by IP Address
Different Rate Limits for Different Endpoints
You can apply different rate limits for different operations:Using with Contract Metadata
You can define rate limit metadata on your contracts:Rate Limiting by User ID
Implementation Details
- Algorithm: Uses fixed window rate limiting via
Ratelimit.fixedWindow() - Backend: Upstash Redis REST API (serverless-compatible)
- Per-request configuration: Creates a new
Ratelimitinstance for eachhit()call to support dynamic limits - Key prefix: Configurable prefix to avoid key collisions
Advanced Usage
Access the Underlying Redis Client
The provider extends the standardRateLimitPort with access to the underlying Upstash Redis client:
Best Practices
Use descriptive keys
Use descriptive keys
Use clear, hierarchical keys like
api:user:123 or login:ip:192.168.1.1 for better debugging.Set appropriate limits
Set appropriate limits
Balance security and user experience. Too strict limits frustrate users, too lenient limits won’t protect your API.
Include rate limit headers
Include rate limit headers
Return
X-RateLimit-* headers to inform clients about their rate limit status.Different limits for different actions
Different limits for different actions
Use stricter limits for sensitive operations like login attempts and authentication.
Consider authenticated vs anonymous
Consider authenticated vs anonymous
Apply different rate limits for authenticated users vs anonymous requests.