Skip to main content

Overview

The RateLimiter 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

The RateLimiter 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 state
  • getValue() - Get current rate limit state and metadata
  • hookAPI() - 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 config parameter for predefined limits
  • Require config parameter for inline (unnamed) rate limits