Environment Variables
Learn how to use environment variables to securely store API keys, database credentials, and configuration values that your agents can access at runtime.
What are Environment Variables?
Securely store secrets and configuration outside your code
Environment variables allow you to store sensitive data like API keys, database credentials, and configuration values separately from your code. They are secure (never in git),environment-specific (different values per environment), andeasy to update without code changes.
When you deploy your agents, Connic automatically injects all environment variables configured for that environment. Access them using standard Python:
import os
api_key = os.environ.get("MY_API_KEY")
log_level = os.environ.get("LOG_LEVEL", "INFO")Variables are only applied on deployment. You must redeploy for changes to take effect.
Using Variables in Tools
# tools/api_client.py
import os
import httpx
async def call_external_api(query: str) -> dict:
"""Call an external API using credentials from environment variables."""
api_key = os.environ.get("EXTERNAL_API_KEY")
api_url = os.environ.get("EXTERNAL_API_URL", "https://api.example.com")Using Variables in Middleware
# middleware/auth.py
import os
from connic import StopProcessing
async def before(payload: dict, context: dict) -> dict:
"""Validate requests using an internal auth service."""
auth_url = os.environ.get("AUTH_SERVICE_URL")
auth_secret = os.environ.get("AUTH_SERVICE_SECRET")
if not auth_url or not auth_secret:
raise StopProcessing("Auth service not configured")
# ... validation logic using auth_url and auth_secret
return payloadEnvironment-Specific Values
# Your code stays the same - values differ per environment
import os
database_url = os.environ.get("DATABASE_URL") # Different per environment
api_key = os.environ.get("API_KEY") # sk_test_xxx in staging, sk_live_xxx in prod
log_level = os.environ.get("LOG_LEVEL", "INFO") # DEBUG in staging, WARNING in prodConfigure different values per environment in the dashboard. Your code stays the same.
Mark variables as sensitive to hide them in the dashboard (shown as ••••••••) and mask them in logs. Always mark these as sensitive: API keys, passwords, database credentials, and tokens.
- Use uppercase with underscores:
DATABASE_URLnotdatabaseUrl - Provide defaults for optional config:
os.environ.get("LOG_LEVEL", "INFO") - Validate required variables early: Check at startup, not when first used
- Use different values per environment: Test keys for staging, live keys for production
Redeploy required: Changes only take effect after redeployment.Don't commit secrets: Never put API keys in your repository.
Managing Variables in the Dashboard
Go to your project → Settings → Variables. Select the environment, add variables with the form, and redeploy to apply changes.