Retrieve environment variables from your sandbox. You can get all variables at once or fetch specific variables by name.
Overview
Getting environment variables is useful for:
- Inspecting current environment configuration
- Verifying variables are set correctly
- Accessing system and custom variables
- Debugging environment-related issues
Environment variables include both system variables (like PATH, HOME) and custom variables you’ve set. Sensitive values may be masked in responses.
Get All Environment Variables
Retrieve all environment variables:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Get all environment variables
env = sandbox.env.get_all()
print(f"Total variables: {len(env)}")
# Access specific variables
print(f"PATH: {env.get('PATH', 'not set')}")
print(f"HOME: {env.get('HOME', 'not set')}")
sandbox.kill()
Get Specific Variable
Get a single environment variable:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Get a specific variable
api_key = sandbox.env.get("API_KEY")
if api_key:
print(f"API key: {api_key[:10]}...")
else:
print("API_KEY not set")
# Get with default value
db_url = sandbox.env.get("DATABASE_URL", "postgres://localhost/db")
print(f"Database URL: {db_url}")
sandbox.kill()
List All Variables
Iterate through all environment variables:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Get all variables
env = sandbox.env.get_all()
# List all variables
print("Environment Variables:")
for key, value in sorted(env.items()):
# Mask sensitive values
if any(sensitive in key.upper() for sensitive in ['KEY', 'SECRET', 'PASSWORD', 'TOKEN']):
print(f" {key}=***MASKED***")
else:
print(f" {key}={value}")
sandbox.kill()
Filter Variables
Filter environment variables by prefix or pattern:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Get all variables
env = sandbox.env.get_all()
# Filter by prefix
api_vars = {k: v for k, v in env.items() if k.startswith('API_')}
print(f"API variables: {list(api_vars.keys())}")
# Filter by pattern
debug_vars = {k: v for k, v in env.items() if 'DEBUG' in k.upper()}
print(f"Debug variables: {list(debug_vars.keys())}")
# Filter custom variables (exclude system vars)
system_vars = {'PATH', 'HOME', 'USER', 'SHELL', 'PWD'}
custom_vars = {k: v for k, v in env.items() if k not in system_vars}
print(f"Custom variables: {list(custom_vars.keys())}")
sandbox.kill()
Check Variable Existence
Check if a variable exists:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Check if variable exists
api_key = sandbox.env.get("API_KEY")
if api_key:
print("API_KEY is set")
else:
print("API_KEY is not set")
# Check multiple variables
required_vars = ["API_KEY", "DATABASE_URL", "NODE_ENV"]
env = sandbox.env.get_all()
missing = [var for var in required_vars if var not in env]
if missing:
print(f"Missing variables: {missing}")
else:
print("All required variables are set")
sandbox.kill()
Verify Variables in Code
Verify environment variables are accessible in code execution:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set some variables
sandbox.env.set("API_KEY", "sk-123")
sandbox.env.set("DEBUG", "true")
# Verify they're accessible in code
result = sandbox.run_code('''
import os
print(f"API_KEY: {os.getenv('API_KEY')}")
print(f"DEBUG: {os.getenv('DEBUG')}")
''')
print(result.stdout)
# Get variables via env API
env = sandbox.env.get_all()
print(f"\nVia API - API_KEY: {env.get('API_KEY')}")
print(f"Via API - DEBUG: {env.get('DEBUG')}")
sandbox.kill()
Complete Example
Here’s a complete example showing environment variable retrieval:
from hopx_ai import Sandbox
sandbox = Sandbox.create(template="code-interpreter")
# Set up environment
sandbox.env.set_all({
"API_KEY": "sk-prod-xyz",
"DATABASE_URL": "postgres://localhost/db",
"NODE_ENV": "production",
"DEBUG": "false"
})
# Get all variables
print("📋 All Environment Variables:")
env = sandbox.env.get_all()
for key, value in sorted(env.items()):
if any(sensitive in key.upper() for sensitive in ['KEY', 'SECRET', 'PASSWORD']):
print(f" {key}=***MASKED***")
else:
print(f" {key}={value}")
# Get specific variables
print("\n🔍 Specific Variables:")
api_key = sandbox.env.get("API_KEY")
print(f" API_KEY: {'Set' if api_key else 'Not set'}")
db_url = sandbox.env.get("DATABASE_URL")
print(f" DATABASE_URL: {db_url}")
node_env = sandbox.env.get("NODE_ENV", "development")
print(f" NODE_ENV: {node_env}")
# Verify in code execution
print("\n✅ Verification in Code:")
result = sandbox.run_code('''
import os
print(f"API_KEY: {os.getenv('API_KEY', 'not set')[:10]}...")
print(f"DATABASE_URL: {os.getenv('DATABASE_URL', 'not set')}")
print(f"NODE_ENV: {os.getenv('NODE_ENV', 'not set')}")
''')
print(result.stdout)
sandbox.kill()
Best Practices
1. Use get() for Single Variables
Use env.get(key) for retrieving individual variables with optional defaults.
2. Use get_all() for Multiple Variables
Use env.get_all() when you need to access multiple variables or iterate through all.
3. Handle Missing Variables
Always check if variables exist or provide defaults when retrieving.
4. Mask Sensitive Values
When displaying environment variables, mask sensitive values (keys, secrets, passwords, tokens).
5. Verify in Code
Test that environment variables are accessible in code execution to ensure they’re set correctly.
Next Steps