{"record":{"id":"3c72b79a210fe6d6","repo":"BerriAI/litellm","slug":"missing-cloudflare-api-key-a-call-is-being-made","errorCode":null,"errorMessage":"Missing Cloudflare API Key - A call is being made to cloudflare but no key is set either in the environment variables or via params","messagePattern":"Missing Cloudflare API Key - A call is being made to cloudflare but no key is set either in the environment variables or via params","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/cloudflare/chat/transformation.py","lineNumber":76,"sourceCode":"        if trimmed.endswith(\"/ai/run\"):\n            verbose_logger.warning(\n                \"Cloudflare api_base ending in '/ai/run' is the legacy Workers AI path and no longer serves OpenAI-compatible requests; rewriting to the '/ai/v1' endpoint\"\n            )\n            return f\"{trimmed[: -len('/ai/run')]}/ai/v1\"\n        return api_base\n\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ) -> dict:\n        if api_key is None:\n            raise ValueError(\n                \"Missing Cloudflare API Key - A call is being made to cloudflare but no key is set either in the environment variables or via params\"\n            )\n        return super().validate_environment(\n            headers=headers,\n            model=model,\n            messages=messages,\n            optional_params=optional_params,\n            litellm_params=litellm_params,\n            api_key=api_key,\n            api_base=api_base,\n        )\n\n    def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:\n        return CloudflareError(\n            status_code=status_code,\n            message=error_message,\n        )\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/cloudflare/chat/transformation.py#L58-L94","documentation":"ValueError raised in the Cloudflare adapter's validate_environment when the api_key argument is None — meaning neither the api_key parameter nor any environment fallback produced a credential. Unlike many providers, this adapter refuses to build headers without a key rather than sending an anonymous request that would fail upstream.","triggerScenarios":"Calling a cloudflare/* model with no api_key passed and no CLOUDFLARE_API_KEY (or provider-prefixed equivalent) present in the environment at request time.","commonSituations":"Only CLOUDFLARE_ACCOUNT_ID was configured; key stored under a differently-named variable; env vars set in a local shell but not in Docker/CI; typos in the variable name.","solutions":["Set the key in the environment: export CLOUDFLARE_API_KEY=<token> (a Workers AI / API token with Workers AI permission)","Or pass it per call: litellm.completion(model=\"cloudflare/...\", api_key=token, messages=messages)","Verify with printenv that the key exists in the process that runs litellm","If using the Router/proxy, set the key in the deployment environment or model_list entry, not just locally"],"exampleFix":"# before\nlitellm.completion(model=\"cloudflare/@cf/meta/llama-3.1-8b-instruct\", messages=messages)\n# ValueError: Missing Cloudflare API Key\n\n# after\nos.environ[\"CLOUDFLARE_API_KEY\"] = \"your-workers-ai-token\"\nlitellm.completion(model=\"cloudflare/@cf/meta/llama-3.1-8b-instruct\", messages=messages)","handlingStrategy":"validation","validationCode":"import os\n\napi_key = os.environ.get(\"CLOUDFLARE_API_KEY\") or os.environ.get(\"CLOUDFLARE_AI_TOKEN\")\nif not api_key:\n    raise RuntimeError(\"Cloudflare API key missing; set CLOUDFLARE_API_KEY or pass api_key=\")\n\nlitellm.completion(\n    model=\"cloudflare/@cf/meta/llama-3.1-8b-instruct\",\n    messages=messages,\n    api_key=api_key,\n)","typeGuard":null,"tryCatchPattern":"try:\n    litellm.completion(model=\"cloudflare/@cf/...\", messages=messages)\nexcept ValueError as e:\n    if \"Missing Cloudflare API Key\" in str(e):\n        litellm.completion(\n            model=\"cloudflare/@cf/...\", messages=messages, api_key=get_key_from_secret_store()\n        )\n    else:\n        raise","preventionTips":["Set CLOUDFLARE_API_KEY (Workers AI token) wherever CLOUDFLARE_ACCOUNT_ID is set","Use a startup credential check across all providers to catch missing keys before traffic","Prefer explicit api_key= in short scripts so the dependency is visible"],"tags":["cloudflare","api-key","config","auth"],"backgroundTag":"missing-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}