{"record":{"id":"0e143cb6abeb1c68","repo":"nautechsystems/nautilus_trader","slug":"missing-environment-variable-key-env","errorCode":null,"errorMessage":"Missing environment variable: {key_env}","messagePattern":"Missing environment variable: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/deribit/src/websocket/client.rs","lineNumber":310,"sourceCode":"    /// to the environment variable for the given `environment`:\n    /// - Testnet: `DERIBIT_TESTNET_API_KEY` and `DERIBIT_TESTNET_API_SECRET`\n    /// - Mainnet: `DERIBIT_API_KEY` and `DERIBIT_API_SECRET`\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if neither the argument nor the environment variable\n    /// provides a credential.\n    pub fn with_credentials(\n        environment: DeribitEnvironment,\n        api_key: Option<String>,\n        api_secret: Option<String>,\n        auth_timeout_secs: Option<u64>,\n        proxy_url: Option<String>,\n    ) -> anyhow::Result<Self> {\n        let (key_env, secret_env) = credential_env_vars(environment);\n\n        let api_key = get_or_env_var_opt(api_key, key_env)\n            .ok_or_else(|| anyhow::anyhow!(\"Missing environment variable: {key_env}\"))?;\n        let api_secret = get_or_env_var_opt(api_secret, secret_env)\n            .ok_or_else(|| anyhow::anyhow!(\"Missing environment variable: {secret_env}\"))?;\n\n        Self::new(\n            None,\n            Some(api_key),\n            Some(api_secret),\n            DERIBIT_WS_HEARTBEAT_SECS,\n            auth_timeout_secs,\n            environment,\n            TransportBackend::default(),\n            proxy_url,\n        )\n    }\n\n    /// Returns the current connection mode.\n    fn connection_mode(&self) -> ConnectionMode {\n        let mode_u8 = self.connection_mode.load().load(Ordering::Relaxed);","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/deribit/src/websocket/client.rs#L292-L328","documentation":"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.","triggerScenarios":"Calling DeribitWebSocketClient::with_credentials without passing api_key and without exporting the environment-specific key variable before process start.","commonSituations":"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.","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)."],"exampleFix":"// before\nlet client = DeribitWebSocketClient::with_credentials(None, None, None, environment, ...).await?;\n// after\nlet key = std::env::var(\"DERIBIT_API_KEY\")?; // or pass Some(key)\nlet client = DeribitWebSocketClient::with_credentials(Some(key), None, None, environment, ...).await?;","handlingStrategy":"validation","validationCode":"// Rust: fail fast before constructing the client\nlet key = std::env::var(\"DERIBIT_API_KEY\")\n    .map_err(|_| anyhow!(\"set DERIBIT_API_KEY (or pass api_key) before connecting\"))?;","typeGuard":"fn credential_env_present(environment: Environment) -> bool {\n    let (key_env, _) = credential_env_vars(environment);\n    std::env::var(key_env).is_ok()\n}","tryCatchPattern":"let client = match DeribitWebSocketClient::with_credentials(None, None, None, env, ...).await {\n    Ok(c) => c,\n    Err(e) if e.to_string().contains(\"Missing environment variable\") => {\n        return Err(anyhow!(\"configure {}\", e))\n    }\n    Err(e) => return Err(e),\n};","preventionTips":["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"],"tags":["configuration","environment","credentials","rust"],"backgroundTag":"missing-env-var","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}