nautechsystems/nautilus_trader · error
Missing environment variable: {key_env}
Error message
Missing environment variable: {key_env} What it means
with_credentials resolves the API key from the provided value or from the environment variable named by credential_env_vars(environment). When neither the explicit api_key argument nor the environment variable (e.g. DERIBIT_TESTNET_API_KEY or DERIBIT_API_KEY) is set, the builder cannot create an authenticated WebSocket client and fails fast.
Source
Thrown at crates/adapters/deribit/src/websocket/client.rs:310
/// to the environment variable for the given `environment`:
/// - Testnet: `DERIBIT_TESTNET_API_KEY` and `DERIBIT_TESTNET_API_SECRET`
/// - Mainnet: `DERIBIT_API_KEY` and `DERIBIT_API_SECRET`
///
/// # Errors
///
/// Returns an error if neither the argument nor the environment variable
/// provides a credential.
pub fn with_credentials(
environment: DeribitEnvironment,
api_key: Option<String>,
api_secret: Option<String>,
auth_timeout_secs: Option<u64>,
proxy_url: Option<String>,
) -> anyhow::Result<Self> {
let (key_env, secret_env) = credential_env_vars(environment);
let api_key = get_or_env_var_opt(api_key, key_env)
.ok_or_else(|| anyhow::anyhow!("Missing environment variable: {key_env}"))?;
let api_secret = get_or_env_var_opt(api_secret, secret_env)
.ok_or_else(|| anyhow::anyhow!("Missing environment variable: {secret_env}"))?;
Self::new(
None,
Some(api_key),
Some(api_secret),
DERIBIT_WS_HEARTBEAT_SECS,
auth_timeout_secs,
environment,
TransportBackend::default(),
proxy_url,
)
}
/// Returns the current connection mode.
fn connection_mode(&self) -> ConnectionMode {
let mode_u8 = self.connection_mode.load().load(Ordering::Relaxed);View on GitHub (pinned to 18893faf8b)
Solutions
- Export the required environment variable (e.g. `export DERIBIT_API_KEY=...` for mainnet or the testnet equivalent) before starting the process.
- Pass the api_key explicitly as the first argument to with_credentials.
- Check which environment is configured so you set the matching variable (testnet vs mainnet naming).
Example fix
// before
let client = DeribitWebSocketClient::with_credentials(None, None, None, environment, ...).await?;
// after
let key = std::env::var("DERIBIT_API_KEY")?; // or pass Some(key)
let client = DeribitWebSocketClient::with_credentials(Some(key), None, None, environment, ...).await?; Defensive patterns
Strategy: validation
Validate before calling
// Rust: fail fast before constructing the client
let key = std::env::var("DERIBIT_API_KEY")
.map_err(|_| anyhow!("set DERIBIT_API_KEY (or pass api_key) before connecting"))?; Type guard
fn credential_env_present(environment: Environment) -> bool {
let (key_env, _) = credential_env_vars(environment);
std::env::var(key_env).is_ok()
} Try / catch
let client = match DeribitWebSocketClient::with_credentials(None, None, None, env, ...).await {
Ok(c) => c,
Err(e) if e.to_string().contains("Missing environment variable") => {
return Err(anyhow!("configure {}", e))
}
Err(e) => return Err(e),
}; Prevention
- Load .env secrets at process startup, before client construction
- Inject secrets in CI/containers rather than relying on shell env
- Match the environment (testnet vs mainnet) to the correct variable names
- Validate required env vars with a startup check
When it happens
Trigger: Calling DeribitWebSocketClient::with_credentials without passing api_key and without exporting the environment-specific key variable before process start.
Common situations: Forgetting to source a .env file, running in a container/CI where the secret was not injected, using the wrong environment variant (testnet var set but production client requested), or a typo in the variable name.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- Missing environment variable: {secret_env}
- Redis config error: username supplied without password. Eith
- {secret_var} is required when {key_var} is provided
- {key_var} is required when {secret_var} is provided
- Both `api_key` and `api_secret` must be provided together
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/0e143cb6abeb1c68.
Report an issue: GitHub.