nikivdev/code · error

missing CLOUDFLARE_API_TOKEN; set it in shell env or Flow pe

Error message

missing CLOUDFLARE_API_TOKEN; set it in shell env or Flow personal env store

What it means

cloudflare_credentials loads CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN from the shell env or the Flow personal env store. If the account ID is set but the API token is not, the pair is incomplete, so it bails instead of silently sending partial credentials. The library intentionally requires both or neither.

Source

Thrown at src/url_inspect.rs:906

        cache_hit: None,
    })
}

fn timeout_from_secs(seconds: f64) -> Result<Duration> {
    if !seconds.is_finite() || seconds <= 0.0 {
        bail!("timeout must be a positive finite number");
    }
    Ok(Duration::from_secs_f64(seconds))
}

fn cloudflare_credentials() -> Result<Option<(String, String)>> {
    let account_id = load_secret_env_var("CLOUDFLARE_ACCOUNT_ID")?;
    let api_token = load_secret_env_var("CLOUDFLARE_API_TOKEN")?;
    match (account_id, api_token) {
        (Some(account_id), Some(api_token)) => Ok(Some((account_id, api_token))),
        (None, None) => Ok(None),
        (Some(_), None) => {
            bail!("missing CLOUDFLARE_API_TOKEN; set it in shell env or Flow personal env store")
        }
        (None, Some(_)) => {
            bail!("missing CLOUDFLARE_ACCOUNT_ID; set it in shell env or Flow personal env store")
        }
    }
}

fn load_secret_env_var(key: &str) -> Result<Option<String>> {
    if let Ok(value) = std::env::var(key) {
        let trimmed = value.trim();
        if !trimmed.is_empty() {
            return Ok(Some(trimmed.to_string()));
        }
    }

    let primary = flow_env::get_personal_env_var(key)
        .with_context(|| format!("failed to load {key} from Flow personal env store"));
    match primary {

View on GitHub (pinned to a747e741ae)

Solutions

  1. Set CLOUDFLARE_API_TOKEN in the shell environment (export CLOUDFLARE_API_TOKEN=...) or via `flow env set CLOUDFLARE_API_TOKEN=...`.
  2. Verify the variable name is exactly CLOUDFLARE_API_TOKEN (no typo, no extra suffix).
  3. Check the value is non-empty after trimming — an empty or whitespace-only value counts as missing.
  4. Alternatively, remove CLOUDFLARE_ACCOUNT_ID too if you intended to disable Cloudflare integration entirely (None/None is accepted).
  5. Confirm the Flow personal env store is reachable if you rely on it instead of shell env.

Example fix

// before: partial credentials in env
// CLOUDFLARE_ACCOUNT_ID=abc123
// after: export both
// CLOUDFLARE_ACCOUNT_ID=abc123
// CLOUDFLARE_API_TOKEN=cf-token-here
Defensive patterns

Strategy: validation

Validate before calling

let token = std::env::var("CLOUDFLARE_API_TOKEN").ok().filter(|v| !v.trim().is_empty());
let account = std::env::var("CLOUDFLARE_ACCOUNT_ID").ok().filter(|v| !v.trim().is_empty());
if account.is_some() && token.is_none() {
    return Err(anyhow!("missing CLOUDFLARE_API_TOKEN; set it in shell env or Flow personal env store"));
}

Try / catch

match inspect_url(url, &opts) {
    Err(e) if e.to_string().contains("missing CLOUDFLARE_API_TOKEN") => {
        eprintln!("Cloudflare creds incomplete; skipping CF path");
        inspect_url_no_cloudflare(url, &opts)?
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling inspect_url or crawl_url when CLOUDFLARE_ACCOUNT_ID is present (non-empty) in shell env or Flow store, but CLOUDFLARE_API_TOKEN is unset or empty.

Common situations: Setting only one of the two Cloudflare variables in .env or the Flow env store; a typo like CLOUDFLARE_API_TOKEN vs CLOUDFLARE_API_KEY; empty-string export from a failed secret fetch; CI environment where only the account ID was configured.

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 nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/da33876896840281. Report an issue: GitHub.