All Products
Search
Document Center

Function Compute:Environment Variables

Last Updated:Jul 10, 2026

Environment variables let you inject runtime configuration into a Sandbox. They are suitable for non-sensitive switches, task parameters, and tool configuration. Secrets, tokens, and other sensitive information should remain under the control of your application, and should not be written to logs or command output.

Inject Environment Variables When Creating a Sandbox

const sandbox = await Sandbox.create({
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
  envs: {
    NODE_ENV: "production",
    TASK_ID: "task-001",
  },
});

const result = await sandbox.commands.run("echo $TASK_ID");
console.log(result.stdout.trim());

Override Environment Variables When Running a Command

const result = await sandbox.commands.run("echo $TASK_ID", {
  envs: {
    TASK_ID: "task-002",
  },
});

console.log(result.stdout.trim());

Recommendations

  • Sandbox-level envs are suitable for configuration reused by multiple commands.

  • Command-level envs are suitable for one-off execution parameters.

  • Do not rely on environment variables to persist business state. Persistent state shared across Sandboxes should be stored in external storage.