Internal
Usage Tracking

Usage Tracking

The Partner API Gateway tracks API usage per partner for analytics, billing, and monitoring.

What Gets Tracked

For each API request, the following is recorded:

  • Partner ID
  • Tenant ID
  • Service name (brand, clinician, pharmacy, shared)
  • Endpoint path
  • HTTP method
  • Response status code
  • Response time (milliseconds)
  • Timestamp

Storage Options

Usage data can be stored in multiple ways:

Option 1: Cloudflare KV

Environment Variable: USAGE_KV

Stores usage records in KV with 30-day TTL:

  • Key: usage:{partnerId}:{timestamp}
  • Value: JSON-encoded usage record

Option 2: Analytics Service

Environment Variable: ANALYTICS_URL

Sends usage data to external analytics service via HTTP POST.

Option 3: Console Logging

In development mode, usage is logged to console for debugging.

Implementation

File: packages/api/src/middleware/usage-tracking.ts

The middleware:

  1. Captures request start time
  2. Executes request
  3. Calculates response time
  4. Records usage (non-blocking)
  5. Returns response

Usage Analytics

Usage data can be used for:

  • Billing: Calculate costs based on API calls
  • Analytics: Understand partner usage patterns
  • Monitoring: Track API health and performance
  • Capacity Planning: Identify high-usage partners

Privacy

Usage tracking includes:

  • Partner identification
  • Service and endpoint accessed
  • Response times and status codes

Does NOT include:

  • Request/response bodies
  • Sensitive data
  • User information