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:
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
Authorizationheader is present → verify token - If no header → forward request without verification
Token Route Matching
Tokens must match the requested route (or beCORE/AUTH routes which have elevated privileges):
Routing Flow
Error Handling
All routing errors follow the unifiedGatewayErrorSchema:
Common Routing Errors
| Code | Status | Description |
|---|---|---|
NOT_FOUND | 404 | No route matched the request path |
MISSING_TARGET | 502 | Environment variable for target service URL not set |
MISSING_TOKEN | 401 | Token required but not provided (for /build routes) |
INVALID_TOKEN | 403 | Token verification failed |
ROUTE_MISMATCH | 403 | Token route doesn’t match requested route |
Configuration
Environment Variables
Configure target service URLs inwrangler.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
https://build.metacogna.ai/projects
Headers added: X-Gateway-Route: BUILD
Headers removed: Authorization
KV Service Request (Unauthenticated)
https://kv.metacogna.ai/data
No authentication required (forwarded as-is)
Core Service Request (Authenticated)
https://parti.metacogna.ai/ingest
Token verified before forwarding
Related Documentation
- Authentication API - Token generation and verification
- Gateway Worker - Implementation details
- Routing Matrix - Complete routing reference