> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hopx.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Environment Variables

> Complete reference for HopX CLI environment variable commands - list, get, set, and delete environment variables in sandboxes. Includes examples for configuring execution environments.

Manage environment variables in HopX sandboxes using the `env` command. List, get, set, and delete environment variables to configure execution environments.

## Command Syntax

```bash theme={null}
hopx env <subcommand> [options]
```

## Subcommands

### `list`

List all environment variables in a sandbox.

**Syntax:**

```bash theme={null}
hopx env list SANDBOX_ID [OPTIONS]
```

**Arguments:**

* `SANDBOX_ID` - Sandbox ID (required)

**Options:**

* `--output, -o FORMAT` - Output format: `table`, `json`, `plain` (default: `table`)

**Examples:**

```bash theme={null}
# List all environment variables
hopx env list sb_abc123

# List with JSON output
hopx env list sb_abc123 --output json
```

**Expected Output (Table):**

```
┌───────────┬─────────────┐
│ Variable  │ Value       │
├───────────┼─────────────┤
│ API_KEY   │ secret123   │
│ DEBUG     │ true        │
│ NODE_ENV  │ production  │
└───────────┴─────────────┘
```

**Exit Codes:**

* `0` - Success
* `3` - Authentication error
* `4` - Sandbox not found

### `get`

Get the value of a specific environment variable.

**Syntax:**

```bash theme={null}
hopx env get SANDBOX_ID VARIABLE [OPTIONS]
```

**Arguments:**

* `SANDBOX_ID` - Sandbox ID (required)
* `VARIABLE` - Environment variable name (required)

**Examples:**

```bash theme={null}
# Get environment variable
hopx env get sb_abc123 API_KEY

# Get with JSON output
hopx env get sb_abc123 DEBUG --output json
```

**Expected Output:**

```
secret123
```

**Exit Codes:**

* `0` - Success
* `3` - Authentication error
* `4` - Sandbox or variable not found

### `set`

Set one or more environment variables.

**Syntax:**

```bash theme={null}
hopx env set SANDBOX_ID [KEY] [VALUE] [OPTIONS]
```

**Arguments:**

* `SANDBOX_ID` - Sandbox ID (required)
* `KEY` - Environment variable name (optional, use with `--env` for multiple)
* `VALUE` - Environment variable value (optional, use with `--env` for multiple)

**Options:**

* `--env, -e TEXT` - Environment variable in KEY=VALUE format (repeatable)
* `--env-file PATH` - Load environment variables from file

**Examples:**

```bash theme={null}
# Set single variable (positional arguments)
hopx env set sb_abc123 API_KEY secret123

# Set multiple variables using --env flag
hopx env set sb_abc123 --env API_KEY=secret123 --env DEBUG=true --env NODE_ENV=production

# Set from file
hopx env set sb_abc123 --env-file .env
```

**Expected Output:**

```
✓ Environment variables set
```

**Exit Codes:**

* `0` - Success
* `3` - Authentication error
* `4` - Sandbox not found

### `delete`

Delete an environment variable.

**Syntax:**

```bash theme={null}
hopx env delete SANDBOX_ID VARIABLE [OPTIONS]
```

**Arguments:**

* `SANDBOX_ID` - Sandbox ID (required)
* `VARIABLE` - Environment variable name to delete (required)

**Examples:**

```bash theme={null}
# Delete environment variable
hopx env delete sb_abc123 DEBUG
```

**Expected Output:**

```
✓ Environment variable deleted: DEBUG
```

**Exit Codes:**

* `0` - Success
* `3` - Authentication error
* `4` - Sandbox or variable not found

## Shell Scripting Examples

### Configure Environment for Code Execution

```bash theme={null}
#!/bin/bash
set -e

SANDBOX_ID="sb_abc123"

# Set environment variables
hopx env set "$SANDBOX_ID" \
  API_KEY=secret123 \
  DEBUG=true \
  NODE_ENV=production

# Execute code that uses environment variables
hopx run "import os; print(os.environ['API_KEY'])" --sandbox "$SANDBOX_ID"
```

### Load Environment from File

```bash theme={null}
#!/bin/bash

# Create .env file
cat > .env << EOF
API_KEY=secret123
DEBUG=true
NODE_ENV=production
EOF

# Load into sandbox
hopx env set sb_abc123 --env-file .env
```

### Conditional Environment Setup

```bash theme={null}
#!/bin/bash

# Check if variable exists
if hopx env get sb_abc123 API_KEY > /dev/null 2>&1; then
    echo "API_KEY already set"
else
    echo "Setting API_KEY..."
    hopx env set sb_abc123 API_KEY=secret123
fi
```

### List and Export Variables

```bash theme={null}
#!/bin/bash

# List variables and export to local environment
hopx env list sb_abc123 --output json | jq -r 'to_entries[] | "export \(.key)=\(.value)"' | source /dev/stdin
```

## Related

* **[CLI Quickstart](/cli/quickstart)** - Get started with CLI
* **[Sandbox Commands](/cli/commands/sandbox)** - Manage sandboxes
* **[Code Execution](/cli/commands/run)** - Execute code with environment variables
* **[Shell Commands](/cli/commands/cmd)** - Run commands with environment variables
* **[Python SDK: Environment Variables](/sdk/python/environment-variables)** - Python SDK environment management
* **[JavaScript SDK: Environment Variables](/sdk/javascript/environment-variables)** - JavaScript SDK environment management
* **[API: Environment Variables](/api/vm-agent/get-env)** - REST API endpoints

## Next Steps

* Learn about [Code Execution](/cli/commands/run) to use environment variables in code
* Explore [Shell Commands](/cli/commands/cmd) to run commands with environment variables
* Review [Sandbox Management](/cli/commands/sandbox) to create sandboxes with initial environment variables
