Overview
TheRateLimiter class is the main interface for defining and managing rate limits in your Convex application. It provides a type-safe way to configure multiple named rate limits and enforce them across your queries and mutations.
Constructor
Type Parameters
Record<string, RateLimitConfig>
default:"Record<never, never>"
A record mapping rate limit names to their configurations. This type parameter enables type-safe access to named rate limits.
Parameters
ComponentApi
required
The rate limiter component from your Convex generated API. Import this as
components.rateLimiter from ./_generated/api.js.Limits
An object defining your rate limits. Each key is a rate limit name, and each value is a
RateLimitConfig object. If you don’t define limits here, you must provide the config inline when calling rate limit methods.Usage Example
Available Methods
TheRateLimiter class provides the following methods:
limit()- Check and consume rate limit tokens (mutations only)check()- Check rate limit without consuming tokens (queries and mutations)reset()- Reset a rate limit to its initial stategetValue()- Get current rate limit state and metadatahookAPI()- Create client-accessible queries for rate limit status
Type Safety
When you define rate limits in the constructor, TypeScript will:- Auto-complete rate limit names when calling methods
- Enforce that named rate limits exist
- Allow optional
configparameter for predefined limits - Require
configparameter for inline (unnamed) rate limits
Related
- RateLimitConfig - Rate limit configuration types
- limit() - Rate limiting method
- check() - Non-consuming check method