Skip to main content
A quota key is the string that identifies a single quota bucket. Every incoming request is mapped to exactly one key, and the storage driver tracks usage against it. By controlling what 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.
The function has full access to the request—headers, body, route params, any properties your auth middleware has attached—so you can build keys from any combination of data.

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.
A key like quota:emails:tenant_42 is clear, namespaced, and easy to search for in your storage backend.

Empty keys skip the quota check

If keyGenerator 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:
This behavior is intentional—it lets you opt certain requests out of quota enforcement by returning "" 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 in keyGenerator. 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.