Skip to main content

Set and get environment variables

Deno Sandboxes provide a sandboxed environment for evaluating JavaScript code. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in the 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());

For more information about Sandboxes, see the Sandboxes documentation.

Did you find what you needed?

Privacy policy