createQuotaLimiter is the main entry point of express-quota-limiter. You call it once with your configuration and it returns a standard Express middleware function — (req, res, next) => Promise<void> — that you attach directly to any route or router.
Function signature
Options
The storage driver that persists quota state. Use
RedisDriver in production and MemoryDriver for tests. You can also supply any object that implements the QuotaStorage interface.A function that receives the incoming request and returns the quota key for that request. Keys are typically scoped to a tenant, user, or route — for example
quota:emails:${req.tenantId}. When this function returns an empty string the quota check is skipped entirely and the request passes through.The maximum number of allowed operations per quota window. You can pass a static number or an async function that resolves a per-request limit — useful for multi-tenant apps where different plans have different quotas.
The human-readable message sent in the
message field of the HTTP 429 JSON response body. Override this to provide context-specific messages such as "Monthly email quota exceeded".Controls how the middleware behaves when the storage driver throws an error. When
true (the default), the error is logged and the request passes through. When false, the middleware returns HTTP 500.An optional callback invoked after every quota check, whether the request was allowed or blocked. It runs fire-and-forget — any error it throws is caught and logged, and never affects the HTTP response. Use it to sync remaining quota to your database, push metrics to Datadog or Prometheus, or power usage dashboards.
An optional callback invoked only when a request is blocked because the quota is exhausted. Like
onQuotaChecked, it runs fire-and-forget. Use it to log violations, send billing alerts, or notify the tenant.An optional function that returns how much quota the current request should consume. Defaults to
1 when omitted. Use this for endpoints where some requests are more expensive than others — for example, charging by the number of emails in a bulk send.Behavior
On every request the middleware:- Calls
keyGenerator(req)to determine the quota key. If the key is an empty string, it logs a warning and callsnext()without checking quota. - Resolves the
limitvalue — static or dynamic. - Resolves the
quotaWeightvalue (defaults to1). - Calls
storage.decrement(key, limit, weight)atomically. - Sets
Quota-RemainingandQuota-Limitheaders on the response regardless of whether the request was allowed or blocked. - Fires
onQuotaCheckedas a fire-and-forget callback if provided. - If the quota is exceeded, fires
onQuotaExceededas a fire-and-forget callback if provided, then returns the HTTP 429 JSON response below. - If the quota is not exceeded, calls
next()to pass the request to the next handler.
HTTP 429 response
When a request exceeds the quota, the middleware responds with status429 Too Many Requests and the following JSON body:
message field reflects the value of the errorMessage option. The quota.remaining field is always 0 in a 429 response.
Storage error behavior
Whenstorage.decrement throws, the middleware logs the error to console.error and then does one of the following based on failOpen: