Skip to content

Environment Variables

This page covers how to set and use environment variables in sessions, and default environment variables inside each session.

Upon creating a session, useful metadata is set as environment variables:

  • ERA_SESSION is set to true to know if code is running inside an ERA session
  • ERA_SESSION_ID contains the session ID
  • ERA_LANGUAGE contains the language runtime (python, node, typescript, go, deno)

You can try it out by running code in a session:

Terminal window
# Create a session
curl -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 variables
curl -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'\'')}\")"
}'

⚠️ 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:

  1. Per-run environment variables (recommended for secrets)
  2. Setup environment variables (for package installation only)

Recommended for secrets and dynamic values.

Set environment variables for a specific code execution. Works with all languages.

Terminal window
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"
}
}'
Terminal window
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"
}
}'
Terminal window
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"
}
}'
Terminal window
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"
}
}'
Terminal window
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

Only for package installation commands.

Set environment variables during session setup (package installation phase):

Terminal window
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 envs are only available during setup commands
  • They are NOT available during runtime
  • Do NOT use for secrets - use per-run env instead

This is critical for security:

Terminal window
# Day 1: Create session and run code with API key
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: 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: secret123

Why 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

Terminal window
# Make API call with secret key
curl -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-..."
}
}'
Terminal window
# Connect to database with credentials
curl -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"
}
}'
Terminal window
# Pass multiple secrets at once
curl -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"
}
}'
Terminal window
# Different env vars per execution
curl -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 values
curl -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"
}
}'

Environment variables work identically across all languages:

LanguageAccess MethodExample
Pythonos.environ.get("VAR")import os; print(os.environ.get("API_KEY"))
Node.jsprocess.env.VARconsole.log(process.env.API_KEY)
TypeScriptprocess.env.VARconst key = process.env.API_KEY
Goos.Getenv("VAR")import "os"; key := os.Getenv("API_KEY")
DenoDeno.env.get("VAR")const key = Deno.env.get("API_KEY")

  1. Pass secrets at runtime

    Terminal window
    {"code": "...", "env": {"API_KEY": "secret"}}
  2. Pass env vars on every execution

    Terminal window
    # Every run that needs secrets must provide them
  3. Use different secrets for dev/prod

    Terminal window
    # Different env values per environment
  1. Don’t use setup envs for secrets

    Terminal window
    # BAD - Don't do this!
    {"setup": {"envs": {"API_KEY": "secret"}}}
  2. Don’t expect env vars to persist

    Terminal window
    # They won't be there on the next run!
  3. Don’t store secrets in files

    Terminal window
    # Files like .env are excluded from persistence anyway

ERA Agent automatically protects your secrets:

  1. No Persistence

    • Runtime env vars exist only for that execution
    • Never saved to R2 storage
  2. Automatic File Exclusion

    • .env, credentials.json, etc. never persisted
    • Even if created during setup
  3. Execution Isolation

    • Each run gets fresh environment
    • No env var leakage between runs
  4. Session Isolation

    • Env vars don’t leak between sessions
    • Each session is completely isolated