Rate Limiting
Algorithm
The portal uses a sliding window rate limiter stored in-memory per API key.
Window: 60 secondsLimit: N requests per window (set by plan or subscription override)
On each validate call: 1. Load timestamps[] for this API key 2. Remove all timestamps older than (now - 60s) 3. If timestamps.length >= limit → reject 429 4. Else → push current timestamp, proceedThis gives a true sliding window (not fixed buckets), so a burst at the window boundary doesn’t reset the counter.
Limits by plan
| Plan | Requests / min |
|---|---|
free |
10 |
professional |
100 |
enterprise |
1,000 |
Overriding per subscription
PUT /api/subscriptions/:id{ "rateLimit": 50 }Set to null to revert to the plan default.
Response headers
Every gateway validate response includes these headers — forward them to clients:
| Header | Value |
|---|---|
X-RateLimit-Limit |
Allowed requests per minute |
X-RateLimit-Remaining |
Requests remaining in current window |
X-RateLimit-Reset |
Seconds until oldest request in window expires |
When rate limited:
HTTP 429X-RateLimit-Limit: 10X-RateLimit-Remaining: 0X-RateLimit-Reset: 23Important: in-memory storage
Rate limit state is held in a Map<string, number[]> in the Node process. This means:
- Restarts clear all rate limit counters — fine for dev, acceptable for most prod deployments
- Multiple API instances won’t share state — if you run multiple portal replicas, move the rate limit store to Redis
To add Redis-backed rate limiting, replace the in-memory map with:
import { Redis } from 'ioredis';const redis = new Redis(process.env.REDIS_URL);
async function checkRateLimit(apiKey: string, limitPerMin: number): Promise<boolean> { const key = `rl:${apiKey}`; const now = Date.now(); const windowStart = now - 60_000;
await redis.zremrangebyscore(key, '-inf', windowStart); const count = await redis.zcard(key); if (count >= limitPerMin) return false;
await redis.zadd(key, now, `${now}`); await redis.expire(key, 120); return true;}Testing rate limits
# Hit the rate limit quickly (free plan = 10 req/min)for i in {1..12}; do curl -s -X POST http://localhost:3000/api/gateway/validate \ -H "Content-Type: application/json" \ -d '{"apiKey":"ltg_live_...","method":"GET","path":"/test"}' \ | jq '.valid, .error'doneThe 11th and 12th calls should return "valid": false.