Documentation Index
Fetch the complete documentation index at: https://mintlify.com/get-convex/rate-limiter/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Thelimit() 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
The context object from a mutation, including
runMutation. This can be the full mutation context or any object that provides runMutation.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.Optional configuration for this rate limit check.
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.
The number of tokens to consume. Useful for rate limiting based on request size or cost.
If
true, reserves tokens for future use instead of consuming them immediately. The returned retryAfter indicates when the reserved work should be executed.If
true, throws a ConvexError with RateLimitError data when the rate limit is exceeded. If false, returns { ok: false, retryAfter } instead.The rate limit configuration. Required only if the rate limit name was not defined in the
RateLimiter constructor. See RateLimitConfig for details.Return Type
Returns a promise that resolves to one of two shapes:When rate limit is not exceeded:When rate limit is exceeded (and
Indicates the request is allowed.
Only present when
reserve: true. The duration in milliseconds to wait before executing the reserved work.throws: false):Indicates the rate limit was exceeded.
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.Related Methods
check()- Check rate limit without consuming tokensreset()- Reset a rate limitgetValue()- Get current rate limit state