Skip to main content

Documentation Index

Fetch the complete documentation index at: https://e2b-squash-sandbox-pages.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

E2B sandboxes support environment variables at multiple levels: default metadata variables set automatically, global variables set at sandbox creation, and per-command overrides.

Default environment variables

Upon creating a sandbox, useful sandbox metadata is set as environment variables for commands:
  • E2B_SANDBOX is set to true for processes to know if they are inside our VM.
  • E2B_SANDBOX_ID to know the ID of the sandbox.
  • E2B_TEAM_ID to know the team ID that created the sandbox.
  • E2B_TEMPLATE_ID to know what template was used for the current sandbox.
You can try it out by running the following code in the sandbox:
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()
const result = await sandbox.commands.run('echo $E2B_SANDBOX_ID')
These default environment variables are only accessible via the SDK, when using the CLI you can find them in the form of dot files in the /run/e2b/ dir:
user@e2b:~$ ls -a /run/e2b/
.E2B_SANDBOX  .E2B_SANDBOX_ID  .E2B_TEAM_ID  .E2B_TEMPLATE_ID

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.

Global environment variables

You can set global environment variables when creating a sandbox.
import { Sandbox } from 'e2b'

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

Per code execution

You can set environment variables for a specific code execution call in the sandbox.
  • These environment variables are scoped to the command but are not private in the OS.
  • If you had a global environment variable with the same name, it will be overridden only for the command.
import { Sandbox } from '@e2b/code-interpreter'

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

Per command

You can set environment variables for a specific command execution in the sandbox.
  • These environment variables are scoped to the command but are not private in the OS.
  • If you had a global environment variable with the same name, it will be overridden only for the command.
import { Sandbox } from 'e2b'

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