nautechsystems/nautilus_trader · error · anyhow::Error

{key_var} is required when {secret_var} is provided

Error message

{key_var} is required when {secret_var} is provided

What it means

Credential pairing check in with_credentials: authenticating requires both an API key and secret. When only one of the environment-derived credential variables (key_var/secret_var, e.g. the API key variable without its secret counterpart) is present, client construction aborts.

Source

Thrown at crates/adapters/bitmex/src/http/client.rs:1044

        // Determine environment from URL to select correct environment variables
        let environment = if base_url.as_ref().is_some_and(|url| url.contains("testnet")) {
            BitmexEnvironment::Testnet
        } else {
            BitmexEnvironment::Mainnet
        };

        let (key_var, secret_var) = credential_env_vars(environment);

        let api_key = get_or_env_var_opt(api_key, key_var);
        let api_secret = get_or_env_var_opt(api_secret, secret_var);

        // If we're trying to create an authenticated client, we need both key and secret
        if api_key.is_some() && api_secret.is_none() {
            anyhow::bail!("{secret_var} is required when {key_var} is provided");
        }

        if api_key.is_none() && api_secret.is_some() {
            anyhow::bail!("{key_var} is required when {secret_var} is provided");
        }

        Self::new(
            base_url,
            api_key,
            api_secret,
            environment,
            timeout_secs,
            max_retries,
            retry_delay_ms,
            retry_delay_max_ms,
            recv_window_ms,
            max_requests_per_second,
            max_requests_per_minute,
            proxy_url,
        )
        .map_err(|e| anyhow::anyhow!("Failed to create HTTP client: {e}"))
    }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Set the key env var named by key_var (e.g. BITMEX_API_KEY)
  2. Pass api_key explicitly alongside api_secret
  3. Check credential plumbing so key and secret are sourced from the same config

Example fix

// before
export BITMEX_API_SECRET=xyz
// after
export BITMEX_API_KEY=abc
export BITMEX_API_SECRET=xyz
Defensive patterns

Strategy: validation

Validate before calling

let has_key = api_key.is_some() || std::env::var("BITMEX_API_KEY").is_ok();
let has_secret = api_secret.is_some() || std::env::var("BITMEX_API_SECRET").is_ok();
if has_secret && !has_key { panic!("BITMEX_API_KEY missing"); }

Try / catch

match client_result {
    Ok(c) => c,
    Err(e) if e.to_string().contains("is required when") => configure_missing_key(),
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling with_credentials with api_key=None (and key_var env var unset) while api_secret is Some or the secret_var env var is set.

Common situations: Setting only the secret env var and forgetting the key; deleting/renaming the key env var during a config change; passing None for api_key while supplying the secret argument.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/4f81e6c72bd38111. Report an issue: GitHub.