Upstash Documentation

Methods

2 min read

This page contains information on what methods are available in Ratelimit and how they can be used. For information on the cost of these operations in term of number of Redis commands, refer to the Costs page.

limit#

The limit method is the heart of the Ratelimit algorithm.

It receives an identifier to rate limit. Additionally, it can be passed a req parameter which can contain either a geo or a rate field. geo field is passed to the analytics but is not in use currently. The rate field determines the amount of tokens/requests to subtract from the state of the algorithm with regards to the provided identifier.

The limit method returns some more metadata that might be useful to you:

blockUntilReady#

In case you don't want to reject a request immediately but wait until it can be processed, we also provide

It is very similar to the limit method and takes an identifier and returns the same response. However if the current limit has already been exceeded, it will automatically wait until the next window starts and will try again. Setting the timeout parameter (in milliseconds) will cause the returned Promise to resolve in a finite amount of time.

Warning

In Cloudflare, blockUntilReady will not work as intended due to Date.now() not behaving the same as in Node environments.



For more information, check:
https://developers.cloudflare.com/workers/runtime-apis/web-standards

resetUsedTokens#

This method resets the state of the algorithm with respect to some identifier:

getRemaining#

This method returns the remaining tokens/requests available for some identifier:

remaining is the remaining tokens. reset is the reset timestamp.

setDynamicLimit#

This method sets or removes a global dynamic rate limit. Dynamic limits override the default limiter settings for all subsequent rate limit checks.

Parameters:

  • limit: The new limit value (number), or false to remove the dynamic limit and fall back to the default

Example:

getDynamicLimit#

This method retrieves the current dynamic limit if one is set.

Returns: The current dynamic limit value, or null if no dynamic limit is set

Example: