{"record":{"id":"495dd60268f55015","repo":"slopus/happy","slug":"gemini-no-api-key-found-run-happy-connect-gemi","errorCode":null,"errorMessage":"[Gemini] No API key found. Run 'happy connect gemini' to authenticate via Google OAuth, or set ${GEMINI_API_KEY_ENV} environment variable.","messagePattern":"\\[Gemini\\] No API key found\\. Run 'happy connect gemini' to authenticate via Google OAuth, or set (.+?) environment variable\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/happy-cli/src/agent/factories/gemini.ts","lineNumber":92,"sourceCode":"export function createGeminiBackend(options: GeminiBackendOptions): GeminiBackendResult {\n\n  // Resolve API key from multiple sources (in priority order):\n  // 1. Happy cloud OAuth token (via 'happy connect gemini') - highest priority\n  // 2. Local Gemini CLI config files (~/.gemini/)\n  // 3. GEMINI_API_KEY environment variable\n  // 4. GOOGLE_API_KEY environment variable - lowest priority\n  \n  // Try reading from local Gemini CLI config (token and model)\n  const localConfig = readGeminiLocalConfig();\n  \n  let apiKey = options.cloudToken       // 1. Happy cloud token (passed from runGemini)\n    || localConfig.token                // 2. Local config (~/.gemini/)\n    || process.env[GEMINI_API_KEY_ENV]  // 3. GEMINI_API_KEY env var\n    || process.env[GOOGLE_API_KEY_ENV]  // 4. GOOGLE_API_KEY env var\n    || options.apiKey;                  // 5. Explicit apiKey option (fallback)\n\n  if (!apiKey) {\n    logger.warn(`[Gemini] No API key found. Run 'happy connect gemini' to authenticate via Google OAuth, or set ${GEMINI_API_KEY_ENV} environment variable.`);\n  }\n\n  // Command to run gemini\n  const geminiCommand = 'gemini';\n  \n  // Get model from options, local config, system environment, or use default\n  // Priority: options.model (if provided) > local config > env var > default\n  // If options.model is undefined, check local config, then env, then use default\n  // If options.model is explicitly null, skip local config and use env/default\n  const model = determineGeminiModel(options.model, localConfig);\n\n  // Build args - use only --experimental-acp flag\n  // Model is passed via GEMINI_MODEL env var (gemini CLI reads it automatically)\n  // We don't use --model flag to avoid potential stdout conflicts with ACP protocol\n  const geminiArgs = ['--experimental-acp'];\n\n  // Get Google Cloud Project from local config (for Workspace accounts)\n  // Only use if: no email stored (global), or email matches current user","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/slopus/happy/blob/b824cd0a4681d41af631a8e422a813873e4455b0/packages/happy-cli/src/agent/factories/gemini.ts#L74-L110","documentation":"createGeminiBackend resolves a Gemini API key from (in order) happy auth storage, local ~/.gemini config, GEMINI_API_KEY, GOOGLE_API_KEY, and an explicit apiKey option. If none is present it logs this warning; the backend is still created but gemini will likely fail to authenticate when launched.","triggerScenarios":"Running happy with the gemini agent (happy gemini) before ever running 'happy connect gemini', with no GEMINI_API_KEY/GOOGLE_API_KEY env var set in the current (or daemon) environment, and no apiKey in options.","commonSituations":"Fresh machine after cloning dotfiles without the key, CI/daemon environment that lacks the interactive shell's exported GEMINI_API_KEY, or key stored in ~/.gemini that was wiped/reinstalled.","solutions":["Run `happy connect gemini` to authenticate via Google OAuth.","Or export GEMINI_API_KEY=<your-key> (or GOOGLE_API_KEY) in the environment where happy runs.","If using the daemon, set the env var in the daemon's environment and restart it, since daemons don't inherit your shell env.","Verify with `echo $GEMINI_API_KEY` that the variable is set and non-empty in the actual execution environment."],"exampleFix":"// before\nhappy gemini  // warn: no API key\n// after\nexport GEMINI_API_KEY=AIza...\nhappy gemini","handlingStrategy":"validation","validationCode":"if (!process.env.GEMINI_API_KEY && !process.env.GOOGLE_API_KEY && !fs.existsSync(require('os').homedir() + '/.gemini/oauth_creds.json')) {\n  console.error('No Gemini credentials: run `happy connect gemini` or export GEMINI_API_KEY.');\n  process.exit(1);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run `happy connect gemini` once on each machine.","Export GEMINI_API_KEY in shell profile AND in the daemon environment.","In CI, inject the key as a secret env var.","For daemons, use an EnvironmentFile so the key survives restarts."],"tags":["gemini","api-key","authentication","env-var"],"backgroundTag":"missing-env-var","analyzedSha":"b824cd0a4681d41af631a8e422a813873e4455b0","analyzedAt":"2026-08-31T23:12:36.205Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}