xtekky/gpt4free · error · MissingAuthError

Add a "api_key"

Error message

Add a "api_key"

What it means

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.

Source

Thrown at g4f/Provider/needs_auth/Anthropic.py:107

        proxy: str = None,
        timeout: int = 120,
        media: MediaListType = None,
        api_key: str = None,
        temperature: float = None,
        max_tokens: int = 4096,
        top_k: int = None,
        top_p: float = None,
        stop: list[str] = None,
        stream: bool = False,
        headers: dict = None,
        impersonate: str = None,
        tools: Optional[list] = None,
        beta_headers: Optional[list] = None,
        extra_body: dict = {},
        **kwargs,
    ) -> AsyncResult:
        if api_key is None:
            raise MissingAuthError('Add a "api_key"')

        # Handle image inputs
        if media is not None:
            insert_images = []
            for image, _ in media:
                data = to_bytes(image)
                insert_images.append(
                    {
                        "type": "image",
                        "source": {
                            "type": "base64",
                            "media_type": is_accepted_format(data),
                            "data": base64.b64encode(data).decode(),
                        },
                    }
                )
            messages[-1]["content"] = [
                *insert_images,

View on GitHub (pinned to 973504e177)

Solutions

  1. 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=...).
  2. Or store it persistently so AuthManager supplies it: g4f.SecretStr / the g4f auth storage for the Anthropic provider.
  3. Set the ANTHROPIC_API_KEY environment variable if your g4f version reads it.
  4. Verify you actually intended an authenticated provider — a free alternative may suit better.

Example fix

# before
response = await Anthropic.create_async_generator(model="claude-3-5-sonnet-latest", messages=messages)

# after
response = await Anthropic.create_async_generator(
    model="claude-3-5-sonnet-latest", messages=messages, api_key="sk-ant-..."
)
Defensive patterns

Strategy: validation

Validate before calling

api_key = api_key or os.environ.get('ANTHROPIC_API_KEY')
if not api_key:
    raise SystemExit('Set ANTHROPIC_API_KEY or pass api_key= to use the Anthropic provider')

Type guard

def has_anthropic_key(api_key) -> bool:
    return isinstance(api_key, str) and api_key.strip() != ''

Try / catch

try:
    ...
except MissingAuthError as e:
    if 'api_key' in str(e):
        api_key = prompt_for_key()  # collect once, store via AuthManager
        ...

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/6ef624c2322982db. Report an issue: GitHub.