Skip to main content
Multi-tenant applications typically need different quota ceilings for different customers. A free plan might allow 500 emails per month, while a pro plan allows 5,000. express-quota-limiter handles this by letting you derive both the quota key and the limit dynamically from the incoming request, keeping all quota logic in one place.
1

Set keyGenerator to scope quota per tenant

The keyGenerator function receives the Express Request and must return a unique string key. To isolate each tenant’s quota, include the tenant identifier in the key:
Every tenant gets their own counter, so one tenant exhausting their quota never affects another.
2

Use a dynamic limit to look up the plan from your database

The limit option accepts an async function that receives the same Request object. You can query your database inside it and return a different ceiling based on the tenant’s plan:
The resolved limit is passed to the storage driver on every request and is also available in response headers and callbacks.
3

Attach the middleware to your routes

Pass the configured middleware to any route or router. It runs before your handler and returns a 429 response automatically when the quota is exhausted:

Full example

How dynamic limits work

The limit option accepts three forms:
  • A static number: limit: 500
  • A synchronous function: limit: (req) => req.body.tier === "pro" ? 5000 : 500
  • An async function: limit: async (req) => { /* await DB */ }
When limit is a function, createQuotaLimiter calls it on every request with the full Request object before contacting the storage driver. The resolved value is used as the ceiling for that specific request and is also set in the Quota-Limit response header.
Cache plan lookups in memory or Redis to avoid a database round-trip on every request. A short TTL (30–60 seconds) is usually sufficient and reduces latency significantly under load.
When failOpen is true (the default), a database error inside the limit function or a storage driver error will not block the request — it will pass through and the error is logged to the console. Set failOpen: false if you need strict enforcement even during outages.