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

# Enforce multi-tenant quotas with dynamic per-plan limits

> Scope quota keys per tenant and resolve per-request limits from your database to enforce different quotas across free and paid plans.

Multi-tenant applications typically need different quota ceilings for different customers. A free plan might allow 500 emails per month, while a pro plan allows 5,000. `express-quota-limiter` handles this by letting you derive both the quota key and the limit dynamically from the incoming request, keeping all quota logic in one place.

**1. Set keyGenerator to scope quota per tenant**

The `keyGenerator` function receives the Express `Request` and must return a unique string key. To isolate each tenant's quota, include the tenant identifier in the key:

```typescript theme={null}
keyGenerator: (req) => `quota:emails:${req.tenantId}`,
```

Every tenant gets their own counter, so one tenant exhausting their quota never affects another.

**2. Use a dynamic limit to look up the plan from your database**

The `limit` option accepts an async function that receives the same `Request` object. You can query your database inside it and return a different ceiling based on the tenant's plan:

```typescript theme={null}
limit: async (req) => {
  const tenant = await db.query(
    "SELECT plan FROM tenants WHERE id = $1",
    [req.tenantId]
  );
  return tenant.plan === "pro" ? 5000 : 500;
},
```

The resolved limit is passed to the storage driver on every request and is also available in response headers and callbacks.

**3. Attach the middleware to your routes**

Pass the configured middleware to any route or router. It runs before your handler and returns a `429` response automatically when the quota is exhausted:

```typescript theme={null}
app.post("/send-email", emailQuota, (req, res) => {
  res.json({ ok: true });
});
```

## Full example

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

const app = express();

const emailQuota = createQuotaLimiter({
  storage: new RedisDriver(redis),
  limit: async (req) => {
    const tenant = await db.query(
      "SELECT plan FROM tenants WHERE id = $1",
      [req.tenantId]
    );
    // Pro plan: 5000/month, free plan: 500/month
    return tenant.plan === "pro" ? 5000 : 500;
  },
  keyGenerator: (req) => `quota:emails:${req.tenantId}`,
});

app.post("/send-email", emailQuota, (req, res) => {
  res.json({ ok: true });
});
```

## How dynamic limits work

The `limit` option accepts three forms:

* A static number: `limit: 500`
* A synchronous function: `limit: (req) => req.body.tier === "pro" ? 5000 : 500`
* An async function: `limit: async (req) => { /* await DB */ }`

When `limit` is a function, `createQuotaLimiter` calls it on every request with the full `Request` object before contacting the storage driver. The resolved value is used as the ceiling for that specific request and is also set in the `Quota-Limit` response header.

> [!TIP]
>
> Cache plan lookups in memory or Redis to avoid a database round-trip on every request. A short TTL (30–60 seconds) is usually sufficient and reduces latency significantly under load.

> [!NOTE]
>
> When `failOpen` is `true` (the default), a database error inside the `limit` function or a storage driver error will not block the request — it will pass through and the error is logged to the console. Set `failOpen: false` if you need strict enforcement even during outages.


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