Appearance
Sandbox Environment Variables
Environment variables allow you to pass configuration information when creating a sandbox or executing code/commands. The sandbox supports setting environment variables at multiple scopes.
Default Environment Variables
The sandbox automatically sets the following metadata environment variables:
| Variable Name | Description |
|---|---|
E2B_SANDBOX | Set to true, used by processes to detect if running inside a sandbox |
E2B_SANDBOX_ID | Unique identifier of the sandbox |
E2B_TEMPLATE_ID | Template ID used |
Three Ways to Set Environment Variables
Method 1: Set Global Variables When Creating a Sandbox
Global environment variables are visible to all code and commands throughout the sandbox's lifecycle.
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create({
envs: {
MY_VAR: 'my_value',
},
})
await sandbox.kill()
}
main()Method 2: Set Temporary Variables When Executing Code
These variables are only visible during that specific code execution and can temporarily override global variables.
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
const result = await sandbox.runCode('import os; print(os.environ.get("MY_VAR"))', {
envs: {
MY_VAR: 'my_value',
},
})
await sandbox.kill()
}
main()Method 3: Set Temporary Variables When Running Commands
These variables are only visible during that specific command execution and can temporarily override global variables.
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandbox = await Sandbox.create()
await sandbox.commands.run('echo $MY_VAR', {
envs: {
MY_VAR: '123',
},
})
await sandbox.kill()
}
main()Environment Variable Isolation
Important: Code-level and command-level environment variables are not private within the operating system. They temporarily override global variables but remain visible to other parts of the operating system.