# Environment variables - Documentation

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown is available with `Accept: text/markdown` and `.md` URL variants.

Source: /docs/guides/sandbox-environment-variables

# Environment variables

##

[​](#setting-environment-variables)

Setting environment variables

There are 3 ways to set environment variables in a sandbox:

- [Global environment variables when creating the sandbox](#1-global-environment-variables).

- [When running code in the sandbox](#2-setting-environment-variables-when-running-code).

- [When running commands in the sandbox](#3-setting-environment-variables-when-running-commands).

###

[​](#1-global-environment-variables)

1. Global environment variables

You can set global environment variables when creating a sandbox.

JavaScript & TypeScript

Python

```
import { Sandbox } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create({
envs: {
MY_VAR: 'my_value',
},
})

const result = await sandbox.commands.run("echo $MY_VAR")

console.log(result)

// Output example:
// { exitCode: 0, error: undefined, stdout: 'my_value\n', stderr: '' }

await sandbox.kill()
```

```
from novita_sandbox.code_interpreter import Sandbox

sandbox = Sandbox.create(
envs={
'MY_VAR': 'my_value',
},
)

result = sandbox.commands.run("echo $MY_VAR")

print(result)

# Output example:
# { exitCode: 0, error: undefined, stdout: '123\n', stderr: '' }

sandbox.kill()
```

###

[​](#2-setting-environment-variables-when-running-code)

2. Setting environment variables when running code

You can set environment variables while running specific code in the sandbox.

If a global environment variable has the same name, it will be overridden.

JavaScript & TypeScript

Python

```
import { Sandbox } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create()

const result = await sandbox.runCode('import os; print(os.environ.get("MY_VAR"))', {
envs: {
MY_VAR: 'my_value',
},
})
console.log(result.logs)

await sandbox.kill()
```

```
from novita_sandbox.code_interpreter import Sandbox

sandbox = Sandbox.create()
result = sandbox.run_code(
'import os; print(os.environ.get("MY_VAR"))',
envs={
'MY_VAR': 'my_value'
}
)

print(result)

sandbox.kill()
```

###

[​](#3-setting-environment-variables-when-running-commands)

3. Setting environment variables when running commands

You can set environment variables while executing specific commands in the sandbox.

If a global environment variable has the same name, it will be overridden.

JavaScript & TypeScript

Python

```
import { Sandbox } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create()

const result = await sandbox.commands.run('echo $MY_VAR', {
envs: {
MY_VAR: '123',
},
})

console.log(result)

// Output example:
// { exitCode: 0, error: undefined, stdout: '123\n', stderr: '' }

await sandbox.kill()
```

```
from novita_sandbox.code_interpreter import Sandbox

sandbox = Sandbox.create()
result = sandbox.commands.run(
'echo $MY_VAR',
envs={
'MY_VAR': '123'
}
)

print(result)

# Output example:
# CommandResult(stderr='', stdout='123\n', exit_code=0, error='')

sandbox.kill()
```

Last modified on June 24, 2026
