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

# Quota keys: scoping limits by tenant, user, or route

> Learn how keyGenerator lets you scope quota limits to any dimension—tenant, user, route, or a combination—using a plain string key.

A quota key is the string that identifies a single quota bucket. Every incoming request is mapped to exactly one key, and the storage driver tracks usage against it. By controlling what `keyGenerator` returns, you control the granularity of your limits: you can give every tenant its own 500-email-per-month pool, cap each user's daily API calls independently, or impose per-route limits across the board.

## How `keyGenerator` works

`keyGenerator` is a required option on `createQuotaLimiter`. It receives the Express `req` object and must return a string. The middleware calls it synchronously on every request, then passes the result directly to the storage driver.

```typescript theme={null}
keyGenerator: (req) => string
```

The function has full access to the request—headers, body, route params, any properties your auth middleware has attached—so you can build keys from any combination of data.

## Examples

```typescript theme={null}
// Per-tenant: one bucket shared across all users in a tenant
keyGenerator: (req) => `quota:emails:${req.tenantId}`

// Per-user: each user gets their own quota
keyGenerator: (req) => `quota:api:${req.userId}`

// Per-route + per-tenant: separate limits per endpoint per tenant
keyGenerator: (req) => `quota:${req.path}:${req.tenantId}`
```

## Key naming best practices

* **Use a consistent prefix** such as `quota:` so keys are easy to identify and scan in Redis.
* **Include the resource type** in the key (e.g. `emails`, `api`, `sms`) so a single tenant can have independent limits for different resources without collisions.
* **Keep keys deterministic** — the same request properties should always produce the same key, otherwise usage will be spread across unrelated buckets.

A key like `quota:emails:tenant_42` is clear, namespaced, and easy to search for in your storage backend.

## Empty keys skip the quota check

If `keyGenerator` returns an empty string, the middleware skips the quota check entirely and calls `next()` without decrementing any counter. A warning is printed to the console:

```
[express-quota-limiter] keyGenerator returned an empty key — skipping quota check.
```

This behavior is intentional—it lets you opt certain requests out of quota enforcement by returning `""` based on conditions (for example, requests from internal service accounts). Be deliberate about it; returning an empty key silently bypasses all quota logic.

## Key TTL

You do not manage key expiry directly in `keyGenerator`. TTL is handled by the storage driver: `RedisDriver` and `MemoryDriver` both default to a 30-day TTL, configurable via the `ttlSeconds` option when constructing the driver. See [Storage drivers](https://express-quota-limiter.mintlify.site/concepts/drivers.md) for details.


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