# Environment and secrets

> Set environment variables for a sandbox or a single command, and understand how secrets are stored in recordings.

This is the Markdown of https://docs.dvm.run/guides/environment/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

Use this page to pass environment variables to a whole sandbox or to an individual command, and to see what happens to a secret you pass in, given that every run is recorded.

## Environment variables

Setting `env` on a sandbox adds variables to the image’s environment for every process inside it. Setting `env` on a command adds variables for that command alone, overriding any sandbox variable with the same name.

* Python

  `python/examples/environment.py`

  ```python
  with Sandbox.create("alpine:3.20", env={"STAGE": "test", "LOG_LEVEL": "debug"}) as sandbox:
      print(sandbox.run('echo "$STAGE $LOG_LEVEL"').stdout)
      print(sandbox.run('echo "$STAGE"', env={"STAGE": "override"}).stdout)
      print(sandbox.spec["environment"])
  ```

* TypeScript

  `js/examples/environment.ts`

  ```ts
  await using sandbox = await Sandbox.create("alpine:3.20", {
    env: { STAGE: "test", LOG_LEVEL: "debug" },
  });
  console.log(text((await sandbox.run('echo "$STAGE $LOG_LEVEL"')).stdout));
  console.log(text((await sandbox.run('echo "$STAGE"', { env: { STAGE: "override" } })).stdout));
  console.log(sandbox.spec.environment);
  ```

* Rust

  `rust/dvm-run/examples/environment.rs`

  ```rust
  let sandbox = client
      .create_sandbox("alpine:3.20")
      .env("STAGE", "test")
      .env("LOG_LEVEL", "debug")
      .await?;
  sandbox
      .scope(async |sandbox| {
          let both = sandbox.run(r#"echo "$STAGE $LOG_LEVEL""#).await?;
          print!("{}", String::from_utf8_lossy(&both.stdout));
          let one = sandbox
              .run(r#"echo "$STAGE""#)
              .env("STAGE", "override")
              .await?;
          print!("{}", String::from_utf8_lossy(&one.stdout));
          println!("{:?}", sandbox.spec().environment());
          Ok(())
      })
      .await?;
  ```

Sandbox variable names must contain only letters, digits, and `_`, cannot start with a digit, and must be unique; their values cannot contain a NUL byte. Command variable names cannot be empty or contain `=`.

The sandbox’s spec, returned when you create or fetch the sandbox, includes its sandbox-level environment variables and their values.

## Secrets

A secret becomes part of the recording

Everything you pass into a sandbox is stored: in the sandbox’s spec (visible through the API and the panel) and in its recording. Environment variables, command arguments, stdin, and written files all enter the recording, and files can be read back at any point in the run. Data the program reads from the network, including private data it fetches, is recorded as well.

Give a sandbox only revocable secrets: a key or token scoped to the minimum access needed, revoked once the run finishes. Never pass your dvm.run API key into a sandbox.

When a single command needs a secret, pass it to that command from your own environment instead of setting it on the whole sandbox. That keeps it out of the sandbox’s spec, though it is still stored with the commands you sent and in the recording.

* Python

  `python/examples/environment.py`

  ```python
  token = os.environ.get("EXAMPLE_TOKEN", "not-a-real-token")
  with Sandbox.create("alpine:3.20") as sandbox:
      result = sandbox.run('test -n "$TOKEN" && echo has a token', env={"TOKEN": token})
      print(result.stdout)
  ```

* TypeScript

  `js/examples/environment.ts`

  ```ts
  const token = process.env.EXAMPLE_TOKEN ?? "not-a-real-token";
  await using sandbox = await Sandbox.create("alpine:3.20");
  const result = await sandbox.run('test -n "$TOKEN" && echo has a token', { env: { TOKEN: token } });
  console.log(text(result.stdout));
  ```

* Rust

  `rust/dvm-run/examples/environment.rs`

  ```rust
  let token = std::env::var("EXAMPLE_TOKEN").unwrap_or_else(|_| "not-a-real-token".to_owned());
  let sandbox = client.create_sandbox("alpine:3.20").await?;
  sandbox
      .scope(async |sandbox| {
          let result = sandbox
              .run(r#"test -n "$TOKEN" && echo has a token"#)
              .env("TOKEN", token)
              .await?;
          print!("{}", String::from_utf8_lossy(&result.stdout));
          Ok(())
      })
      .await?;
  ```

Deleting a sandbox in the panel deletes its record, output, and recording. See the [privacy policy](https://dvm.run/privacy/) for what dvm.run retains and for how long.
