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

# Environment Variables

> Configure your app with environment variables and secrets.

Environment variables are the standard way to pass configuration and secrets (database credentials, API keys, feature flags) to your app without hardcoding them.

<Note>
  Running a [1-Click App](/guides/one-click-apps)? Use `instapods env set` - it writes to the app's own config file and restarts the app automatically. The `.env`-in-app-directory approach below is for custom-code pods (Static, PHP, Node.js, Python).
</Note>

## Setting Environment Variables

### Option 1: The `instapods env` command (Recommended)

Set one or more variables straight from the CLI:

```bash theme={null}
instapods env set my-app DATABASE_URL=postgres://instapod:secret@localhost:5432/instapod API_KEY=sk-your-api-key-here
```

List or remove them later (values are masked unless you pass `--show-values`):

```bash theme={null}
instapods env list my-app
instapods env list my-app --show-values
instapods env unset my-app API_KEY
```

For custom-code pods this writes to your app's `.env` file - run `instapods pods reload` afterward to restart. For [1-Click Apps](/guides/one-click-apps) it targets the app's own config file and restarts the app for you.

### Option 2: Create a `.env` File

The manual approach — create a `.env` file in your app directory via the CLI:

```bash theme={null}
instapods exec my-app -- "cat > /home/instapod/app/.env << 'EOF'
DATABASE_URL=postgres://instapod:secret@localhost:5432/instapod
API_KEY=sk-your-api-key-here
NODE_ENV=production
EOF"
```

Then reload to pick up changes:

```bash theme={null}
instapods pods reload my-app
```

### Option 3: Set at Pod Creation (API)

Pass environment variables when creating a pod via the API:

```bash theme={null}
curl -X POST https://app.instapods.com/api/pods \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-app",
    "preset": "nodejs",
    "customizations": {
      "env_vars": {
        "NODE_ENV": "production",
        "LOG_LEVEL": "info"
      }
    }
  }'
```

Variables set this way are written to `/etc/environment` and automatically available to all processes.

## How Each Preset Reads Environment Variables

### Node.js

The systemd service sets `NODE_ENV=production` by default. Variables from `/etc/environment` are inherited automatically.

For `.env` files, use the `dotenv` package:

```javascript theme={null}
// At the top of your entry point (index.js)
require('dotenv').config();

// Access variables
const dbUrl = process.env.DATABASE_URL;
const apiKey = process.env.API_KEY;
```

```bash theme={null}
npm install dotenv
```

### Python

Variables from `/etc/environment` are inherited by the systemd service automatically.

For `.env` files, use `python-dotenv`:

```python theme={null}
# app.py
from dotenv import load_dotenv
import os

load_dotenv()  # loads .env from current directory

db_url = os.environ.get('DATABASE_URL')
api_key = os.environ.get('API_KEY')
```

```
# requirements.txt
python-dotenv>=1.0
flask>=3.0
```

### PHP

PHP-FPM provides access to system environment variables via `getenv()`. For `.env` files, parse them manually or use a library:

```php theme={null}
<?php
// Simple .env loading (no library needed)
$envFile = __DIR__ . '/../.env';
if (file_exists($envFile)) {
    foreach (file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
        if ($line[0] === '#') continue;
        if (strpos($line, '=') !== false) putenv(trim($line));
    }
}

$dbPass = getenv('DB_PASS') ?: '';
$apiKey = getenv('API_KEY') ?: '';
```

For Laravel, the framework handles `.env` files automatically — just create the file and it works.

### Static

Static sites don't run server-side code. For JavaScript apps that need configuration, use a `config.js` file:

```javascript theme={null}
// config.js (served as a static file)
window.APP_CONFIG = {
  API_URL: 'https://api.example.com',
  ENV: 'production'
};
```

## Common Pattern: Database Credentials

After installing a database service, get the credentials and set them as env vars:

```bash theme={null}
# Install MySQL
instapods services add my-app -s mysql -w

# Get the auto-generated password
instapods services creds my-app -s mysql

# Create .env with the credentials
instapods exec my-app -- "cat > /home/instapod/app/.env << 'EOF'
DB_HOST=localhost
DB_PORT=3306
DB_NAME=instapod
DB_USER=instapod
DB_PASS=<password from creds command>
EOF"

# Reload to apply
instapods pods reload my-app
```

## Updating Variables

To update environment variables after pod creation:

```bash theme={null}
# Overwrite the .env file with new values
instapods exec my-app -- "cat > /home/instapod/app/.env << 'EOF'
DATABASE_URL=postgres://new-credentials@localhost/db
API_KEY=new-key
DEBUG=false
EOF"

# Restart the app to pick up changes
instapods pods reload my-app
```

<Warning>
  The `.env` file is stored inside your pod. If you delete the pod, the file is lost. Keep a copy of your environment variables in a secure location.
</Warning>

## Best Practices

* **Never commit secrets** to git — always use `.env` files or environment variables
* **Use defaults in code** — Always provide fallback values (`process.env.PORT || 3000`)
* **Reload after changes** — Run `instapods pods reload` after updating `.env` to restart the app
* **Separate configs** — Use different `.env` files for development vs production
