Skip to main content

API Gateway

The Zerq API Gateway is the runtime layer that receives every inbound API request from clients, enforces access control, applies rate limiting and quotas, optionally runs a workflow, and forwards the request to your backend.

What this page covers​

  • Required request headers
  • How the gateway authenticates and authorizes each request
  • Rate limiting and quota enforcement
  • Streaming and SSE support
  • IP and method restrictions
  • Common response status codes

Required request headers​

Every request to the gateway must include two identity headers in addition to any auth credentials:

HeaderRequiredDescription
X-Client-IDAlwaysMongoDB ObjectID of the API client
X-Profile-IDAlwaysMongoDB ObjectID of the access profile under that client
AuthorizationWhen authType is token, jwt, or oidcBearer <credential>

For mtls auth, the client certificate is validated at the TLS layer — no Authorization header is needed.

For none auth, no Authorization header is needed; X-Client-ID and X-Profile-ID alone identify the caller.

Example request:

curl https://gateway.yourcompany.com/v1/products \
-H "X-Client-ID: 66a1b2c3d4e5f6a7b8c9d0e1" \
-H "X-Profile-ID: 66a1b2c3d4e5f6a7b8c9d0e2" \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

Auth enforcement sequence​

The gateway runs the following checks in order on every request. A failure at any step returns the error immediately — later steps are not evaluated.


Authentication methods at the gateway​

Auth TypeHow the credential is sentValidation
tokenAuthorization: Bearer <static-token>HMAC comparison against stored token
jwtAuthorization: Bearer <JWT>Signature verified with client/profile-scoped secret
oidcAuthorization: Bearer <OIDC-access-token>Verified via JWKS endpoint of configured OIDC issuer
mtlsClient TLS certificateValidated at TLS termination; no Authorization header needed
noneNo credentialIdentity from headers only; use only on internal or test routes

To configure the auth type for a profile, go to Access Control → Profiles → Auth Management in the management UI, or use the Auth Management UI guide.


Rate limiting and quotas​

Policies attach a two-tier limit to a client:

TierConfig fieldsWindow examples
Rate limitrate_limit_requests + rate_limit_interval100 requests per 1m
Quotaquota_requests + quota_interval50000 requests per 30d

When either limit is exceeded the gateway returns 429 Too Many Requests with a Retry-After header.

Limits are enforced per client (not per profile). All profiles under the same client share the same policy bucket.

Rate limits and quotas run after authentication and are charged to the client the gateway authenticated, never to a caller-supplied X-Client-ID value. Requests rejected by authentication or access checks (for example 401 and 403) never reach the limiter, so they do not count against any client's limits. The gateway limiter therefore does not throttle unauthenticated traffic such as credential guessing; rate-limit that at your ingress or load balancer. Client and profile IDs are matched case-insensitively and normalised to lower case, so changing the case of X-Client-ID does not create a separate bucket.

See Policies for how to create and assign a policy.


IP and method restrictions​

Both restrictions are optional and configured per profile:

  • allowedIPs — list of exact IPs or CIDR ranges. Empty list = allow all.
  • allowedMethods — list of HTTP methods (GET, POST, etc.). Empty list = allow all.

A mismatch returns 403 Forbidden.


Response cache and idempotency​

Both are optional gateway middleware, turned on by the operator with GATEWAY_ENABLE_CACHE and GATEWAY_ENABLE_IDEMPOTENCY. Both run only after the caller has authenticated; unauthenticated requests are never served from, or stored in, either one.

Response cache

  • A cached response is only replayed to a caller that authenticated as the same client and profile with the same credentials. The cache key combines the authenticated client and profile, a fingerprint of every credential-bearing request header (Authorization, Cookie, X-API-Key, token and session headers) and of the client certificate, the Accept, Accept-Encoding, Accept-Language and Origin headers, and the path and query string. Profiles with auth type none are additionally separated by client IP.
  • Only plain 200 responses are stored. Error, 206 and 304 responses are not, nor are responses the upstream marks Cache-Control: no-store or private, or whose Vary names a request header that is not part of the key (Vary: * is never cached).
  • A response served from the cache carries X-Cache: hit and Cache-Control: private, so shared caches in front of the gateway do not store it. The gateway does not add Cache-Control: public to responses.

Idempotency

Requests with an X-Idempotency-Key header (non-safe methods only) are de-duplicated for 30 seconds. Keys are scoped to the authenticated client, profile, HTTP method and path, so the same key from another client, or on another endpoint, never replays a different caller's response.

Streaming and SSE​

The gateway detects streaming responses and disables compression, caching, and etag middleware automatically when:

  • Accept: text/event-stream is present in the request
  • X-Gateway-Streaming: 1 header is set
  • ?streaming=1 query parameter is present

Set streaming: true on the proxy definition in the management UI to opt in at the proxy level. Responses of a streaming proxy are passed through event by event even when the caller sends none of the markers above: compression, ETag, the response cache and idempotency all leave them alone, so a stream is never buffered.


Common response codes​

CodeMeaningCommon cause
400Bad requestMalformed X-Client-ID or X-Profile-ID (not a valid ObjectID)
401UnauthorizedClient/profile not found, inactive, or credential invalid
403ForbiddenIP not in allowlist, HTTP method not allowed, or client lacks access to the collection
404Not foundNo proxy path matches the request path
429Too many requestsRate limit or quota exceeded
502Bad gatewayBackend is unreachable or timed out, or the upstream credential configured on the proxy or collection could not be applied (the request is not forwarded)