Skip to main content
Set environment variables in your sandbox. You can set individual variables or replace all variables at once.

Overview

Setting environment variables is essential for:
  • Configuring application settings
  • Providing API keys and secrets
  • Setting up database connections
  • Configuring runtime behavior
set_all() replaces ALL existing environment variables. Use Updating Environment Variables if you want to merge with existing variables instead.

Set Single Variable

Set a single environment variable:
  • Python
  • JavaScript
from hopx_ai import Sandbox

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

# Set a single variable
sandbox.env.set("API_KEY", "sk-prod-xyz")
print("API_KEY set")

# Verify it's set
api_key = sandbox.env.get("API_KEY")
print(f"API_KEY value: {api_key}")

sandbox.kill()

Set Multiple Variables

Set multiple environment variables at once:
  • Python
  • JavaScript
from hopx_ai import Sandbox

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

# Set multiple variables (replaces all existing)
sandbox.env.set_all({
    "API_KEY": "sk-prod-xyz",
    "DATABASE_URL": "postgres://localhost/db",
    "NODE_ENV": "production",
    "DEBUG": "false"
})

# Verify all are set
env = sandbox.env.get_all()
print(f"Set {len(env)} variables")
for key in ["API_KEY", "DATABASE_URL", "NODE_ENV", "DEBUG"]:
    print(f"  {key}: {env.get(key, 'not set')}")

sandbox.kill()

Replace All Variables

Replace all environment variables (removes existing ones):
  • Python
  • JavaScript
from hopx_ai import Sandbox

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

# Get current variables
current_env = sandbox.env.get_all()
print(f"Current variables: {len(current_env)}")

# Replace all with new set
sandbox.env.set_all({
    "API_KEY": "sk-new-key",
    "DATABASE_URL": "postgres://new-host/db"
})

# Verify replacement
new_env = sandbox.env.get_all()
print(f"New variables: {len(new_env)}")
print(f"Variables: {list(new_env.keys())}")

# Note: System variables like PATH may still exist
sandbox.kill()

Use in Code Execution

Set variables and use them in code:
  • Python
  • JavaScript
from hopx_ai import Sandbox

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

# Set environment variables
sandbox.env.set_all({
    "API_KEY": "sk-123",
    "DATABASE_URL": "postgres://localhost/mydb",
    "DEBUG": "true"
})

# Use in code execution
result = sandbox.run_code('''
import os
print(f"API key: {os.getenv('API_KEY')}")
print(f"Database: {os.getenv('DATABASE_URL')}")
print(f"Debug mode: {os.getenv('DEBUG')}")
''')

print(result.stdout)

sandbox.kill()

Use in Commands

Set variables and use them in shell commands:
  • Python
  • JavaScript
from hopx_ai import Sandbox

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

# Set environment variables
sandbox.env.set_all({
    "API_KEY": "sk-123",
    "NODE_ENV": "production"
})

# Use in commands
result = sandbox.commands.run('echo $API_KEY')
print(f"Command output: {result.stdout}")

result = sandbox.commands.run('echo $NODE_ENV')
print(f"Command output: {result.stdout}")

sandbox.kill()

Complete Example

Here’s a complete example showing environment variable setup:
  • Python
  • JavaScript
from hopx_ai import Sandbox

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

# Configure application environment
print("🔧 Setting up environment...")

sandbox.env.set_all({
    "API_KEY": "sk-prod-xyz123",
    "DATABASE_URL": "postgres://localhost/production",
    "REDIS_URL": "redis://localhost:6379",
    "NODE_ENV": "production",
    "DEBUG": "false",
    "LOG_LEVEL": "info"
})

# Verify setup
env = sandbox.env.get_all()
print(f"\n✅ Environment configured with {len(env)} variables")

# Use in application code
print("\n💻 Testing in code execution...")
result = sandbox.run_code('''
import os

config = {
    "api_key": os.getenv("API_KEY", "not set"),
    "database": os.getenv("DATABASE_URL", "not set"),
    "redis": os.getenv("REDIS_URL", "not set"),
    "node_env": os.getenv("NODE_ENV", "development"),
    "debug": os.getenv("DEBUG", "false"),
    "log_level": os.getenv("LOG_LEVEL", "debug")
}

for key, value in config.items():
    if 'key' in key.lower():
        print(f"{key}: {value[:10]}...")
    else:
        print(f"{key}: {value}")
''')

print(result.stdout)

sandbox.kill()

Best Practices

1

1. Use set() for Single Variables

Use env.set(key, value) for setting individual variables. This merges with existing variables.
2

2. Use set_all() Carefully

set_all() replaces ALL variables. Only use when you want to completely replace the environment.
3

3. Prefer update() for Multiple Variables

For setting multiple variables while preserving existing ones, use Updating Environment Variables instead.
4

4. Set Before Execution

Set environment variables before running code or commands that need them.
5

5. Verify After Setting

Always verify variables are set correctly using get() or get_all() after setting.

Next Steps