xtekky/gpt4free · error · MissingAuthError

API key is required.

Error message

API key is required.

What it means

Error "API key is required." thrown in xtekky/gpt4free.

Source

Thrown at g4f/Provider/template/OpenaiTemplate.py:52

    supports_native_tools: bool = True
    _checked_api_keys: dict = {}

    @classmethod
    async def get_quota(cls, api_key: Optional[str] = None, **kwargs) -> dict:
        """Get the quota information for the API key."""
        if not api_key:
            from ...tools.run_tools import AuthManager

            api_key = AuthManager.load_api_key(cls)
        if api_key and cls.models_needs_auth and cls.quota_url is None:
            cls.quota_url = f"{cls.base_url}/models"
        if cls.quota_url is None:
            if cls.backup_url is not None:
                cls.quota_url = f"{cls.backup_url}/chat/completions"
        if cls.quota_url is not None:
            return await super().get_quota(api_key=api_key, **kwargs)
        if not api_key and cls.needs_auth:
            raise MissingAuthError("API key is required.")
        if not cls.default_model:
            raise NotImplementedError("No default model specified.")
        return await cls.test_api_key(api_key)

    @classmethod
    async def test_api_key(cls, api_key: str):
        if api_key in cls._checked_api_keys:
            return cls._checked_api_keys[api_key]
        url = f"{cls.base_url}/chat/completions"
        headers = {"authorization": f"Bearer {api_key}"} if api_key else {}
        json_data = {
            "model": cls.default_model,
            "messages": [{"role": "user", "content": "say only okay"}],
            "max_tokens": 1,
        }
        async with StreamSession() as session:
            async with session.post(url, headers=headers, json=json_data) as response:
                await raise_for_status(response)

View on GitHub (pinned to 973504e177)

Solutions

  1. Pass an api_key argument to the provider call.
  2. Set the provider's API key environment variable before calling.

When it happens

Trigger: Thrown at g4f/Provider/template/OpenaiTemplate.py:52 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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