nautechsystems/nautilus_trader · error

Missing Betfair credentials in config and environment

Error message

Missing Betfair credentials in config and environment

What it means

The Betfair adapter config's credential resolver looks for username, password, and app key either in the config or in environment variables. If BetfairCredential::resolve returns Ok(None), nothing was found in either place, and the adapter bails with this error during client construction.

Solutions

  1. Set BETFAIR_USERNAME, BETFAIR_PASSWORD, and BETFAIR_APP_KEY in the environment
  2. Provide username, password, and app_key in the BetfairDataClientConfig/BetfairExecClientConfig
  3. Check for env var name typos and that the vars are visible to the process (e.g. inside Docker/K8s)

Example fix

// before
let config = BetfairDataClientConfig { username: None, password: None, app_key: None, .. };
// after
let config = BetfairDataClientConfig {
    username: Some(SecretString::new(user)),
    password: Some(SecretString::new(pass)),
    app_key: Some(SecretString::new(app_key)),
    ..
};
Defensive patterns

Strategy: validation

Validate before calling

fn betfair_env_ready() -> bool {
    ["BETFAIR_USERNAME", "BETFAIR_PASSWORD", "BETFAIR_APP_KEY"]
        .iter().all(|k| std::env::var(k).map(|v| !v.is_empty()).unwrap_or(false))
}
assert!(betfair_env_ready(), "set BETFAIR_USERNAME/PASSWORD/APP_KEY");

Try / catch

match BetfairFactory::create(config) {
    Err(e) if e.to_string().contains("Missing Betfair credentials") => {
        anyhow::bail!("set BETFAIR_USERNAME/BETFAIR_PASSWORD/BETFAIR_APP_KEY or fill config")
    }
    other => other,
}

Prevention

When it happens

Trigger: Constructing a Betfair client/factory with a config where username, password, and app_key are all None and the corresponding environment variables (BETFAIR_USERNAME, BETFAIR_PASSWORD, BETFAIR_APP_KEY) are unset.

Common situations: Deploying to a server/container where the env vars were never set; passing a config struct built without credentials; typo'd env var names; credentials present but only partial (that produces the 'Invalid Betfair credentials' variant instead).

Related errors


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

Appendix: source

Thrown at crates/adapters/betfair/src/config.rs:73

            .map(|_| ())
            .map_err(|e| anyhow::anyhow!("Invalid {label} '{value}': {e}"))?;
    }

    Ok(())
}

fn resolve_credential(
    username: Option<SecretString>,
    password: Option<SecretString>,
    app_key: Option<SecretString>,
) -> anyhow::Result<BetfairCredential> {
    match BetfairCredential::resolve(
        username.map(SecretString::into_inner),
        password.map(SecretString::into_inner),
        app_key.map(SecretString::into_inner),
    ) {
        Ok(Some(credential)) => Ok(credential),
        Ok(None) => anyhow::bail!("Missing Betfair credentials in config and environment"),
        Err(e) => Err(match e {
            CredentialError::MissingPassword => anyhow::anyhow!(
                "Invalid Betfair credentials: username provided but password is missing",
            ),
            CredentialError::MissingUsername => anyhow::anyhow!(
                "Invalid Betfair credentials: password or app key provided but username is missing",
            ),
            CredentialError::MissingAppKey => {
                anyhow::anyhow!("Invalid Betfair credentials: app key is missing")
            }
        }),
    }
}

fn build_stream_config(
    stream_host: &Option<String>,
    stream_port: &Option<u16>,
    stream_heartbeat_secs: Option<u64>,

View on GitHub (pinned to 18893faf8b)