> ## Documentation Index
> Fetch the complete documentation index at: https://express-quota-limiter.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Track quota usage with onQuotaChecked and onQuotaExceeded

> Use onQuotaChecked and onQuotaExceeded callbacks to sync remaining quota to your database, emit metrics to Datadog, and fire billing alerts.

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:

| Field | Type | Description |
| - | - | - |
| `key` | `string` | The quota key that was checked (e.g. `"quota:emails:tenant_42"`) |
| `limit` | `number` | The resolved limit for this request |
| `success` | `boolean` | `true` if the request was allowed, `false` if blocked |
| `remaining` | `number` | Remaining quota after this operation. Always `>= 0`. |
| `req` | `Request` | The incoming Express request |
| `weight` | `number` | The calculated weight consumed by this request |

### `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:

| Field | Type | Description |
| - | - | - |
| `key` | `string` | The quota key that was exceeded |
| `limit` | `number` | The resolved limit for this request |
| `req` | `Request` | The incoming Express request |
| `weight` | `number` | The calculated weight of the blocked request |

## Full example

```typescript theme={null}
import { createQuotaLimiter, RedisDriver } from "express-quota-limiter";

const emailQuota = createQuotaLimiter({
  storage: new RedisDriver(redis),
  limit: 500,
  keyGenerator: (req) => `quota:emails:${req.tenantId}`,

  // runs on every request — keeps your DB in sync
  onQuotaChecked: async ({ remaining, req }) => {
    await db.query(
      "UPDATE tenants SET quota_remaining = $1 WHERE id = $2",
      [remaining, req.tenantId]
    );
  },

  // runs only when the quota is exhausted — sends an alert
  onQuotaExceeded: async ({ req }) => {
    await slack.send(`Tenant ${req.tenantId} exhausted their email quota`);
  },
});
```

## 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.

> [!WARNING]
>
> 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.

> [!NOTE]
>
> 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.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.