Skip to main content
Callbacks let you react to quota events — syncing remaining counts to a database, publishing metrics to a monitoring system, or alerting a tenant — without modifying the middleware itself. Both callbacks run fire-and-forget: they are invoked asynchronously after the quota check completes, and any errors they throw are caught and logged. A callback error will never change the HTTP response your users receive.

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 remaining value from onQuotaChecked to your database so your frontend can show live usage without making extra API calls.
  • Metrics and observability — Publish remaining, limit, and success to Datadog, Prometheus, or any time-series store to track quota consumption trends across tenants.
  • Billing alerts — Use onQuotaExceeded to 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.
Callbacks run asynchronously — the HTTP response is sent to the client before the callback completes. Do not rely on a callback finishing before the next line of your application code executes. If you need synchronous side effects, handle them inside your route handler instead.
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.