nautechsystems/nautilus_trader · critical · anyhow::Error

{private_key_env} not found in config or environment

Error message

{private_key_env} not found in config or environment

What it means

The dYdX execution client needs the wallet's Ethereum private key to sign orders. resolve_private_key looks for the value in the client config first, then in the environment variable named by private_key_env; if neither yields a non-empty string it bails naming the missing env var.

Source

Thrown at crates/adapters/dydx/src/execution/mod.rs:286

    }

    fn resolve_private_key(config: &DydxAdapterConfig) -> anyhow::Result<String> {
        let (private_key_env, _) = credential_env_vars(config.network);

        if let Some(ref pk) = config.private_key
            && !pk.expose_secret().trim().is_empty()
        {
            return Ok(pk.expose_secret().to_owned());
        }

        if let Some(pk) = std::env::var(private_key_env)
            .ok()
            .filter(|s| !s.trim().is_empty())
        {
            return Ok(pk);
        }

        anyhow::bail!("{private_key_env} not found in config or environment")
    }

    fn register_order_context(&self, client_id_u32: u32, context: OrderContext) {
        self.order_contexts.insert(client_id_u32, context);
    }

    fn get_order_context(&self, client_id_u32: u32) -> Option<OrderContext> {
        self.order_contexts
            .get(&client_id_u32)
            .map(|r| r.value().clone())
    }

    fn get_chain_id(&self) -> ChainId {
        self.config.get_chain_id()
    }

    fn spawn_ws_stream_handler(
        &self,

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Set the expected environment variable (e.g. export DYDX_PRIVATE_KEY=0x...) before starting the process
  2. Pass the private key via the client config's private_key field
  3. Verify the env var name in your config matches what is actually exported; check .env loading
  4. Use a secrets manager/injection so the key is present in CI or containers

Example fix

// before (shell)
./start-strategy  # DYDX_PRIVATE_KEY unset
// after (shell)
export DYDX_PRIVATE_KEY=0xabc123... && ./start-strategy
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_private_key_available(env_var: &str, config_key: Option<&str>) -> Result<(), String> {
    if config_key.map_or(false, |k| !k.trim().is_empty()) {
        return Ok(());
    }
    match std::env::var(env_var) {
        Ok(v) if !v.trim().is_empty() => Ok(()),
        _ => Err(format!("{env_var} not found in config or environment")),
    }
}

Prevention

When it happens

Trigger: Creating/connecting the dYdX execution client when the private key is absent from the config and the referenced environment variable is unset or contains only whitespace.

Common situations: Running in an environment where the .env file or secret injection was not loaded; misnaming the env var (e.g. DYDX_PRIVATE_KEY vs DYDX_ETHEREUM_PRIVATE_KEY); empty-string key in config after templating.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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