Skip to main content
Version: 0.3.0
Last Verified: 2025-01-27
Package: hopx-ai on PyPI

Overview

The EnvironmentVariables resource (accessed via sandbox.env) provides methods for managing environment variables in sandboxes. Use this resource to set, get, update, and delete environment variables that are available to all code execution and commands. Environment variables set through this resource persist for the lifetime of the sandbox and are available to all executions.

Access

from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")
env = sandbox.env  # Lazy-loaded

Methods

get_all

Get all environment variables.
env.get_all(*, timeout=None) -> Dict[str, str]
Returns: Dict[str, str] - Dictionary of all environment variables Example:
all_vars = sandbox.env.get_all()
for key, value in all_vars.items():
    print(f"{key}={value}")
Expected Output:
PATH=/usr/local/bin:/usr/bin:/bin
HOME=/root
LANG=en_US.UTF-8
See Also:

get

Get a specific environment variable value.
env.get(key, default=None) -> Optional[str]
Parameters:
  • key (str): Environment variable name
  • default (str, optional): Default value if not found
Returns: Optional[str] - Variable value or default Example:
api_key = sandbox.env.get("API_KEY", "not-set")
print(f"API key: {api_key}")
Expected Output:
API key: not-set
See Also:

set

Set a single environment variable.
env.set(key, value, *, timeout=None) -> Dict[str, str]
Parameters:
  • key (str): Environment variable name
  • value (str): Variable value
  • timeout (int, optional): Request timeout
Returns: Dict[str, str] - Updated dictionary of all environment variables Example:
sandbox.env.set("API_KEY", "secret123")
all_vars = sandbox.env.get_all()
print(all_vars["API_KEY"])  # "secret123"
Expected Output:
secret123
See Also:

set_all

Set/replace all environment variables (destructive).
env.set_all(env_vars, *, timeout=None) -> Dict[str, str]
Parameters:
  • env_vars (Dict[str, str]): Dictionary of environment variables
  • timeout (int, optional): Request timeout
Returns: Dict[str, str] - Updated dictionary of all environment variables
This replaces ALL existing environment variables. Use update() to merge instead.
Example:
sandbox.env.set_all({
    "API_KEY": "secret123",
    "DEBUG": "true",
    "ENV": "production"
})
Expected Output:
All environment variables replaced successfully

update

Update specific environment variables (merge with existing).
env.update(env_vars, *, timeout=None) -> Dict[str, str]
Parameters:
  • env_vars (Dict[str, str]): Dictionary of environment variables to update
  • timeout (int, optional): Request timeout
Returns: Dict[str, str] - Updated dictionary of all environment variables Example:
# Merge new variables with existing
sandbox.env.update({
    "NEW_VAR": "value",
    "API_KEY": "updated-secret"
})
Expected Output:
Environment variables updated successfully
See Also:

delete

Delete a specific environment variable.
env.delete(key, *, timeout=None) -> None
Parameters:
  • key (str): Environment variable name to delete
  • timeout (int, optional): Request timeout
Example:
sandbox.env.delete("OLD_VAR")
Expected Output:
Environment variable deleted successfully

Examples

Example 1: Basic Environment Variable Management

from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")

# Set environment variables
sandbox.env.set("API_KEY", "secret123")
sandbox.env.set("DEBUG", "true")

# Get a variable
api_key = sandbox.env.get("API_KEY")
print(f"API key: {api_key}")

# Get all variables
all_vars = sandbox.env.get_all()
print(f"Total variables: {len(all_vars)}")

sandbox.kill()
Expected Output:
API key: secret123
Total variables: 5

Example 2: Setting Variables at Creation

from hopx_ai import Sandbox

# Set environment variables when creating sandbox
sandbox = Sandbox.create(
    template="code-interpreter",
    env_vars={
        "API_KEY": "secret123",
        "DEBUG": "true",
        "ENV": "development"
    }
)

# Verify they're set
result = sandbox.run_code("import os; print(os.getenv('API_KEY'))")
print(result.stdout)

sandbox.kill()
Expected Output:
secret123

Example 3: Updating Variables

from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")

# Set initial variables
sandbox.env.set_all({
    "VAR1": "value1",
    "VAR2": "value2"
})

# Update specific variables (merge)
sandbox.env.update({
    "VAR2": "updated-value2",
    "VAR3": "value3"
})

# VAR1 still exists, VAR2 updated, VAR3 added
all_vars = sandbox.env.get_all()
print(all_vars)

sandbox.kill()
Expected Output:
{'VAR1': 'value1', 'VAR2': 'updated-value2', 'VAR3': 'value3', 'PATH': '/usr/local/bin:/usr/bin:/bin', 'HOME': '/root'}

Example 4: Using Variables in Code Execution

from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")

# Set environment variable
sandbox.env.set("SECRET", "my-secret-key")

# Use it in code execution
result = sandbox.run_code(
    "import os; print(os.getenv('SECRET'))"
)
print(result.stdout)  # "my-secret-key\n"

sandbox.kill()
Expected Output:
my-secret-key

Example 5: Using Variables in Commands

from hopx_ai import Sandbox

sandbox = Sandbox.create(template="code-interpreter")

# Set environment variable
sandbox.env.set("MY_VAR", "Hello from env")

# Use it in command
result = sandbox.commands.run("echo $MY_VAR")
print(result.stdout)  # "Hello from env\n"

sandbox.kill()
Expected Output:
Hello from env

  • Sandbox - Main sandbox class
  • Commands - Command execution resource
  • Files - File operations resource

See Also

Next Steps