{"record":{"id":"33aea36622f54acd","repo":"Hmbown/CodeWhale","slug":"xai-api-key-not-found-get-a-key-https-console","errorCode":null,"errorMessage":"xAI API key not found. Get a key: https://console.x.ai/\nRun 'codewhale auth set --provider xai', set XAI_API_KEY, or add [providers.xai] api_key.\nOAuth alternative: run `codewhale auth xai-device` for Codewhale-owned storage and set [providers.xai] auth_mode = \"oauth\".","messagePattern":"xAI API key not found\\. Get a key: https://console\\.x\\.ai/\nRun 'codewhale auth set --provider xai', set XAI_API_KEY, or add \\[providers\\.xai\\] api_key\\.\nOAuth alternative: run `codewhale auth xai-device` for Codewhale-owned storage and set \\[providers\\.xai\\] auth_mode = \"oauth\"\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/config.rs","lineNumber":7405,"sourceCode":"            ApiProvider::Anthropic | ApiProvider::Openmodel => {\n                anyhow::bail!(\"{}\", missing_provider_api_key_message(provider)?)\n            }\n            ApiProvider::OpencodeZen => {\n                anyhow::bail!(\"{}\", missing_provider_api_key_message(provider)?)\n            }\n            ApiProvider::OpenaiCodex => anyhow::bail!(\n                \"{}\",\n                crate::oauth::missing_auth_message(crate::oauth::OAuthProvider::Chatgpt)\n            ),\n            ApiProvider::Xai => {\n                // Prefer OAuth guidance when auth_mode requests it or Grok CLI\n                // tokens already exist; otherwise show both API-key and OAuth.\n                if self\n                    .provider_config_for(provider)\n                    .is_some_and(provider_config_uses_xai_oauth)\n                    || crate::oauth::credentials_present(crate::oauth::OAuthProvider::Xai, self)\n                {\n                    anyhow::bail!(\n                        \"{}\",\n                        crate::oauth::missing_auth_message(crate::oauth::OAuthProvider::Xai)\n                    );\n                }\n                anyhow::bail!(\n                    \"xAI API key not found. Get a key: https://console.x.ai/\\n\\\n                     Run 'codewhale auth set --provider xai', set XAI_API_KEY, or add \\\n                     [providers.xai] api_key.\\n\\\n                     OAuth alternative: run `codewhale auth xai-device` for \\\n                     Codewhale-owned storage and set [providers.xai] auth_mode = \\\"oauth\\\".\"\n                );\n            }\n            // Self-hosted deployments commonly run without auth on localhost.\n            // Return an empty key and let the client omit the Authorization header.\n            ApiProvider::Sglang | ApiProvider::Vllm => Ok(String::new()),\n            ApiProvider::Ollama\n                if provider_route_is_keyless_self_hosted(\n                    provider,","sourceCodeStart":7387,"sourceCodeEnd":7423,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/config.rs#L7387-L7423","documentation":"The TUI/config layer refuses to start a session for the xAI provider because no API key (or OAuth credential) can be found. The library checks the provider config entry for xAI OAuth usage and codewhale's OAuth credential store; if neither has tokens and no static API key is configured, it bails with setup instructions. It exists to fail loud at load rather than let a request go out unauthenticated.","triggerScenarios":"Selecting/using provider `xai` when: no XAI_API_KEY env var, no [providers.xai] api_key in ~/.codewhale/config.toml, no stored OAuth tokens for Xai, and the provider config is not flagged auth_mode=\"oauth\" with existing tokens.","commonSituations":"Fresh install without `codewhale auth set --provider xai`; CI/container where XAI_API_KEY is unset; user deleted or renamed the config table; switching machines without migrating ~/.codewhale credentials.","solutions":["Run `codewhale auth set --provider xai` and paste an API key from https://console.x.ai/","Set the XAI_API_KEY environment variable in your shell/CI environment","Add `api_key = \"xai-...\"` under [providers.xai] in ~/.codewhale/config.toml","Or run `codewhale auth xai-device` for OAuth and set auth_mode = \"oauth\" under [providers.xai]"],"exampleFix":"// before (config.toml)\n[providers.xai]\n// after\n[providers.xai]\napi_key = \"xai-your-key-here\"\n# or: auth_mode = \"oauth\"","handlingStrategy":"validation","validationCode":"let ok = std::env::var(\"XAI_API_KEY\").map(|v| !v.trim().is_empty()).unwrap_or(false);\nif !ok { eprintln!(\"xAI key missing: run `codewhale auth set --provider xai` or set XAI_API_KEY\"); }\nelse { /* proceed */ }","typeGuard":null,"tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"xAI API key not found\") => configure_xai_auth_and_retry(),\n    other => other?,\n}","preventionTips":["Provision keys via `codewhale auth set` once per machine","Keep XAI_API_KEY in your shell profile or secret manager","In CI, inject the key as a secret env var"],"tags":["authentication","api-key","config","xai"],"backgroundTag":"missing-api-key","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}