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

# TypeScript types and interfaces for express-quota-limiter

> Complete TypeScript type definitions for express-quota-limiter: QuotaStorage, QuotaResult, QuotaOptions, LimitResolver, and callback context interfaces.

All types described on this page are exported directly from `express-quota-limiter`. You can import them by name alongside the runtime exports — no separate `@types/` package is needed.

```typescript theme={null}
import {
  QuotaStorage,
  QuotaResult,
  QuotaOptions,
  LimitResolver,
  QuotaCheckedContext,
  QuotaExceededContext,
} from "express-quota-limiter";
```

***

## `QuotaStorage`

The interface every storage driver must implement. Pass an object satisfying this interface to the `storage` option of `createQuotaLimiter`.

```typescript theme={null}
export interface QuotaStorage {
  decrement(key: string, limit?: number, weight?: number): Promise<QuotaResult>;
  increment(key: string, weight: number): Promise<void>;
}
```

| Method | Description |
| - | - |
| `decrement` | Atomically checks and decrements the quota for `key`. Returns a `QuotaResult` indicating whether the operation was within quota. |
| `increment` | Increments quota usage for `key` by `weight`. Used for manual adjustments or refunds. |

***

## `QuotaResult`

The value returned by `QuotaStorage.decrement`.

```typescript theme={null}
export interface QuotaResult {
  /** Whether the operation was within quota (true = allowed, false = exceeded) */
  success: boolean;
  /** How many operations remain in the current window. Always >= 0. */
  remaining: number;
}
```

***

## `LimitResolver`

The accepted type for the `limit` option in `QuotaOptions`. You can provide a static number or a (possibly async) function that receives the request and returns the limit for that specific call.

```typescript theme={null}
export type LimitResolver = number | ((req: Request) => number | Promise<number>);
```

**Static example:**

```typescript theme={null}
limit: 500
```

**Dynamic example — per-tenant 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" ? 10_000 : 500;
}
```

***

## `QuotaOptions`

The configuration object passed to `createQuotaLimiter`.

```typescript theme={null}
export interface QuotaOptions {
  storage: QuotaStorage;
  keyGenerator: (req: Request) => string;
  limit?: LimitResolver;
  errorMessage?: string;
  failOpen?: boolean;
  onQuotaChecked?: (ctx: QuotaCheckedContext) => Promise<void> | void;
  onQuotaExceeded?: (ctx: QuotaExceededContext) => Promise<void> | void;
  quotaWeight?: (req: Request) => number | Promise<number>;
}
```

| Field | Type | Default | Description |
| - | - | - | - |
| `storage` | `QuotaStorage` | **required** | The storage driver to use |
| `keyGenerator` | `(req: Request) => string` | **required** | Returns the quota key for a request. Return an empty string to skip the check. |
| `limit` | `LimitResolver` | `100` | Max operations per window — static or per-request |
| `errorMessage` | `string` | `"Quota exceeded"` | Message returned in the HTTP 429 body |
| `failOpen` | `boolean` | `true` | Pass requests through on storage errors |
| `onQuotaChecked` | `(ctx: QuotaCheckedContext) => void` | — | Fired after every check (fire-and-forget) |
| `onQuotaExceeded` | `(ctx: QuotaExceededContext) => void` | — | Fired only when a request is blocked (fire-and-forget) |
| `quotaWeight` | `(req: Request) => number \| Promise<number>` | — | How much quota each request consumes. Defaults to `1`. |

***

## `QuotaCheckedContext`

Passed to the `onQuotaChecked` callback after every quota check, whether the request was allowed or blocked.

```typescript theme={null}
export interface QuotaCheckedContext {
  /** The quota key that was checked */
  key: string;
  /** The resolved limit for this request */
  limit: number;
  /** The calculated weight of the current request */
  weight: number;
  /** Whether the request was allowed (true) or blocked (false) */
  success: boolean;
  /** Remaining quota after this operation. Always >= 0. */
  remaining: number;
  /** The incoming request */
  req: Request;
}
```

***

## `QuotaExceededContext`

Passed to the `onQuotaExceeded` callback only when a request is blocked due to quota exhaustion. Note that `remaining` and `success` are omitted — they are always `0` and `false` respectively when this callback fires.

```typescript theme={null}
export interface QuotaExceededContext {
  /** The quota key that was exceeded (e.g. "quota:emails:tenant_42") */
  key: string;
  /** The resolved limit for this request */
  limit: number;
  /** The calculated weight of the current request */
  weight: number;
  /** The incoming request */
  req: Request;
}
```

***

## `RedisDriverOptions`

Options accepted by the `RedisDriver` constructor.

```typescript theme={null}
export interface RedisDriverOptions {
  /**
   * TTL (in seconds) applied when a new quota key is first created.
   * @default 2592000 (30 days)
   */
  ttlSeconds?: number;
}
```

***

## `MemoryDriverOptions`

Options accepted by the `MemoryDriver` constructor.

```typescript theme={null}
export interface MemoryDriverOptions {
  /**
   * TTL (in seconds) for each quota window.
   * @default 2592000 (30 days)
   */
  ttlSeconds?: number;
}
```


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