req.
Why weighting matters
Consider an email-sending endpoint where a single request might send one email or a hundred. Without weighting, both calls deduct1 from the quota — a tenant could exhaust 500 emails by sending 500 single-email requests, but could also send 50,000 emails in 500 bulk requests and never trip the limit. Weighting closes that gap by charging the actual cost of each call.
How quotaWeight works
quotaWeight is an optional function you pass to createQuotaLimiter. It receives the Express req object and returns a number — synchronously or as a Promise. That number is passed directly to the storage driver’s decrement method as the weight argument.
body.count = 50 deducts 50 from the quota in one shot.
Default weight
WhenquotaWeight is not provided, the weight defaults to 1. Every request costs exactly one unit, which is the standard rate-limiting behavior.
Where the weight appears
The resolved weight is threaded through the entire request lifecycle:- Storage driver — passed as the third argument to
decrement(key, limit, weight), so the driver subtracts the correct amount atomically. onQuotaCheckedcallback — available asctx.weight, alongsideremaining,success, and the other fields. Use it to record the true cost of each request in your metrics or database.onQuotaExceededcallback — available asctx.weightwhen a request is blocked. Useful for logging how large a batch attempted to exceed the quota.- Manual
incrementcalls —driver.increment(key, weight)accepts the same weight value, so you can refund quota by the same amount that was originally charged.
Async weights
quotaWeight can be async, which lets you look up pricing rules or batch sizes from a database before the quota check runs:
The weight is evaluated on every request, so keep the function fast. If you need to fetch data asynchronously, make sure the lookup is low-latency or cached — it runs in the critical path before the response is sent.