Internal
Service Routing

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

  1. Partner request arrives at Partner API Gateway
  2. Authentication middleware validates credentials
  3. Rate limiting middleware checks limits
  4. Service access middleware verifies permissions
  5. Service client routes to appropriate backend
  6. 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