Skip to main content

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 the RateLimiter class and create an instance with your rate limit configurations:

Time Constants

The library provides helpful time constants:
  • SECOND = 1000 milliseconds
  • MINUTE = 60 seconds
  • HOUR = 60 minutes
  • DAY = 24 hours
  • WEEK = 7 days

Using limit() in Mutations

Call limit() in any mutation to enforce a rate limit:

Understanding the Return Value

The limit() 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 consumed
  • false: The rate limit was exceeded, and no token was consumed

retryAfter (number | undefined)

The duration in milliseconds until the request could succeed:
  • When ok is false: How long to wait before retrying
  • When ok is true and retryAfter is defined: You reserved capacity for the future (see Reserving Capacity)
  • undefined: The request succeeded immediately

Global vs Singleton Rate Limits

When you call limit() without a key parameter, the rate limit applies globally to all requests:
This is useful for:
  • 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