Environment variable interpolation
Substitute environment variables into YAML route files with ${env:VAR}
tokens. The tokens expand before YAML parsing, so they work in endpoint
URIs, log messages, header values, and any other string field.
Syntax
# =============================================================================
# env-interpolation example routes
#
# Uses ${env:VAR_NAME} to inject runtime environment variables.
# Supports default values with ${env:VAR_NAME:-default} syntax.
# Substitution happens before YAML parsing — works in URIs, messages, values.
#
# Try setting these before running:
# GREET_TARGET=log:my-app
# POLL_PERIOD_MS=2000
# LOG_PREFIX="[demo]"
#
# If unset, defaults kick in automatically (see route 2).
# =============================================================================
routes:
# ---------------------------------------------------------------------------
# Route 1: env var in log message and endpoint URI
# ---------------------------------------------------------------------------
- id: "env-greet"
from: "timer:env-tick?period=${env:POLL_PERIOD_MS}&repeatCount=3"
steps:
- log: "${env:LOG_PREFIX} Firing env-interpolation route"
- set_header:
key: "app"
value: "${env:APP_NAME}"
- to: "${env:GREET_TARGET}"
# ---------------------------------------------------------------------------
# Route 2: env var with default fallback syntax
# ---------------------------------------------------------------------------
- id: "env-defaults"
from: "timer:env-defaults?period=${env:DEFAULT_POLL_MS:-3000}&repeatCount=2"
steps:
- log: "${env:DEFAULT_LOG_PREFIX:-[defaults]} Using fallback defaults"
- to: "${env:DEFAULT_TARGET:-log:info}"
${env:VAR} reads the variable VAR. ${env:VAR:-default} uses
default when VAR is unset. An unset variable with no default fails
route discovery. The error names the variable. Set a default or export
the variable to avoid the failure.
How it works
The DSL loader (camel_dsl::interpolate_env) scans route source for
${env:...} patterns and replaces them before YAML parsing. Substituted
values pass through sanitize_env_value, which strips control characters
and newlines. This blocks newline injection from a hostile or malformed
variable.
The PropertiesResolver type in camel-config exposes the same
resolution as a public API for config-value placeholders.
Setup
let config =
CamelConfig::from_file("Camel.toml").map_err(|e| CamelError::Config(e.to_string()))?;
let mut ctx = CamelContext::builder().build().await.unwrap(); // allow-unwrap
ctx.register_component(TimerComponent::new());
ctx.register_component(LogComponent::new());
let routes = discover_routes(&config.routes).map_err(|e| CamelError::Config(e.to_string()))?;
The Camel.toml for this example is minimal. Route discovery and
component registration follow the standard pattern.
[default]
routes = ["routes/**/*.yaml"]
log_level = "INFO"
When to use
- Twelve-factor apps: inject configuration that varies per deploy through the environment, not through files in source control.
- Secrets: pass credentials and tokens from the environment. The route file never stores the secret value.
- Per-environment endpoints: point routes at different brokers, HTTP hosts, or databases without editing route files.
Reference: PropertiesResolver in the Config crate