Skip to main content
This page covers how to set and use environment variables in a sandbox, and default environment variables inside the sandbox.

Default environment variables

Knowing if you are inside a sandbox

Sometimes it’s useful to know if the code is running inside a sandbox. 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_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:
These default environment variables are set for commands and terminals started through the SDK or the CLI. They are also stored as dot files in the /run/e2b/ directory. On a recently built template, the directory looks like this:
Templates built with an older version of envd may not set some or all of these variables and may not have the /run/e2b/ directory. To get these variables and the directory, rebuild the template.

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.

2. Setting environment variables when running code

You can set environment variables for 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.

3. Setting environment variables when running commands

You can set environment variables for 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.