Setting environment variables

There are 3 ways to set environment variables in a sandbox:

  1. Global environment variables when creating the sandbox.
  2. When running code in the sandbox.
  3. When running commands in the sandbox.

1. Global environment variables

You can set global environment variables when creating a sandbox.

import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await Sandbox.create({
  env: { 
    MY_VAR: 'my_value', 
  }, 
})

2. Setting environment variables when running code

You can set environment variables for specific code execution call in the sandbox.

const sandbox = await Sandbox.create()
const result = await sandbox.runCode('import os; print(os.environ.get("MY_VAR"))', {
  envs: { 
    MY_VAR: 'my_value', 
  }, 
})

3. Setting environment variables when running commands

You can set environment variables for specific command execution in the sandbox.

const sandbox = await Sandbox.create()
sandbox.commands.run('echo $MY_VAR', {
  envs: { 
    MY_VAR: '123', 
  }, 
})