nikivdev/code · error
OPENROUTER_API_KEY not set. Get one at https://openrouter.ai
Error message
OPENROUTER_API_KEY not set. Get one at https://openrouter.ai/keys
What it means
Thrown when the commit review/generation feature needs an OpenRouter API key but none can be found in the environment or config lookup. The message includes a link to obtain a key at https://openrouter.ai/keys. The library never silently proceeds without credentials for the AI-backed path.
Source
Thrown at src/commit.rs:13170
fn openrouter_api_key() -> Result<String> {
if let Ok(value) = std::env::var("OPENROUTER_API_KEY") {
if !value.trim().is_empty() {
return Ok(value);
}
}
if is_local_env_backend() {
if let Ok(vars) = crate::env::fetch_personal_env_vars(&["OPENROUTER_API_KEY".to_string()]) {
if let Some(value) = vars.get("OPENROUTER_API_KEY") {
if !value.trim().is_empty() {
return Ok(value.clone());
}
}
}
}
bail!("OPENROUTER_API_KEY not set. Get one at https://openrouter.ai/keys");
}
fn parse_review_json(output: &str) -> Option<ReviewJson> {
let trimmed = output.trim();
if trimmed.is_empty() {
return None;
}
if let Ok(parsed) = serde_json::from_str::<ReviewJson>(trimmed) {
return Some(parsed);
}
let start = trimmed.find('{')?;
let end = trimmed.rfind('}')?;
if end <= start {
return None;
}
let candidate = &trimmed[start..=end];View on GitHub (pinned to a747e741ae)
Solutions
- Export the key: `export OPENROUTER_API_KEY=sk-or-...` (get one at https://openrouter.ai/keys)
- Add the key to your CI secrets / .env file so automated runs get it
- Verify with `echo ${OPENROUTER_API_KEY:+set}` that the variable is visible to the process
- If you meant a different provider, configure that provider's key/env name instead
Example fix
// before (shell) cargo run -- commit --ai // error: OPENROUTER_API_KEY not set // after (shell) export OPENROUTER_API_KEY=sk-or-v1-... cargo run -- commit --ai
Defensive patterns
Strategy: validation
Validate before calling
// run before invoking AI-backed commands
if std::env::var("OPENROUTER_API_KEY").map_or(true, |v| v.trim().is_empty()) {
eprintln!("OPENROUTER_API_KEY not set — get one at https://openrouter.ai/keys");
std::process::exit(1);
} Try / catch
match run_ai_commit() {
Err(e) if e.to_string().contains("OPENROUTER_API_KEY") => {
eprintln!("Set OPENROUTER_API_KEY (https://openrouter.ai/keys), then retry.");
std::process::exit(2);
}
other => other,
} Prevention
- Commit a documented .env.example listing OPENROUTER_API_KEY
- Declare the secret in CI (GitHub Actions secrets) before enabling AI features
- Source your profile or use direnv so the key is set in every shell
- Never commit the real key; rotate if leaked
When it happens
Trigger: Invoking the AI commit-message/review flow when OPENROUTER_API_KEY is absent from the process environment and no alternate config location checked by the key-lookup loop provides a value.
Common situations: Fresh clone on a new machine without .env, CI pipeline missing the secret, key stored under a different variable name (e.g. OPENAI_API_KEY), or shell not sourcing the profile that exports the key.
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
- Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to
- could not resolve commit report directory
- fish shell required. Run: chsh -s {}
- missing CLOUDFLARE_API_TOKEN; set it in shell env or Flow pe
- missing CLOUDFLARE_ACCOUNT_ID; set it in shell env or Flow p
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/1aea4c043a8127da.
Report an issue: GitHub.