{"record":{"id":"915ea6dba9f5ce02","repo":"santifer/career-ops","slug":"models-failed-to-fetch-free-model-list-reason-915ea6","errorCode":null,"errorMessage":"[models] Failed to fetch free model list: ${reason}. OPENROUTER_API_KEY is not set — copy .env.example to .env and add your key.","messagePattern":"\\[models\\] Failed to fetch free model list: (.+?)\\. OPENROUTER_API_KEY is not set — copy \\.env\\.example to \\.env and add your key\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"openrouter-runner.mjs","lineNumber":140,"sourceCode":"    if (list.length === 0) throw new Error('No free models found in API response');\n\n    // Sort by provider priority; within the same provider sort alphabetically\n    function providerOf(id) { return id.split('/')[0]; }\n    function priorityOf(id) {\n      const idx = PROVIDER_PRIORITY.indexOf(providerOf(id));\n      return idx === -1 ? PROVIDER_PRIORITY.length : idx;\n    }\n\n    freeModels = list.sort((a, b) => {\n      const diff = priorityOf(a) - priorityOf(b);\n      return diff !== 0 ? diff : a.localeCompare(b);\n    });\n\n    console.log(`[models] ${freeModels.length} free models loaded from OpenRouter API.`);\n  } catch (e) {\n    const reason = e instanceof Error ? e.message : String(e);\n    const hasKey = Boolean(process.env.OPENROUTER_API_KEY);\n    throw new Error(\n      `[models] Failed to fetch free model list: ${reason}. ` +\n      (hasKey ? 'Check that your API key is valid and that network access to OpenRouter is available.'\n               : 'OPENROUTER_API_KEY is not set — copy .env.example to .env and add your key.')\n    );\n  }\n\n  return freeModels;\n}\n\n// List and exit (helper command)\nasync function cmdModels() {\n  const models = await loadFreeModels();\n  console.log(`\\nFree models available on OpenRouter (${models.length} total):\\n`);\n  models.forEach((id, i) => console.log(`  ${String(i + 1).padStart(2)}. ${id}`));\n  console.log('');\n}\n\n// ---------------------------------------------------------------------------","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/santifer/career-ops/blob/60398d6549a46f5266929538af21cfab94badc75/openrouter-runner.mjs#L122-L158","documentation":"Same wrapper as the models-fetch failure (openrouter-runner.mjs:140), but this branch is selected when process.env.OPENROUTER_API_KEY is falsy. The .env loader at the top of the script (lines 40-48) only reads .env from the script's own directory and only fills variables that are not already defined, and its regex requires a bare KEY=value line — so an empty var, a typo'd name, or spaces around '=' leaves the key unset. With no key the request still goes out (as 'Bearer undefined') and whatever failure resulted is appended as ${reason}.","triggerScenarios":"Running openrouter-runner.mjs with no .env in the repo root (or OPENROUTER_API_KEY= empty, or 'OPENROUTER_API_KEY = \"sk-...\"' with spaces that break the /^([A-Z_][A-Z0-9_]*)=(.*)$/ parse), causing the models fetch to fail; the catch then reports the missing key as the actionable cause.","commonSituations":"Fresh clone without copying .env.example to .env; key added to the wrong file (~/.env instead of repo .env); CI environment missing the secret; var name misspelled (OPENROUTER_KEY, OPENROUTER_APIKEY).","solutions":["cp .env.example .env in the repo root, then set OPENROUTER_API_KEY=sk-or-v1-... (no spaces around '=', no quotes needed)","Get a free key at https://openrouter.ai if you do not have one","Alternatively export it in the shell: export OPENROUTER_API_KEY=sk-or-... (env vars win over .env)","Verify with: node -e \"console.log(Boolean(process.env.OPENROUTER_API_KEY))\" from the same directory you run the runner from"],"exampleFix":"# before\n# .env contains:  OPENROUTER_API_KEY = \"sk-or-v1-xyz\"   (spaces break the parser)\n\n# after\n# .env contains:  OPENROUTER_API_KEY=sk-or-v1-xyz","handlingStrategy":"validation","validationCode":"// Gate before any run: the .env loader only fills UNDEFINED vars, so test exactly that\nfunction assertOpenRouterKey() {\n  if (!process.env.OPENROUTER_API_KEY) {\n    throw new Error('OPENROUTER_API_KEY is not set. Copy .env.example to .env (repo root) and add your key — free at https://openrouter.ai');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await run(mode);\n} catch (e) {\n  if (e.message.includes('OPENROUTER_API_KEY is not set')) {\n    console.error('Setup: cp .env.example .env && edit .env');\n    process.exit(3); // dedicated exit code for setup errors\n  }\n  throw e;\n}","preventionTips":["Write .env lines as KEY=value with no spaces around '=' and no inline comments","Verify with: node -e \"console.assert(process.env.OPENROUTER_API_KEY, 'missing')\" after editing .env","Remember vars already present in the environment take precedence — an empty exported var suppresses the .env value"],"tags":["openrouter","llm","env","setup","api-key"],"backgroundTag":"missing-env-var","analyzedSha":"60398d6549a46f5266929538af21cfab94badc75","analyzedAt":"2026-08-20T23:00:06.764Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}