Vercel Environment Variable Fix - Frontend API Base URL (Selective Routing)
Problem
Frontend is hitting wrong backend service for hierarchy endpoints:- Current (WRONG): Hierarchy endpoints hit
payroll-pipeline-cbs-*.uc.a.run.app→ returns 404 - Required (CORRECT): Hierarchy endpoints must hit
payroll-backend-prod-238826317621.us-central1.run.app→ returns 200
Root Cause
The frontend uses route-scoped API base URL selection with selective routing:/api/v1/admin/hierarchy/*and/api/v1/admin/agents/resolver→ useNEXT_PUBLIC_HIERARCHY_API_URL(NEW)- Other
/api/v1/admin/*endpoints → useNEXT_PUBLIC_PIPELINE_API_URL(fallback toNEXT_PUBLIC_API_URL) - All other endpoints → use
NEXT_PUBLIC_API_URL
- Hierarchy endpoints exist ONLY on
payroll-backend-prod(System of Record) - Rate Plans should remain on
payroll-pipeline-cbs(Config Plane) per documented architecture - This preserves the two-service design while fixing hierarchy functionality
Fix Required in Vercel (Selective Routing)
Step 1: Add NEXT_PUBLIC_HIERARCHY_API_URL (NEW - Required)
- Go to Vercel Dashboard → Project Settings → Environment Variables
- Add
NEXT_PUBLIC_HIERARCHY_API_URL: - Ensure it’s set for Production environment (and Preview/Development if needed)
- This is REQUIRED - hierarchy endpoints will fail if not set
Step 2: Verify NEXT_PUBLIC_PIPELINE_API_URL (Keep as-is)
- Check current value of
NEXT_PUBLIC_PIPELINE_API_URL - DO NOT CHANGE - should remain pointing to
payroll-pipeline-cbs(Config Plane) - This is used for Rate Plans and other admin endpoints
Step 3: Verify NEXT_PUBLIC_API_URL (Keep as-is)
- Check if
NEXT_PUBLIC_API_URLexists - Should point to
payroll-backend-prod-238826317621.us-central1.run.app(System of Record) - Used for CEO metrics, commissions, leaderboard, and as fallback for admin endpoints
Step 4: Trigger Redeploy
After updating env vars:- Go to Vercel Dashboard → Deployments
- Click “Redeploy” on the latest deployment, OR
- Push a no-op commit to trigger auto-deploy:
Validation
After redeploy, verify in browser DevTools → Network tab:-
Navigate to:
/admin/identity-overrides?tab=hierarchy - Click: “Export Agent IDs” or trigger any hierarchy endpoint
-
Check Network tab:
- Request URL should be:
https://payroll-backend-prod-238826317621.us-central1.run.app/api/v1/admin/agents/resolver - Status should be:
200 OK(not 404) - Headers should include:
X-Org-Id: creative_benefit_strategies
- Request URL should be:
-
Verify Rate Plans still work:
- Navigate to Rate Plans tab
- Check Network tab - Rate Plans calls should hit
payroll-pipeline-cbshost - Verify Rate Plans functionality works correctly
Code Reference
File:dashboard/src/lib/apiClient.ts
- Function:
getBaseUrlForPath(path: string) - Lines: 25-97
- Routing Logic (selective):
/api/v1/admin/hierarchy/*OR/api/v1/admin/agents/resolver→NEXT_PUBLIC_HIERARCHY_API_URL(required)- Other
/api/v1/admin/*→NEXT_PUBLIC_PIPELINE_API_URL(fallback:NEXT_PUBLIC_API_URL) - All other endpoints →
NEXT_PUBLIC_API_URL
Related Fixes
Selective Routing Implementation (Code Change)
- File:
dashboard/src/lib/apiClient.ts - Change: Added hierarchy-specific routing using
NEXT_PUBLIC_HIERARCHY_API_URL - Rationale:
- Hierarchy endpoints exist ONLY on
payroll-backend-prod(System of Record) - Rate Plans should remain on
payroll-pipeline-cbs(Config Plane) - Preserves documented two-service architecture
- Hierarchy endpoints exist ONLY on
Logout Behavior Fix (Code Change)
- File:
dashboard/src/lib/apiClient.ts - Change: Only 401 triggers logout. 403/404 do NOT trigger logout.
- Rationale:
- 401 = Authentication failed (session expired) → logout required
- 403 = Authorization failed (valid auth, insufficient permissions) → do NOT logout
- 404 = Resource not found (valid auth, endpoint missing) → do NOT logout
Environment Variable Summary
| Env Var | Value | Used For |
|---|---|---|
NEXT_PUBLIC_HIERARCHY_API_URL | https://payroll-backend-prod-238826317621.us-central1.run.app | Hierarchy endpoints + resolver (NEW) |
NEXT_PUBLIC_PIPELINE_API_URL | https://payroll-pipeline-cbs-*.uc.a.run.app | Rate Plans + other admin endpoints (unchanged) |
NEXT_PUBLIC_API_URL | https://payroll-backend-prod-238826317621.us-central1.run.app | CEO metrics, commissions, leaderboard (unchanged) |