Skip to main content

Overview

The limit() method checks a rate limit and consumes tokens if available. This is the primary method for enforcing rate limits in your mutations.

Method Signature

Parameters

RunMutationCtx
required
The context object from a mutation, including runMutation. This can be the full mutation context or any object that provides runMutation.
string
required
The name of the rate limit to check. If this name was defined in the RateLimiter constructor, it will be type-checked and auto-completed.
object
Optional configuration for this rate limit check.
string
A unique identifier for this rate limit instance. Use this to create per-user, per-IP, or other scoped rate limits. If not provided, the rate limit is shared globally.
number
default:"1"
The number of tokens to consume. Useful for rate limiting based on request size or cost.
boolean
default:"false"
If true, reserves tokens for future use instead of consuming them immediately. The returned retryAfter indicates when the reserved work should be executed.
boolean
default:"false"
If true, throws a ConvexError with RateLimitError data when the rate limit is exceeded. If false, returns { ok: false, retryAfter } instead.
RateLimitConfig
The rate limit configuration. Required only if the rate limit name was not defined in the RateLimiter constructor. See RateLimitConfig for details.

Return Type

object
Returns a promise that resolves to one of two shapes:When rate limit is not exceeded:
true
required
Indicates the request is allowed.
number
Only present when reserve: true. The duration in milliseconds to wait before executing the reserved work.
When rate limit is exceeded (and throws: false):
false
required
Indicates the rate limit was exceeded.
number
required
The duration in milliseconds when retrying could succeed.

Examples

Basic Usage

With Key (Per-User Limit)

With Custom Count

With Reserve (Scheduled Execution)

With Throws

Notes

The limit() method can only be called from mutations or within runMutation. For queries, use check() instead.
When reserve: true is used, the tokens are consumed immediately, but retryAfter indicates when the work should be performed to respect the rate limit.
If you use throws: true and the rate limit is exceeded, you can catch the error with isRateLimitError() to access the retryAfter value in the error data.
  • check() - Check rate limit without consuming tokens
  • reset() - Reset a rate limit
  • getValue() - Get current rate limit state