The system has been refactored to support per-request environment configuration instead of a global IS_SANDBOX flag. This allows the API to handle both production and sandbox environments simultaneously.
IS_SANDBOX from /backend/plans/config.tsClients can specify the environment using:
X-Environment: SANDBOX or X-Environment: PRODUCTION?environment=sandbox or ?environment=productionAll database query functions now accept an optional environment parameter:
getPlanDetails(customerId, environment)getLatestSubscriptionPrice(customerId, environment)getSubscriptionHistory(customerId, environment)getNonExpiringBalance(customerId, environment)listGrants(customerId, environment)ensureFreeGrant(customerId, amount, environment)getUserUsageStats(customerId, environment)All endpoints now respect the environment parameter:
/api/usage/api/credits/balance/admin/credits/:customerIdauthenticateUser middlewareThe webhook continues to:
curl https://api.example.com/api/credits/balance \ -H "Authorization: Bearer CUSTOMER_ID"
curl https://api.example.com/api/credits/balance \ -H "Authorization: Bearer CUSTOMER_ID" \ -H "X-Environment: SANDBOX"
curl https://api.example.com/api/credits/balance?environment=sandbox \ -H "Authorization: Bearer CUSTOMER_ID"
Run the test script to verify the changes:
chmod +x test-environment-config.sh ./test-environment-config.sh
sandbox, SANDBOX, Sandbox all work)