Skip to main content

Environment variables

There are multiple ways to configure environment variables in Blaxel sandboxes.
On the new sandbox generation, you can update environment variables on a running sandbox, or give them a new value when you fork a sandbox. Both are in preview. Other parts of the runtime spec, such as memory, cannot be changed on a running sandbox.

When building new sandbox images

Environment variables defined in the sandbox image with the ENV Dockerfile directive are available in every sandbox created from it.
Never add secrets in a Dockerfile.
This also applies when creating sandbox images with the declarative image builder:

When instantiating sandboxes from existing images

When instantiating sandboxes from existing images, any environment variables defined in the base image are automatically available. Additional environment variables can be set using the following methods.

Set variables at sandbox creation time

Pass envs as an array of name/value objects when creating a sandbox with the Blaxel SDKs. These are set as environment variables and are available to every process running inside the deployed sandbox by default (although they can be overridden by process-level variables, discussed in the next section).

Update variables on an existing sandbox

Call updateEnvs (update_envs in Python) with the variables to set. Each variable you pass takes the new value, and every other variable of the sandbox is kept. Pass null (None in Python) as a value to remove a variable.
To mark a variable as secret, pass a list of variables with secret set. When you later change the value of a secret variable by name, it stays secret.
To replace every variable of the sandbox with the ones you pass, set replace:
The API masks variable values as **** in its responses, including in the sandbox the SDK returns. Variables you do not pass keep their real values.
The update takes a few seconds to reach the sandbox. Processes you start before then still read the old values. How it applies depends on the infrastructure generation of the sandbox:
  • On Carbon (Mark 3.1), the sandbox keeps running. Processes you start afterwards read the new values, and processes already running keep the environment they were started with.
  • On Mark 3.0, the sandbox restarts to apply the change. Its files and running processes are lost.
On Mark 3.0, store data you need to keep on a volume before you update the variables of a sandbox.
To keep the state of a sandbox and run it with a different environment, fork it with new variables instead. Without the SDK, read the sandbox, change its spec.runtime.envs list, and send the whole sandbox back. The update replaces the whole sandbox definition, so change nothing else: a change to another field, memory for example, redeploys the sandbox and loses its state.
HTTP API

When executing sandbox processes

Environment variables can also be set at process execution, applicable only to that specific process.

Secrets

The recommended way to inject secrets into a sandbox is with the Blaxel proxy. This intercepts outbound HTTPS requests from the sandbox and injects secrets server-side using {{SECRET:name}} placeholders. The sandbox code never sees raw API keys or credentials. See the proxy routing with secrets injection documentation for examples.

Use a .env.build file

Build variables let you pass secrets and configuration values into the Docker build phase without exposing them at runtime. This is useful when your build process needs credentials that should never appear inside the deployed sandbox. Create a .env.build file in the root of your project for build secrets. A common example of this is installing private npm packages, which require an NPM_TOKEN during npm install. Variables defined here are injected during the build phase only and are never persisted in the runtime environment.
.env.build
Use the --build-env-file argument to bl deploy to specify a custom file name or path instead of the default .env.build.
Ensure that .env.build is ignored during commits to avoid accidentally making secrets public.
Last modified on November 18, 2026