The two callbacks
onQuotaChecked
Fires after every request, whether allowed or blocked. Use it to keep external systems in sync with the current quota state.
The callback receives a QuotaCheckedContext object:
onQuotaExceeded
Fires only when a request is blocked (remaining === 0). Use it for billing alerts, violation logging, or tenant notifications.
The callback receives a QuotaExceededContext object:
Full example
Use cases
- UI quota display — Write the
remainingvalue fromonQuotaCheckedto your database so your frontend can show live usage without making extra API calls. - Metrics and observability — Publish
remaining,limit, andsuccessto Datadog, Prometheus, or any time-series store to track quota consumption trends across tenants. - Billing alerts — Use
onQuotaExceededto notify your billing system or send an email when a tenant’s quota runs out, prompting them to upgrade. - Violation logging — Record blocked requests to an audit table with the tenant ID, timestamp, and weight for compliance or abuse investigation.
Errors thrown inside a callback are caught by the middleware and printed to the console as
[express-quota-limiter] onQuotaChecked threw: .... They are never re-thrown and will never result in a 500 response to the client.