Environment Variables
This page covers how to set and use environment variables in sessions, and default environment variables inside each session.
Default Environment Variables
Section titled “Default Environment Variables”Knowing if you are inside a session
Section titled “Knowing if you are inside a session”Upon creating a session, useful metadata is set as environment variables:
ERA_SESSIONis set totrueto know if code is running inside an ERA sessionERA_SESSION_IDcontains the session IDERA_LANGUAGEcontains the language runtime (python, node, typescript, go, deno)
You can try it out by running code in a session:
# Create a sessioncurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions \ -H "Content-Type: application/json" \ -d '{ "language": "python", "session_id": "my-session", "persistent": true }'
# Check environment variablescurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os; print(f\"Session ID: {os.environ.get('\''ERA_SESSION_ID'\'')}\"); print(f\"Language: {os.environ.get('\''ERA_LANGUAGE'\'')}\")" }'Setting Environment Variables
Section titled “Setting Environment Variables”⚠️ Important: Environment variables are NEVER persisted.
You must pass them every time you run code. This is for security - no secrets are stored anywhere.
There are 2 ways to set environment variables:
- Per-run environment variables (recommended for secrets)
- Setup environment variables (for package installation only)
1. Per-Run Environment Variables
Section titled “1. Per-Run Environment Variables”Recommended for secrets and dynamic values.
Set environment variables for a specific code execution. Works with all languages.
Python
Section titled “Python”curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os\\napi_key = os.environ.get(\"API_KEY\")\\nprint(f\"API Key: {api_key}\")", "env": { "API_KEY": "secret123", "DATABASE_URL": "postgresql://localhost:5432/mydb" } }'Node.js
Section titled “Node.js”curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "const apiKey = process.env.API_KEY;\\nconsole.log(\"API Key:\", apiKey);", "env": { "API_KEY": "secret123" } }'TypeScript
Section titled “TypeScript”curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "const apiKey: string = process.env.API_KEY || \"default\";\\nconsole.log(\"API Key:\", apiKey);", "env": { "API_KEY": "secret123" } }'curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "package main\\nimport (\\n\\t\"fmt\"\\n\\t\"os\"\\n)\\nfunc main() {\\n\\tapiKey := os.Getenv(\"API_KEY\")\\n\\tfmt.Println(\"API Key:\", apiKey)\\n}", "env": { "API_KEY": "secret123" } }'curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "const apiKey = Deno.env.get(\"API_KEY\");\\nconsole.log(\"API Key:\", apiKey);", "env": { "API_KEY": "secret123" } }'Important Notes:
- These environment variables are scoped to this execution only
- They are NOT persisted - you must pass them again on the next run
- Perfect for secrets like API keys, database passwords, tokens
- If you come back to the session later, you must provide them again
2. Setup Environment Variables
Section titled “2. Setup Environment Variables”Only for package installation commands.
Set environment variables during session setup (package installation phase):
curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions \ -H "Content-Type: application/json" \ -d '{ "language": "python", "persistent": true, "setup": { "pip": ["requests"], "commands": [ "mkdir -p /tmp/config" ], "envs": { "SETUP_ENV": "production", "BUILD_TARGET": "linux" } } }'⚠️ Warning:
- Setup
envsare only available during setup commands - They are NOT available during runtime
- Do NOT use for secrets - use per-run
envinstead
Environment Variables Are NOT Persisted
Section titled “Environment Variables Are NOT Persisted”This is critical for security:
# Day 1: Create session and run code with API keycurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/prod-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os; print(os.environ.get(\"API_KEY\"))", "env": {"API_KEY": "secret123"} }'# Output: secret123
# Days later: Come back to same session - API_KEY is gone!curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/prod-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os; print(os.environ.get(\"API_KEY\"))" }'# Output: None
# You must pass it again:curl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/prod-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os; print(os.environ.get(\"API_KEY\"))", "env": {"API_KEY": "secret123"} }'# Output: secret123Why this matters:
- ✅ Secrets never stored in R2
- ✅ No risk of accidental secret exposure
- ✅ You control secrets on every execution
- ✅ Different secrets per run if needed
Common Patterns
Section titled “Common Patterns”1. API Keys for External Services
Section titled “1. API Keys for External Services”# Make API call with secret keycurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import requests\\nimport os\\napi_key = os.environ.get(\"OPENAI_API_KEY\")\\nresponse = requests.get(\"https://api.openai.com/v1/models\", headers={\"Authorization\": f\"Bearer {api_key}\"})\\nprint(response.json())", "env": { "OPENAI_API_KEY": "sk-..." } }'2. Database Credentials
Section titled “2. Database Credentials”# Connect to database with credentialscurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os\\ndb_url = os.environ.get(\"DATABASE_URL\")\\nprint(f\"Connecting to: {db_url}\")", "env": { "DATABASE_URL": "postgresql://user:pass@host:5432/db" } }'3. Multiple Environment Variables
Section titled “3. Multiple Environment Variables”# Pass multiple secrets at oncecurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os\\nfor key in [\"API_KEY\", \"DB_PASSWORD\", \"JWT_SECRET\"]:\\n print(f\"{key}: {os.environ.get(key)}\")", "env": { "API_KEY": "secret123", "DB_PASSWORD": "dbpass456", "JWT_SECRET": "jwt789" } }'4. Dynamic Configuration
Section titled “4. Dynamic Configuration”# Different env vars per executioncurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os\\nenv = os.environ.get(\"ENVIRONMENT\")\\nprint(f\"Running in: {env}\")", "env": { "ENVIRONMENT": "production", "LOG_LEVEL": "info" } }'
# Later: Use different valuescurl -X POST https://era-agent.yawnxyz.workers.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "import os\\nenv = os.environ.get(\"ENVIRONMENT\")\\nprint(f\"Running in: {env}\")", "env": { "ENVIRONMENT": "development", "LOG_LEVEL": "debug" } }'All Languages Support
Section titled “All Languages Support”Environment variables work identically across all languages:
| Language | Access Method | Example |
|---|---|---|
| Python | os.environ.get("VAR") | import os; print(os.environ.get("API_KEY")) |
| Node.js | process.env.VAR | console.log(process.env.API_KEY) |
| TypeScript | process.env.VAR | const key = process.env.API_KEY |
| Go | os.Getenv("VAR") | import "os"; key := os.Getenv("API_KEY") |
| Deno | Deno.env.get("VAR") | const key = Deno.env.get("API_KEY") |
Best Practices
Section titled “Best Practices”-
Pass secrets at runtime
Terminal window {"code": "...", "env": {"API_KEY": "secret"}} -
Pass env vars on every execution
Terminal window # Every run that needs secrets must provide them -
Use different secrets for dev/prod
Terminal window # Different env values per environment
❌ DON’T
Section titled “❌ DON’T”-
Don’t use setup
envsfor secretsTerminal window # BAD - Don't do this!{"setup": {"envs": {"API_KEY": "secret"}}} -
Don’t expect env vars to persist
Terminal window # They won't be there on the next run! -
Don’t store secrets in files
Terminal window # Files like .env are excluded from persistence anyway
Security Features
Section titled “Security Features”ERA Agent automatically protects your secrets:
-
No Persistence
- Runtime
envvars exist only for that execution - Never saved to R2 storage
- Runtime
-
Automatic File Exclusion
.env,credentials.json, etc. never persisted- Even if created during setup
-
Execution Isolation
- Each run gets fresh environment
- No env var leakage between runs
-
Session Isolation
- Env vars don’t leak between sessions
- Each session is completely isolated