Gateway & Rate Limiting
POST /api/gateway/validate
The central enforcement point. Call this before forwarding any request to your upstream API.
Request body
{ apiKey: string; // Required. The subscriber's API key method?: string; // HTTP method (GET, POST, etc.) — for scope check path?: string; // Request path — for package permission check}Success response 200
{ "valid": true, "subscriptionId": "sub_abc123", "productId": "prod_xyz", "plan": "professional", "scope": "write", "name": "Acme Corp", "headers": { "X-RateLimit-Limit": "100", "X-RateLimit-Remaining": "87", "X-RateLimit-Reset": "42", "X-Quota-Limit": "10000", "X-Quota-Used": "234", "X-Quota-Remaining": "9766" }}Failure responses
401 — Invalid or missing key:
{ "valid": false, "error": "Invalid API key" }403 — Scope violation:
{ "valid": false, "error": "Scope 'read' does not allow POST requests" }429 — Rate limit exceeded:
{ "valid": false, "error": "Rate limit exceeded: 10 req/min" }429 — Daily quota exceeded:
{ "valid": false, "error": "Daily quota exceeded: 100 calls/day" }403 — Package permission denied:
{ "valid": false, "error": "Endpoint not permitted by assigned packages", "assignedPackages": ["Read-Only Tickets"]}Rate limiting
Rate limiting uses a sliding window algorithm per API key:
- Window: 60 seconds
- Counter: number of requests in the last 60 seconds
- On each validate call: old timestamps outside the window are pruned, the new timestamp is added, and the count is checked against the plan limit
Rate limits are stored in-memory (per process). In a multi-instance deployment, use a shared Redis store instead.
Rate limit headers
| Header | Meaning |
|---|---|
X-RateLimit-Limit |
Requests allowed per minute |
X-RateLimit-Remaining |
Remaining requests in current window |
X-RateLimit-Reset |
Seconds until window resets |
Custom rate limits
Override the plan default per subscription:
PUT /api/subscriptions/:id{ "rateLimit": 50 }Daily quota
Daily quotas reset at midnight UTC. The quota counter is stored on the subscription record and incremented atomically on each successful validate call.
Quota headers
| Header | Meaning |
|---|---|
X-Quota-Limit |
Total calls allowed today |
X-Quota-Used |
Calls made today |
X-Quota-Remaining |
Remaining calls today |
Checking usage
GET /api/subscriptions/:id/usage{ "callCountToday": 234, "callLimit": 10000, "rateLimit": 100, "remainingToday": 9766, "resetDate": "2024-01-16T00:00:00.000Z"}Integration pattern
Typical nginx integration:
location / { # Validate the API key before proxying auth_request /validate; auth_request_set $rate_limit_remaining $upstream_http_x_ratelimit_remaining;
proxy_pass http://upstream; add_header X-RateLimit-Remaining $rate_limit_remaining;}
location = /validate { internal; proxy_pass http://portal:3000/api/gateway/validate; proxy_pass_request_body off; proxy_set_header Content-Type "application/json"; proxy_set_header X-Original-Method $request_method; proxy_set_header X-Original-URI $request_uri;}Or in a Hono/Express middleware:
app.use(async (c, next) => { const apiKey = c.req.header('X-API-Key'); const res = await fetch('http://portal:3000/api/gateway/validate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ apiKey, method: c.req.method, path: new URL(c.req.url).pathname, }), }); const data = await res.json(); if (!data.valid) return c.json({ error: data.error }, res.status); // Forward rate limit headers Object.entries(data.headers).forEach(([k, v]) => c.res.headers.set(k, v)); await next();});