Overview
ThegetValue() method returns the current state of a rate limit without consuming any tokens. It provides detailed information including the current token value, timestamp, shard number, and configuration.
Method Signature
Parameters
RunQueryCtx
required
The context object from a query, including
runQuery. This method works in both queries and mutations.string
required
The name of the rate limit to query.
object
Optional configuration for retrieving the value.
string
The key for the rate limit instance. If not provided, retrieves the shared global rate limit value.
number
For sharded rate limits, the number of shards to sample. This affects which shard’s data is returned. Useful for debugging or monitoring specific shards.
RateLimitConfig
The rate limit configuration. Required only if the rate limit name was not defined in the
RateLimiter constructor.Return Type
object
Returns an object with the following properties:
number
required
The current number of available tokens. This can be a decimal for token bucket rate limits.
number
required
The timestamp (in milliseconds) of when this value was last updated. For token bucket, this is the last refill time. For fixed window, this is the start of the current window.
number
required
The shard number that this data came from. Useful for sharded rate limits to understand which shard is being used.
RateLimitConfig
required
The complete rate limit configuration for this limit, including all settings like
kind, rate, period, capacity, etc.Use with calculateRateLimit
ThegetValue() method pairs perfectly with the calculateRateLimit helper function for client-side rate limit tracking:
Examples
Basic Usage
Monitoring Dashboard
User Quota Display
Debug Specific Shard
Calculate Time Until Next Token
Notes
Unlike
check(), which evaluates whether an action would be allowed, getValue() returns the raw state data. Use getValue() when you need to display quota information or perform custom calculations.The
value returned can be a decimal number for token bucket rate limits, as tokens accumulate gradually over time. Use Math.floor() if you need a whole number.For rate limits that haven’t been used yet,
getValue() will return the initial state with full capacity.Related
check()- Check if an action would be allowedlimit()- Consume tokens- calculateRateLimit - Client-side state calculation