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

# Request weighting: charge variable quota per request

> Use the quotaWeight option to deduct more than one unit of quota per request, so bulk operations like sending 100 emails count accurately.

By default, every request deducts exactly one unit from the quota counter. Request weighting lets you change that amount on a per-request basis, so a single API call that does the work of many can be charged the correct amount. The weight is evaluated fresh for each incoming request and can be derived from any part of `req`.

## Why weighting matters

Consider an email-sending endpoint where a single request might send one email or a hundred. Without weighting, both calls deduct `1` 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.

```typescript theme={null}
quotaWeight: (req) => number | Promise<number>
```

A practical example from the README:

```typescript theme={null}
const quota = createQuotaLimiter({
  storage: new RedisDriver(redis),
  limit: 1000,
  keyGenerator: (req) => req.userId,
  quotaWeight: (req) => req.body.count || 1, // charge by count
});
```

Here, a request with `body.count = 50` deducts 50 from the quota in one shot.

## Default weight

When `quotaWeight` 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.
* **`onQuotaChecked` callback** — available as `ctx.weight`, alongside `remaining`, `success`, and the other fields. Use it to record the true cost of each request in your metrics or database.
* **`onQuotaExceeded` callback** — available as `ctx.weight` when a request is blocked. Useful for logging how large a batch attempted to exceed the quota.
* **Manual `increment` calls** — `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:

```typescript theme={null}
quotaWeight: async (req) => {
  const count = req.body.count || 1;
  return count;
}
```

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


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