Skip to main content
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

Peer dependencies

The package has zero mandatory runtime dependencies. Install a Redis client only if you plan to use RedisDriver:
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.
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.
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:
When the quota is exhausted the middleware responds with HTTP 429 before your handler runs:
Every response — allowed or blocked — also receives two headers:

Complete example

The following is a working TypeScript example that combines all four steps:
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

Quota keys

Learn how to design keys for per-tenant, per-user, and per-route quotas.

Multi-tenant guide

Enforce different limits per tenant plan with dynamic limit resolution.

createQuotaLimiter reference

Full reference for all options including callbacks and request weighting.