{"record":{"id":"1a652afdaf3596ca","repo":"abi/screenshot-to-code","slug":"no-openai-anthropic-or-gemini-api-key-found-ple","errorCode":null,"errorMessage":"No OpenAI, Anthropic, or Gemini API key found. Please add the environment variable OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY to backend/.env or in the settings dialog. If you add it to .env, make sure to restart the backend server.","messagePattern":"No OpenAI, Anthropic, or Gemini API key found\\. Please add the environment variable OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY to backend/\\.env or in the settings dialog\\. If you add it to \\.env, make sure to restart the backend server\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"backend/routes/generate_code.py","lineNumber":449,"sourceCode":"                num_variants,\n                openai_api_key,\n                anthropic_api_key,\n                gemini_api_key,\n            )\n\n            # Print the variant models (one per line)\n            print(\"Variant models:\")\n            for index, model in enumerate(variant_models):\n                print(f\"Variant {index + 1}: {model.value}\")\n\n            return variant_models\n        except Exception:\n            await self.throw_error(\n                \"No OpenAI, Anthropic, or Gemini API key found. Please add the environment variable \"\n                \"OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY to backend/.env or in the settings dialog. \"\n                \"If you add it to .env, make sure to restart the backend server.\"\n            )\n            raise Exception(\"No API key\")\n\n    def _get_variant_models(\n        self,\n        generation_type: Literal[\"create\", \"update\"],\n        input_mode: InputMode,\n        num_variants: int,\n        openai_api_key: str | None,\n        anthropic_api_key: str | None,\n        gemini_api_key: str | None,\n    ) -> List[Llm]:\n        \"\"\"Simple model cycling that scales with num_variants\"\"\"\n\n        # Video mode requires Gemini - 2 variants for comparison\n        if input_mode == \"video\":\n            if not gemini_api_key:\n                raise Exception(\n                    \"Video mode requires a Gemini API key. \"\n                    \"Please add GEMINI_API_KEY to backend/.env or in the settings dialog\"","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/routes/generate_code.py#L431-L467","documentation":"Error emitted via throw_error in backend/routes/generate_code.py:449 by the variant-model resolver: its try block (which selects models per variant based on available keys) raised, which in practice means none of OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY was available from settings or the environment. Generation cannot proceed without at least one provider key, and the resolver's bare 'except Exception' converts any failure there into this message followed by raise Exception('No API key').","triggerScenarios":"Starting a generation with no API key in backend/.env, no key entered in the in-app Settings dialog, and no key in the process environment; or the keys exist but all selected models' providers are unavailable. Note REPLICATE_API_KEY does not count here.","commonSituations":"Fresh setup where backend/.env was never created; .env edited after the backend started (values are read at startup — restart required); key placed in the frontend .env instead of backend/.env; relying on REPLICATE_API_KEY alone.","solutions":["Add OPENAI_API_KEY (or ANTHROPIC_API_KEY / GEMINI_API_KEY) to backend/.env and restart the backend.","Or enter the key at runtime in the app's Settings dialog, which persists it without a restart.","Verify with a simple script that the key is non-empty in the backend process env; confirm it is not only REPLICATE_API_KEY."],"exampleFix":"# before (backend/.env)\nREPLICATE_API_KEY=r8_xxx\n\n# after (backend/.env)\nREPLICATE_API_KEY=r8_xxx\nOPENAI_API_KEY=sk-...\n# then restart: poetry run uvicorn main:app --reload --port 7001","handlingStrategy":"validation","validationCode":"// client-side preflight: refuse to start generation without a key configured\nif (!settings.openAiApiKey && !settings.anthropicApiKey && !settings.geminiApiKey) {\n  openSettingsDialog();\n  return; // do not open the generation WebSocket\n}","typeGuard":null,"tryCatchPattern":"ws.onmessage = (ev) => {\n  const msg = JSON.parse(ev.data);\n  if (msg.type === 'error' && msg.message.includes('API key')) {\n    // open Settings dialog; after the user saves a key, retry generation\n  }\n};","preventionTips":["Put provider keys in backend/.env and restart the backend after editing.","Remember REPLICATE_API_KEY alone is not sufficient for text generation.","Check key presence before opening the generation stream, not after failure."],"tags":["api-key","configuration","env-vars","generate-code","websocket"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}