ENVIRONMENT_API_KEY is a credential used to authenticate requests between the Synth training backend and your task apps. It’s separate from SYNTH_API_KEY (which authenticates with the Synth backend) and is scoped to your organization.
Purpose
ENVIRONMENT_API_KEY serves as the authentication token that:
-
Authenticates backend-to-task-app requests: When the Synth training backend makes requests to your task app endpoints (like
/rollout,/health,/task_info), it includes this key in theX-API-Keyheader. -
Secures task app endpoints: Your task app validates incoming requests by comparing the
X-API-Keyheader against theENVIRONMENT_API_KEYenvironment variable set in the task app’s runtime environment. -
Enables organization-scoped access: Each organization has its own
ENVIRONMENT_API_KEYstored encrypted in the backend database, ensuring that training jobs can only access task apps belonging to the same organization.
How It Works
Storage and Retrieval
The backend storesENVIRONMENT_API_KEY credentials in the customer_credentials table with credential_type = 'ENVIRONMENT_API_KEY'. Each credential is:
- Encrypted using sealed-box encryption (NaCl) with organization-specific public/private key pairs
- Scoped to an organization via
org_id - Marked as active via
credential_metadata.is_activeflag - Optionally scoped to an environment via
credential_metadata.env(e.g., “dev”, “prod”)
- Retrieves the active
ENVIRONMENT_API_KEYfor the organization from the database - Decrypts it using the organization’s private key
- Includes it in HTTP requests to the task app as the
X-API-Keyheader
Task App Validation
Task apps validate incoming requests by:- Reading
ENVIRONMENT_API_KEYfrom their runtime environment variables - Comparing it against the
X-API-Keyheader (orAuthorization: Bearerheader) in incoming requests - Returning
401 Unauthorizedif the keys don’t match, or503 Service UnavailableifENVIRONMENT_API_KEYis not configured in the task app environment
normalize_environment_api_key() and is_api_key_header_authorized() functions (from synth_ai.task.auth) to handle this validation consistently.
Client-Side Usage
When using the CLI or SDK locally:- Fetch credentials: Run
uvx synth-ai setupto retrieve bothSYNTH_API_KEYandENVIRONMENT_API_KEYfrom your Synth account - Store locally: The CLI writes these to
.envfiles and~/.synth-ai/config.json - Use for health checks: The CLI uses
ENVIRONMENT_API_KEYto verify task app health before submitting training jobs - Pass to task apps: When deploying task apps, ensure
ENVIRONMENT_API_KEYis available in the deployment environment (via environment variables, Modal secrets, etc.)
When It’s Required
ENVIRONMENT_API_KEY is required for:
- Reinforcement Learning training: The backend needs it to call
/rolloutendpoints during training - Prompt Optimization (GEPA/MIPRO): The orchestrator uses it to evaluate prompt candidates via
/rollout - Task app health checks: The CLI validates task app availability before job submission
- Periodic evaluation: During RL training, periodic evaluation rollouts require
ENVIRONMENT_API_KEYto be available in the trainer environment
Common Issues
Missing Active Key
If the backend cannot find an activeENVIRONMENT_API_KEY for your organization, you’ll see errors like:
ENVIRONMENT_API_KEY you used to start your task app is uploaded to the same organization as your SYNTH_API_KEY. Run synth-ai setup to fetch and store both credentials.
Task App Authentication Failures
If your task app returns401 Unauthorized or 503 Service Unavailable:
- 503: The task app is missing
ENVIRONMENT_API_KEYin its environment. Ensure it’s set when deploying (via Modal secrets, environment variables, etc.) - 401: The key in the request header doesn’t match the task app’s
ENVIRONMENT_API_KEY. Verify both are from the same organization and the task app is using the correct key
Key Mismatch Between Backend and Task App
TheENVIRONMENT_API_KEY used by the backend must match the one configured in your task app. If they differ:
- The backend retrieves the key from the organization’s stored credentials
- Your task app reads it from its environment variable
- Both must be the same value for authentication to succeed
ENVIRONMENT_API_KEY to your Synth organization that you’re using in your task app deployment.
Security Considerations
- Organization isolation: Each organization’s
ENVIRONMENT_API_KEYis encrypted and stored separately, preventing cross-organization access - Encryption at rest: Keys are stored encrypted in the database using sealed-box encryption
- Environment scoping: Keys can be scoped to specific environments (dev/prod) to prevent accidental cross-environment access
- Active flag: Only active credentials are used, allowing you to rotate keys by deactivating old ones and creating new ones
Related Concepts
- SYNTH_API_KEY: Authenticates with the Synth backend API (different from
ENVIRONMENT_API_KEY) - Task App Authentication: See task app documentation for details on implementing authentication endpoints
- Credential Management: Use
synth-ai setupor the backend API to manage organization credentials