Service Routing
The Partner API Gateway routes requests to appropriate backend services based on the request path.
Service Routing Configuration
Pharmacy Service
Route: /pharmacy/*
Proxy Target: pharmacy-api-gateway worker
Environment Variable: PHARMACY_API_GATEWAY_URL
Why proxy through pharmacy-api-gateway:
- Handles Cloudflare Access for docs endpoints
- Adds worker authentication to Firebase Functions
- Maintains service-specific concerns in one place
- Provides consistent pattern for future service gateways
Other Services
- Brand:
/brand/*→BRAND_SERVICE_URL - Clinician:
/clinician/*→CLINICIAN_SERVICE_URL - Shared:
/shared/*→SHARED_SERVICE_URL
Service Client
File: packages/api/src/services/client.ts
The service client handles:
- HTTP proxying to backend services
- Partner context header injection
- Request/response transformation
- Timeout and error handling
Usage
import { getPartnerServiceClient } from '../services/client';
const client = getPartnerServiceClient('pharmacy', c);
const response = await client.request({
method: 'GET',
path: '/orders/123',
});Request Flow
- Partner request arrives at Partner API Gateway
- Authentication middleware validates credentials
- Rate limiting middleware checks limits
- Service access middleware verifies permissions
- Service client routes to appropriate backend
- Response returned to partner
Error Handling
Errors from backend services bubble through the gateway:
- 4xx errors: Returned as-is (client errors)
- 5xx errors: Returned as-is (server errors)
- Timeout errors: 504 Gateway Timeout
- Network errors: 502 Bad Gateway