Skip to content

Rate Limiting

Protect your API with configurable rate limits.

Configuration

json
{
  "rateLimit": {
    "enabled": true,
    "default": {
      "windowMs": 60000,
      "max": 100
    }
  }
}

Options

OptionTypeDefaultDescription
windowMsnumber60000Time window in ms
maxnumber100Max requests per window
messagestringCustom error message
statusCodenumber429HTTP status code
skipSuccessfulRequestsbooleanfalseOnly count failed requests
skipFailedRequestsbooleanfalseOnly count successful requests

Per-Endpoint Limits

json
{
  "rateLimit": {
    "enabled": true,
    "default": {
      "windowMs": 60000,
      "max": 100
    },
    "perEndpoint": {
      "/api/heavy": {
        "windowMs": 60000,
        "max": 10
      },
      "/api/auth/login": {
        "windowMs": 300000,
        "max": 5,
        "message": "Too many login attempts"
      }
    }
  }
}

Route-Level Limits

json
{
  "path": "/api/expensive",
  "method": "post",
  "rateLimit": {
    "max": 5,
    "windowMs": 60000
  }
}

Response Headers

Rate limit info in response headers:

X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1704067200

Tenant Rate Limits

Per-tenant limits in tenant config:

json
{
  "rateLimit": {
    "enabled": true,
    "default": {
      "windowMs": 60000,
      "max": 1000
    }
  }
}

Rate Limit Response

When limit exceeded:

json
{
  "error": "Too many requests",
  "message": "Rate limit exceeded. Try again later.",
  "retryAfter": 45
}

HTTP Status: 429 Too Many Requests

Best Practices

  1. Set conservative defaults
  2. Lower limits for authentication endpoints
  3. Higher limits for read operations
  4. Use skipSuccessfulRequests for login endpoints
  5. Monitor rate limit hits in analytics

Released under the ISC License.