EnvironmentVariables resource provides methods for managing environment variables inside sandboxes at runtime.
Accessing Environment Variables Resource
Getting Environment Variables
get_all()
Get all environment variables.
get()
Get a specific environment variable value.
key(str): Environment variable namedefault(str, optional): Default value if variable doesn’t exist
Setting Environment Variables
set()
Set a single environment variable (merges with existing).
key(str): Environment variable namevalue(str): Environment variable valuetimeout(int, optional): Request timeout
set_all()
Replace all environment variables (destructive - removes existing vars not in the provided object).
update() if you want to merge instead.
Example:
update()
Update specific environment variables (merge with existing).
Deleting Environment Variables
delete()
Delete an environment variable.
key(str): Environment variable name to deletetimeout(int, optional): Request timeout
Environment Variable Priority
When executing code or commands, environment variables are applied in this order:- Execution-specific env vars (from
run_code()orcommands.run()envparameter) - Global sandbox env vars (from
sandbox.env) - Agent default env vars (system defaults)
Best Practices
1. Use update() Instead of set_all()
Preserve system variables:
2. Set Variables at Sandbox Creation
Set environment variables when creating the sandbox:3. Use Environment Variables for Secrets
Never hardcode secrets in code:4. Clean Up Temporary Variables
Delete temporary variables when done:Examples
Application Configuration
Database Connection
Multi-Stage Configuration
Related
- Sandbox Class - Main sandbox class
- Code Execution - Execute code with env vars
- Commands - Run commands with env vars

