Overview
The most basic use of the Convex Rate Limiter is to create a global (singleton) rate limit that applies to all requests regardless of user or context.Creating a RateLimiter Instance
First, import theRateLimiter class and create an instance with your rate limit configurations:
Time Constants
The library provides helpful time constants:SECOND= 1000 millisecondsMINUTE= 60 secondsHOUR= 60 minutesDAY= 24 hoursWEEK= 7 days
Using limit() in Mutations
Calllimit() in any mutation to enforce a rate limit:
Understanding the Return Value
Thelimit() method returns an object with two properties:
ok (boolean)
Indicates whether the request was allowed:
true: The rate limit was not exceeded, and a token was consumedfalse: The rate limit was exceeded, and no token was consumed
retryAfter (number | undefined)
The duration in milliseconds until the request could succeed:
- When
okisfalse: How long to wait before retrying - When
okistrueandretryAfteris defined: You reserved capacity for the future (see Reserving Capacity) undefined: The request succeeded immediately
Global vs Singleton Rate Limits
When you calllimit() without a key parameter, the rate limit applies globally to all requests:
- Protecting expensive operations (LLM API calls, image generation)
- Preventing bot signups
- Rate limiting anonymous users
- Enforcing global API quotas
For per-user rate limits, see Per-User Limits.
Complete Example
Here’s a complete example from the source code:Next Steps
- Learn about Per-User Limits
- Understand Error Handling with the
throwsoption - Explore Rate Limit Strategies