{"record":{"id":"7f923d6890441b28","repo":"BerriAI/litellm","slug":"openrouter-api-key-is-required-set-openrouter-api","errorCode":null,"errorMessage":"OpenRouter API key is required. Set OPENROUTER_API_KEY environment variable or pass api_key parameter.","messagePattern":"OpenRouter API key is required\\. Set OPENROUTER_API_KEY environment variable or pass api_key parameter\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/openrouter/responses/transformation.py","lineNumber":51,"sourceCode":"    def custom_llm_provider(self) -> LlmProviders:\n        return LlmProviders.OPENROUTER\n\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        litellm_params: GenericLiteLLMParams | None,\n    ) -> dict:\n        litellm_params = litellm_params or GenericLiteLLMParams()\n        api_key: Final = (\n            litellm_params.api_key\n            or litellm.api_key\n            or get_secret_str(\"OPENROUTER_API_KEY\")\n            or get_secret_str(\"OR_API_KEY\")\n        )\n\n        if not api_key:\n            raise ValueError(\n                \"OpenRouter API key is required. Set OPENROUTER_API_KEY environment variable or pass api_key parameter.\"\n            )\n\n        headers.update(\n            {\n                \"Authorization\": f\"Bearer {api_key}\",\n            }\n        )\n        return headers\n\n    def get_complete_url(\n        self,\n        api_base: str | None,\n        litellm_params: dict,\n    ) -> str:\n        api_base = (\n            api_base or litellm.api_base or get_secret_str(\"OPENROUTER_API_BASE\") or \"https://openrouter.ai/api/v1\"\n        )","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/openrouter/responses/transformation.py#L33-L69","documentation":"OpenRouter's Responses API transformation builds auth headers before any request: it checks litellm_params.api_key, then the global litellm.api_key, then the OPENROUTER_API_KEY and OR_API_KEY environment variables. If all are empty it raises this ValueError; nothing is sent over the network.","triggerScenarios":"Calling litellm.responses(model='openrouter/...') without an api_key argument while neither OPENROUTER_API_KEY nor OR_API_KEY is set in the process environment (including the OR_API_KEY alias).","commonSituations":"Key exported in an interactive shell but missing in the container/service/cron environment; .env file not loaded before litellm runs; typo in the variable name; proxy deployments where the key was configured on a different virtual key/deployment.","solutions":["export OPENROUTER_API_KEY=sk-or-... in the environment that actually runs litellm","Pass the key per call: litellm.responses(model='openrouter/...', api_key='sk-or-...')","Alternatively set the accepted alias OR_API_KEY","For litellm proxy, configure api_key on the openrouter deployment in config.yaml instead of relying on ambient env"],"exampleFix":"// before\nresp = litellm.responses(model=\"openrouter/openai/gpt-4o\", input=\"hello\")\n\n// after\nimport os\nresp = litellm.responses(model=\"openrouter/openai/gpt-4o\", input=\"hello\", api_key=os.environ[\"OPENROUTER_API_KEY\"])","handlingStrategy":"validation","validationCode":"import os, litellm\n\ndef openrouter_key_present() -> bool:\n    return bool(\n        litellm.api_key\n        or os.getenv(\"OPENROUTER_API_KEY\")\n        or os.getenv(\"OR_API_KEY\")\n    )\n\nassert openrouter_key_present(), \"OPENROUTER_API_KEY (or OR_API_KEY) must be set\"  # run at startup","typeGuard":null,"tryCatchPattern":"try/except ValueError around litellm.responses calls can convert the failure into a clear configuration error for ops (the exception is deterministic - no retry makes sense).","preventionTips":["Fail fast at startup: assert the key is resolvable before serving traffic","Load .env files explicitly in long-running processes","In proxies, configure the key on the deployment in config.yaml rather than ambient shell env"],"tags":["openrouter","responses-api","authentication","env-var","litellm"],"backgroundTag":"missing-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}