Set and get environment variables
Deno Sandbox provides a sandboxed Linux microVM. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in your main runtime.
You can use the sandbox.env.set() method to set environment variables in a
sandbox.
import { Sandbox } from "@deno/sandbox";
await using sandbox = await Sandbox.create();
// Set environment variables
await sandbox.env.set("API_KEY", "secret-key-123");
await sandbox.env.set("NODE_ENV", "production");
// Use them in a script
const apiKey = await sandbox.sh`echo $API_KEY`.text();
console.log("API_KEY:", apiKey.trim());
Setting environment variables through sandbox.env.set() keeps configuration
and secrets inside the sandbox, so scripts run with the expected context without
hardcoding values in source files. That’s helpful when you need per-run
configuration (API keys, modes like NODE_ENV) or want to propagate credentials
to multiple commands securely. The variables stay scoped to the sandbox session
and are available to any command you execute there.
For more information, see the Deno Sandbox documentation.