keyGenerator returns, you control the granularity of your limits: you can give every tenant its own 500-email-per-month pool, cap each user’s daily API calls independently, or impose per-route limits across the board.
How keyGenerator works
keyGenerator is a required option on createQuotaLimiter. It receives the Express req object and must return a string. The middleware calls it synchronously on every request, then passes the result directly to the storage driver.
Examples
Key naming best practices
- Use a consistent prefix such as
quota:so keys are easy to identify and scan in Redis. - Include the resource type in the key (e.g.
emails,api,sms) so a single tenant can have independent limits for different resources without collisions. - Keep keys deterministic — the same request properties should always produce the same key, otherwise usage will be spread across unrelated buckets.
quota:emails:tenant_42 is clear, namespaced, and easy to search for in your storage backend.
Empty keys skip the quota check
IfkeyGenerator returns an empty string, the middleware skips the quota check entirely and calls next() without decrementing any counter. A warning is printed to the console:
"" based on conditions (for example, requests from internal service accounts). Be deliberate about it; returning an empty key silently bypasses all quota logic.
Key TTL
You do not manage key expiry directly inkeyGenerator. TTL is handled by the storage driver: RedisDriver and MemoryDriver both default to a 30-day TTL, configurable via the ttlSeconds option when constructing the driver. See Storage drivers for details.