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

# Quickstart: add quota limiting to Express in minutes

> Install express-quota-limiter, choose a storage driver, and attach your first quota middleware to an Express route in under five minutes.

express-quota-limiter is a single middleware function returned by `createQuotaLimiter`. You pass it a storage driver, a function that derives a quota key from the request, and a limit. The middleware then counts usage against that key and responds with HTTP 429 when the quota is exhausted.

## Installation

```bash npm theme={null}
npm install express-quota-limiter
```

```bash yarn theme={null}
yarn add express-quota-limiter
```

### Peer dependencies

The package has zero mandatory runtime dependencies. Install a Redis client only if you plan to use `RedisDriver`:

```bash ioredis theme={null}
npm install ioredis
```

```bash node-redis theme={null}
npm install redis
```

`MemoryDriver` has no peer dependencies and works out of the box.

## Setup

**1. Install the package**

Run one of the install commands above. The package ships with TypeScript types — no separate `@types/` package is needed.

**2. Choose a driver**

Pick the storage driver that fits your environment:

* **`RedisDriver`** — for production and staging. Compatible with both `ioredis` and `node-redis` v4. Uses a MULTI/EXEC pipeline to atomically initialise and decrement the counter with no race conditions.
* **`MemoryDriver`** — for local development and unit tests. No external dependencies. Not suitable for multi-process deployments because each process maintains its own isolated store.

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

// Production: Redis (30-day TTL by default)
const redis = new Redis();
const redisDriver = new RedisDriver(redis, {
  ttlSeconds: 60 * 60 * 24 * 30, // 30 days (default)
});

// Development / tests: in-memory
const memoryDriver = new MemoryDriver({ ttlSeconds: 3600 }); // 1 hour window
```

**3. Create the middleware**

Call `createQuotaLimiter` with your driver and options. The `keyGenerator` function receives the Express `Request` object and must return a string that uniquely identifies the quota bucket — for example, a tenant ID or user ID.

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

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

> [!TIP]
>
> `limit` can be an async function if you need to resolve the cap dynamically — for example, looking up the tenant's plan in a database.

**4. Attach to an Express route**

Pass the middleware directly to any Express route or router:

```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: 500,
  keyGenerator: (req) => `quota:emails:${req.tenantId}`,
  errorMessage: "Monthly email quota exceeded",
});

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

When the quota is exhausted the middleware responds with `HTTP 429` before your handler runs:

```json theme={null}
{
  "error": "Too Many Requests",
  "message": "Monthly email quota exceeded",
  "quota": { "limit": 500, "remaining": 0 }
}
```

Every response — allowed or blocked — also receives two headers:

| Header | Description |
| - | - |
| `Quota-Remaining` | Remaining operations in the current window |
| `Quota-Limit` | The configured limit |

## Complete example

The following is a working TypeScript example that combines all four steps:

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

const app = express();

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

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

> [!WARNING]
>
> `MemoryDriver` stores quota state in-process. If you run multiple Node.js processes or deploy to more than one server, each process will maintain its own independent counter. Use `RedisDriver` for any multi-process or multi-server deployment.

## Next steps

> [card] [Quota keys](https://express-quota-limiter.mintlify.site/concepts/quota-keys.md)
>
> Learn how to design keys for per-tenant, per-user, and per-route quotas.

> [card] [Multi-tenant guide](https://express-quota-limiter.mintlify.site/guides/multi-tenant.md)
>
> Enforce different limits per tenant plan with dynamic `limit` resolution.

> [card] [createQuotaLimiter reference](https://express-quota-limiter.mintlify.site/reference/create-quota-limiter.md)
>
> Full reference for all options including callbacks and request weighting.


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