Environment variables are the standard way to pass configuration and secrets (database credentials, API keys, feature flags) to your app without hardcoding them.
Setting Environment Variables
Option 1: Create a .env File (Recommended)
The simplest approach — create a .env file in your app directory via the CLI:
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:
instapods pods reload my-app
Option 2: Set at Pod Creation (API)
Pass environment variables when creating a pod via the API:
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:
// 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;
Python
Variables from /etc/environment are inherited by the systemd service automatically.
For .env files, use python-dotenv:
# 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
// 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:
// 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:
# 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:
# 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
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.
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