Overview
Setting environment variables is essential for:- Configuring application settings
- Providing API keys and secrets
- Setting up database connections
- Configuring runtime behavior
Set Single Variable
Set a single environment variable:- Python
- JavaScript
Set Multiple Variables
Set multiple environment variables at once:- Python
- JavaScript
Replace All Variables
Replace all environment variables (removes existing ones):- Python
- JavaScript
Use in Code Execution
Set variables and use them in code:- Python
- JavaScript
Use in Commands
Set variables and use them in shell commands:- Python
- JavaScript
Complete Example
Here’s a complete example showing environment variable setup:- Python
- JavaScript
Best Practices
1
1. Use set() for Single Variables
Use
env.set(key, value) for setting individual variables. This merges with existing variables.2
2. Use set_all() Carefully
set_all() replaces ALL variables. Only use when you want to completely replace the environment.3
3. Prefer update() for Multiple Variables
For setting multiple variables while preserving existing ones, use Updating Environment Variables instead.
4
4. Set Before Execution
Set environment variables before running code or commands that need them.
5
5. Verify After Setting
Always verify variables are set correctly using
get() or get_all() after setting.Related
- Getting Environment Variables - Retrieve environment variables
- Updating Environment Variables - Update variables without replacing
- SDK: sandbox.env.set() - Python SDK method
- API: PUT /env - VM Agent API endpoint
- CLI Environment Variables - Environment variables from CLI
Next Steps
- Learn about Getting Environment Variables to retrieve variables
- Explore Updating Environment Variables to merge with existing variables
- Review Clearing Environment Variables to remove variables

