nautechsystems/nautilus_trader · error · anyhow::Error

{standard_key_var} not found in config or environment

Error message

{standard_key_var} not found in config or environment

What it means

resolve_credentials could not find a Binance API key in either the client config or the standard environment variable named in the message (live: BINANCE_API_KEY; spot/margin/options testnet: BINANCE_TESTNET_API_KEY; futures testnet: BINANCE_FUTURES_TESTNET_API_KEY; demo: BINANCE_DEMO_API_KEY). The adapter refuses to create a client without credentials.

Source

Thrown at crates/adapters/binance/src/common/credential.rs:100

            BinanceEnvironment::Live => (
                "BINANCE_ED25519_API_KEY",
                "BINANCE_ED25519_API_SECRET",
                "BINANCE_API_KEY",
                "BINANCE_API_SECRET",
            ),
        };

    // Futures: soft deprecation (warn + fallback),
    // Spot/Margin: hard error on removed env vars.
    let is_futures = matches!(
        product_type,
        BinanceProductType::UsdM | BinanceProductType::CoinM
    );

    let api_key = config_api_key
        .or_else(|| std::env::var(standard_key_var).ok())
        .or_else(|| resolve_deprecated_var(deprecated_key_var, standard_key_var, is_futures))
        .ok_or_else(|| anyhow::anyhow!("{standard_key_var} not found in config or environment"))?;

    let api_secret = config_api_secret
        .or_else(|| std::env::var(standard_secret_var).ok())
        .or_else(|| resolve_deprecated_var(deprecated_secret_var, standard_secret_var, is_futures))
        .ok_or_else(|| {
            anyhow::anyhow!("{standard_secret_var} not found in config or environment")
        })?;

    Ok((api_key, api_secret))
}

fn resolve_deprecated_var(
    deprecated_var: &str,
    standard_var: &str,
    allow_fallback: bool,
) -> Option<String> {
    if deprecated_var.is_empty() {
        return None;

View on GitHub (pinned to a4b06ed870)

Solutions

  1. Export the exact variable named in the message: export BINANCE_API_KEY=... (and the matching _API_SECRET)
  2. Verify the variable name matches environment+product type: live=BINANCE_API_KEY, spot testnet=BINANCE_TESTNET_API_KEY, futures testnet=BINANCE_FUTURES_TESTNET_API_KEY, demo=BINANCE_DEMO_API_KEY
  3. Alternatively pass api_key/api_secret directly in the BinanceDataClientConfig / execution client config
  4. Migrate any deprecated BINANCE*_ED25519_API_KEY variables to the standard names

Example fix

# before
export BINANCE_FUTURES_TESTNET_API_KEY=...   # spot testnet client still fails

# after
export BINANCE_TESTNET_API_KEY=...
export BINANCE_TESTNET_API_SECRET=...
Defensive patterns

Strategy: validation

Validate before calling

fn required_env(name: &str) -> anyhow::Result<String> {
    std::env::var(name).map_err(|_| anyhow::anyhow!("{name} not set - export it or pass credentials in config"))
}
let _ = required_env("BINANCE_API_KEY")?;      // adjust per environment/product
let _ = required_env("BINANCE_API_SECRET")?;

Type guard

fn binance_key_var(env: BinanceEnvironment, pt: BinanceProductType) -> &'static str {
    match (env, pt) {
        (BinanceEnvironment::Live, _) => "BINANCE_API_KEY",
        (BinanceEnvironment::Demo, _) => "BINANCE_DEMO_API_KEY",
        (BinanceEnvironment::Testnet, BinanceProductType::UsdM | BinanceProductType::CoinM) => "BINANCE_FUTURES_TESTNET_API_KEY",
        (BinanceEnvironment::Testnet, _) => "BINANCE_TESTNET_API_KEY",
    }
}

Try / catch

if let Err(e) = resolve_credentials(None, None, environment, product_type) {
    eprintln!("Binance credentials missing: {e}. Check env vars for {environment}/{product_type}.");
    std::process::exit(1);
}

Prevention

When it happens

Trigger: Building a Binance data/execution client with no api_key in the config while the exact env var for the configured environment+product_type combination is unset - e.g. only BINANCE_FUTURES_TESTNET_API_KEY is exported but the client is Spot testnet, or a live client with no BINANCE_API_KEY.

Common situations: Forgot to export the variable or it lives only in an unloaded .env; CI/deploy environment not passing secrets; copied config between live/testnet without changing env var names; still relying on removed *_ED25519_* variables, which spot/margin reject and futures only fall back from with a warning.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@a4b06ed870 (2026-08-16). Data as JSON: /api/errors/6943ddd636e3fa32. Report an issue: GitHub.