nautechsystems/nautilus_trader · error

Invalid Betfair credentials: password or app key provided…

Error message

Invalid Betfair credentials: password or app key provided but username is missing

What it means

Betfair credential resolution requires the credential set to be complete and coherent. When a password or app key is supplied but the username is absent, CredentialError::MissingUsername is mapped to this error. It prevents silent misconfiguration where half a credential is provided.

Solutions

  1. Set username in config (betfair.username) or export BETFAIR_USERNAME
  2. Check for env var name typos (BETFAIR_USERNAME, not BETFAIR_USER)
  3. If the credential block is not intended, remove the orphaned password/app_key entries

Example fix

// before (toml)
[betfair]
password = "secret"
app_key = "key"
// after
[betfair]
username = "myuser"
password = "secret"
app_key = "key"
Defensive patterns

Strategy: validation

Validate before calling

if (password.is_some() || app_key.is_some()) && username.is_none() {
    return Err(anyhow!("Betfair username missing"));
}

Type guard

fn has_username(u: Option<&str>, p: Option<&str>, k: Option<&str>) -> bool {
    u.is_some() || (p.is_none() && k.is_none())
}

Try / catch

let cred = resolve_credential(cfg)
    .map_err(|e| anyhow::anyhow!("Betfair credential config invalid: {e:#}"))?;

Prevention

When it happens

Trigger: resolve_credential (via credential()) sees a password (config or BETFAIR_PASSWORD) or app key (config or BETFAIR_APP_KEY) while username is missing from both config and BETFAIR_USERNAME.

Common situations: Exporting BETFAIR_PASSWORD/BETFAIR_APP_KEY but forgetting BETFAIR_USERNAME; typos like BETFAIR_USER; config file with password/app_key but no username key.

Related errors


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

Appendix: source

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

    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>,
    stream_heartbeat_timeout_secs: Option<u64>,
    stream_reconnect_delay_initial_ms: u64,
    stream_reconnect_delay_max_ms: u64,
    stream_use_tls: bool,

View on GitHub (pinned to 18893faf8b)