Date: 2026-05-11
Time: 15:47
This is a Phase 3 example script demonstrating FTL2's secrets management system. It shows how to securely load, access, inspect, and handle sensitive values (API keys, database URLs, tokens) within automation scripts using the ftl.secrets interface. Secrets are declared upfront when creating an automation context and sourced from environment variables.
Secrets are declared as a list of environment variable names when entering the automation context:
async with automation(secrets=["API_KEY", "DB_PASSWORD"]) as ftl:
value = ftl.secrets["API_KEY"] # Direct access (raises KeyError if missing)
value = ftl.secrets.get("OPTIONAL", "default") # Access with fallback
if "API_KEY" in ftl.secrets: # Existence check
# use it
Secrets can also be passed when constructing AutomationContext directly (without async with):
context = AutomationContext(secrets=["DIRECT_SECRET", "OTHER"])
context.secrets["DIRECT_SECRET"]
Typical use is passing secrets into module calls — e.g., as HTTP headers or connection strings — rather than writing them to files.
automation(secrets=["KEY1", "KEY2", ...]) — declares which environment variables to load as secrets.ftl.secrets["KEY"] — retrieves a secret value; raises KeyError if the env var wasn't set or wasn't in the declared list.ftl.secrets.get("KEY", default) — retrieves with a fallback value."KEY" in ftl.secrets — checks if a secret was actually loaded (env var was set).ftl.secrets.keys() — lists all *requested* secret names.ftl.secrets.loaded_keys() — lists only the secrets that were actually found in the environment.len(ftl.secrets) — count of loaded secrets.str(ftl.secrets) / repr(ftl.secrets) — safe representation that never exposes actual values.Environment variables must be set before running:
export API_KEY="your-api-key"
export DATABASE_URL="postgres://..."
uv run python example_phase3_secrets.py
str() and repr() on the secrets object redact values, preventing accidental exposure in logs or tracebacks.secrets= parameter are accessible. Accessing an undeclared key raises KeyError..get()).SLACK_WEBHOOK is set).ftl2.automation (async context manager), ftl2.AutomationContext (direct construction).ftl.command() for shell commands, ftl.uri() (referenced in comments) for HTTP calls with secret-bearing headers.