Documentation Index
Fetch the complete documentation index at: https://express-rate-limit.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
A store tracks how many hits each client (identified via their IP address) has received and automatically reduce that hit count as time elapses.The Store Interface
A store must have the increment, decrement, and resetKey public
methods. It may optionally have the init, get and resetAll public methods
and a prefix (string) or localKeys (boolean) field. For backwards
compatibility with versions prior to 6.0.0, it may also have the incr and
decr public methods. Finally, it may have a constructor and any number of
private methods.
The increment method is the primary interface between the middleware and the
store. It adds 1 to the internal count for a key and returns an object
consisting of the new internal count (totalHits) and the time that the count
will reach 0 (resetTime).
The decrement method is used only to ‘uncount’ requests when one or both of
the skipSuccessfulRequests or skipFailedRequests options are enabled.
The init method allows the store to set itself up using the options passed to
the middleware. The store can get the windowMs option from this method.
Starting with version 8.5.0, the init method may be async, and is a good place
for things like opening a connection to a database or ensuring the schema is
up-to-date. Errors & promise rejections from init will be caught and logged by
express-rate-limit, but will not prevent other methods such as increment()
from being called. Additionally, other methods such as increment() may be
called before init() completes.
The get method takes a string argument (the key that identifies a client)
and returns an object consisting of the internal hit count (totalHits) and the
time that the count will reach 0 (resetTime) for the given client. It may
return undefined if it cannot find the key.
The resetKey method takes a string argument (the key that identifies a
client) and sets the internal count for that key to zero.
The resetAll method takes no arguments and sets the internal count for all
keys to zero.
The prefix field is used to avoid conflicts when the user creates multiple
instances of the store for multiple rate limits (e.g. 10 hits per minute and 60
hits per hour). Keys in the database should be prefixed with this value.
prefix is generally passed as an option to the constructor. (The singleCount
validation check also takes the prefix field into account and does not report
that a user is being double-counted if the stores have different prefixes.)
The localKeys field is an alternative to prefix for stores such as the
MemoryStore where two instances will automatically keep separate counts. Setting
it to true will prevent false positives from the singleCount validation
check.
The get and resetKey methods can be called from the middleware, like so:
Dependency configuration
Addexpress-rate-limit as a peer dependency, and a development dependency to
the package:
package.json
If the store supports the
incr method, replace >= 6 with >= 2.3.0