Skip to main content
The Gateway API routes requests to downstream services based on URL prefixes. Each route can be configured with authentication requirements and target service URLs.

Route Configuration

Routes are defined with a prefix, environment variable key, and route type:

Route Types

Routing Behavior

URL Rewriting

The gateway strips the route prefix and forwards the remaining path to the target service:

Query Parameters

Query parameters are preserved during URL rewriting:

Headers

The gateway modifies headers before forwarding:
  • Removed: Authorization (prevents token leakage to downstream services)
  • Added: X-Gateway-Route (indicates which route matched)

Authentication Requirements

Required Authentication

/build routes always require authentication:
If no Authorization header is present for /build routes, the gateway returns 401 MISSING_TOKEN.

Optional Authentication

/kv and /core routes verify tokens only if provided:
  • If Authorization header is present → verify token
  • If no header → forward request without verification

Token Route Matching

Tokens must match the requested route (or be CORE/AUTH routes which have elevated privileges):

Routing Flow

Error Handling

All routing errors follow the unified GatewayErrorSchema:

Common Routing Errors

Configuration

Environment Variables

Configure target service URLs in wrangler.toml:

Service Bindings

For internal communication, use Cloudflare service bindings:
Service bindings are preferred over public URLs for security and performance.

Examples

Build Service Request

Gateway forwards to: https://build.metacogna.ai/projects Headers added: X-Gateway-Route: BUILD Headers removed: Authorization

KV Service Request (Unauthenticated)

Gateway forwards to: https://kv.metacogna.ai/data No authentication required (forwarded as-is)

Core Service Request (Authenticated)

Gateway forwards to: https://parti.metacogna.ai/ingest Token verified before forwarding