{"record":{"id":"8c4a6be05e4657fc","repo":"xtekky/gpt4free","slug":"api-key-is-required-for-azure-provider-ask-for-ap","errorCode":null,"errorMessage":"API key is required for Azure provider. Ask for API key in the {cls.login_url} Discord server.","messagePattern":"API key is required for Azure provider\\. Ask for API key in the (.+?) Discord server\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Azure.py","lineNumber":85,"sourceCode":"        api_endpoint: str = None,\n        **kwargs,\n    ) -> AsyncResult:\n        if not model:\n            model = os.environ.get(\"AZURE_DEFAULT_MODEL\", cls.default_model)\n        if model in cls.model_aliases:\n            model = cls.model_aliases[model]\n        if not api_endpoint:\n            if not cls.routes:\n                cls.get_models()\n            api_endpoint = cls.routes.get(model)\n            if cls.routes and not api_endpoint:\n                raise ModelNotFoundError(f\"No API endpoint found for model: {model}\")\n        if not api_endpoint:\n            api_endpoint = os.environ.get(\"AZURE_API_ENDPOINT\")\n        if cls.api_keys:\n            api_key = cls.api_keys.get(model, cls.api_keys.get(\"default\"))\n            if not api_key:\n                raise ValueError(\n                    f\"API key is required for Azure provider. Ask for API key in the {cls.login_url} Discord server.\"\n                )\n        if api_endpoint and \"/images/\" in api_endpoint:\n            prompt = format_media_prompt(messages, kwargs.get(\"prompt\"))\n            width, height = get_width_height(\n                kwargs.get(\"aspect_ratio\", \"1:1\"),\n                kwargs.get(\"width\"),\n                kwargs.get(\"height\"),\n            )\n            output_format = kwargs.get(\"output_format\", \"png\")\n            form = None\n            data = None\n            if media:\n                form = FormData()\n                form.add_field(\"prompt\", prompt)\n                form.add_field(\"width\", str(width))\n                form.add_field(\"height\", str(height))\n                output_format = \"png\"","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Azure.py#L67-L103","documentation":"When AZURE_API_KEYS is configured, the provider resolves the key per model (falling back to the 'default' entry) and raises this ValueError when neither exists. It is the provider's way of saying: endpoint routing worked, but you supplied no credential for that model and Azure OpenAI requires one.","triggerScenarios":"AZURE_API_KEYS='{\"gpt-4o\": \"sk-...\"}' with no 'default' entry, and the caller requests a different model ('gpt-35-turbo'). api_key = cls.api_keys.get(model, cls.api_keys.get('default')) is None -> raise.","commonSituations":"New model added to AZURE_ROUTES but its key forgotten in AZURE_API_KEYS; 'default' fallback omitted; key JSON valid but model-name key mismatched (typos, case).","solutions":["Add a 'default' key entry: AZURE_API_KEYS='{\"default\": \"sk-...\"}' so any routed model has a credential.","Or add a per-model entry whose key exactly matches the model name used in routing.","Restart the process after changing the environment variable — cls.api_keys is cached on the class."],"exampleFix":"# before\nexport AZURE_API_KEYS='{\"gpt-4o\": \"sk-abc\"}'\n# request model=\"gpt-4o-mini\" -> ValueError\n\n# after\nexport AZURE_API_KEYS='{\"default\": \"sk-abc\", \"gpt-4o\": \"sk-abc\"}'","handlingStrategy":"validation","validationCode":"keys = Azure.api_keys or json.loads(os.environ.get(\"AZURE_API_KEYS\", \"{}\"))\nif keys and not (model in keys or \"default\" in keys):\n    raise ValueError(f\"no API key configured for model {model!r}; add it or a 'default' entry\")","typeGuard":"def model_has_api_key(model: str) -> bool:\n    \"\"\"True when the model or a 'default' entry exists in AZURE_API_KEYS.\"\"\"\n    return model in Azure.api_keys or \"default\" in Azure.api_keys","tryCatchPattern":"try:\n    await Azure.create_async_generator(model=model, messages=messages)\nexcept ValueError as e:\n    if \"API key is required\" in str(e):\n        # config gap: add key for the model or a 'default' entry, restart\n        raise","preventionTips":["Always include a 'default' entry in AZURE_API_KEYS as a safety net","Validate at startup that every AZURE_ROUTES key has a matching key or a default","Restart the process after editing key env vars — values are class-cached"],"tags":["configuration","api-key","azure","environment"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}