{"record":{"id":"6ef624c2322982db","repo":"xtekky/gpt4free","slug":"add-a-api-key","errorCode":null,"errorMessage":"Add a \"api_key\"","messagePattern":"Add a \"api_key\"","errorType":"exception","errorClass":"MissingAuthError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Anthropic.py","lineNumber":107,"sourceCode":"        proxy: str = None,\n        timeout: int = 120,\n        media: MediaListType = None,\n        api_key: str = None,\n        temperature: float = None,\n        max_tokens: int = 4096,\n        top_k: int = None,\n        top_p: float = None,\n        stop: list[str] = None,\n        stream: bool = False,\n        headers: dict = None,\n        impersonate: str = None,\n        tools: Optional[list] = None,\n        beta_headers: Optional[list] = None,\n        extra_body: dict = {},\n        **kwargs,\n    ) -> AsyncResult:\n        if api_key is None:\n            raise MissingAuthError('Add a \"api_key\"')\n\n        # Handle image inputs\n        if media is not None:\n            insert_images = []\n            for image, _ in media:\n                data = to_bytes(image)\n                insert_images.append(\n                    {\n                        \"type\": \"image\",\n                        \"source\": {\n                            \"type\": \"base64\",\n                            \"media_type\": is_accepted_format(data),\n                            \"data\": base64.b64encode(data).decode(),\n                        },\n                    }\n                )\n            messages[-1][\"content\"] = [\n                *insert_images,","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Anthropic.py#L89-L125","documentation":"MissingAuthError raised at the top of Anthropic.create_async_generator: the api_key parameter is None. Unlike browser-based providers, Anthropic's API is key-only, so there is nothing to fall back on and the call is rejected before any request is made. This mirrors g4f's standard 'needs_auth' pattern for missing credentials.","triggerScenarios":"Calling the Anthropic provider without api_key=... and with no key registered in AuthManager for Anthropic (g4f injects stored keys automatically, so this fires when neither exists); passing api_key=None explicitly; AppConfig.disable_custom_api_key dropping the key.","commonSituations":"Using g4f.client or the API server without configuring the Anthropic key first; expecting g4f's free providers and accidentally selecting an Anthropic model; typos in the kwarg name (e.g. apiKey) so the real key never reaches api_key.","solutions":["Get an API key from console.anthropic.com and pass it: provider call with api_key='sk-ant-...', or g4f.client.ChatCompletion.create(..., api_key=...).","Or store it persistently so AuthManager supplies it: g4f.SecretStr / the g4f auth storage for the Anthropic provider.","Set the ANTHROPIC_API_KEY environment variable if your g4f version reads it.","Verify you actually intended an authenticated provider — a free alternative may suit better."],"exampleFix":"# before\nresponse = await Anthropic.create_async_generator(model=\"claude-3-5-sonnet-latest\", messages=messages)\n\n# after\nresponse = await Anthropic.create_async_generator(\n    model=\"claude-3-5-sonnet-latest\", messages=messages, api_key=\"sk-ant-...\"\n)","handlingStrategy":"validation","validationCode":"api_key = api_key or os.environ.get('ANTHROPIC_API_KEY')\nif not api_key:\n    raise SystemExit('Set ANTHROPIC_API_KEY or pass api_key= to use the Anthropic provider')","typeGuard":"def has_anthropic_key(api_key) -> bool:\n    return isinstance(api_key, str) and api_key.strip() != ''","tryCatchPattern":"try:\n    ...\nexcept MissingAuthError as e:\n    if 'api_key' in str(e):\n        api_key = prompt_for_key()  # collect once, store via AuthManager\n        ...","preventionTips":["Configure the Anthropic key once via g4f's auth storage before first use.","Validate credentials at startup, not per request.","Catch MissingAuthError to route users to a credential-setup flow."],"tags":["auth","anthropic","api-key","missing-credentials"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}