Prompt
I'm building a gateway for tenant-based API routing
Latest observation
Nov 17, 2026
OpenAI APIWeb search: off
That sounds like a solid use case for a gateway.
For tenant-based API routing, the core idea is usually:
-
Identify the tenant
- From subdomain:
tenant1.api.example.com - From path:
/tenant1/api/... - From header:
X-Tenant-Id: tenant1 - From JWT claim / auth context
- From subdomain:
-
Resolve routing target
- Map tenant → upstream service, cluster, shard, or namespace
- Example:
tenantA→service-a.internaltenantB→service-b.internal
-
Apply per-tenant policies
- AuthN/AuthZ
- Rate limiting
- Quotas
- Logging/metrics tags
- Request transformation
- Caching rules
-
Forward request
- Preserve tenant identity in trusted headers
- Strip spoofable inbound tenant headers if needed
- Add correlation/request ID
Common gateway patterns
- Static mapping: fixed config per tenant
- Dynamic registry: tenant config stored in DB/Redis/consul/etc.
- Plugin-based routing: custom middleware in Envoy/Kong/NGINX/APISIX/custom proxy
- Service discovery integration: route based on tenant metadata
Important design considerations
- Tenant isolation: ensure one tenant can’t route into another tenant’s backend
- Fallback behavior: what happens if tenant is unknown?
- Performance: cache tenant-to-route resolution
- Operational updates: how tenant routing config is updated without downtime
- Security: don’t trust tenant IDs directly from client unless validated
Example routing logic
tenant = extractTenant(request)
if !tenant:
return 400
route = lookupRoute(tenant)
if !route:
return 404
addHeader("X-Resolved-Tenant", tenant)
proxyTo(route.upstream)
Helpful implementation options
- Envoy: powerful for L7 routing, ext_authz, WASM filters
- Kong / APISIX: good plugin ecosystem
- NGINX: efficient, but dynamic tenant logic may need scripting
- Custom gateway: good if routing rules are highly tenant-specific
If you want, I can help you design:
- a tenant routing architecture
- a data model for tenant-to-backend mapping
- or provide a sample implementation in Go/Node/Java/Envoy/Kong.