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 Every tenant gets their own counter, so one tenant exhausting their quota never affects another.
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:2
Use a dynamic limit to look up the plan from your database
The The resolved limit is passed to the storage driver on every request and is also available in response headers and callbacks.
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: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
Thelimit 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 */ }
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.
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.