Formerly known as Azure AI Services or Azure Cognitive Services is a unified collection of prebuilt AI capabilities within the Microsoft Foundry platform
Hello Lisa MM,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand you’re facing a 401 Unauthorized error when accessing your Azure AI Foundry API, despite your agent working in the Playground.
This generally indicates that while your project key and agent are valid for internal testing, external API access may not be enabled on your subscription, and it's a common scenario with Azure OpenAI’s limited access model.
You should first verify that your subscription has been approved for external API access by checking your Azure OpenAI resource settings and ensuring you have the necessary IAM roles (Owner, Contributor, or Cognitive Services Contributor) as detailed in the Azure Access Request documentation - https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/request-access and role-based access control guidelines - https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles Next, confirm that you are using the correct API endpoint and header format; for example, your endpoint should follow a structure similar to:
curl https://<your-resource-name>.openai.azure.com/openai/deployments/<deployment-name>/chat/completions?api-version=2024-02-15-preview \
-H "Content-Type: application/json" \
-H "api-key: <your_api_key>" \
-d '{"messages": [{"role": "user", "content": "Hello"}], "max_tokens": 10}'
Avoid using "Authorization": "Bearer <key>" unless explicitly required by the API. Testing your setup with tools such as Postman or curl can help isolate whether the issue stems from network restrictions, misconfigured endpoints, or key inaccuracies. If all configuration steps are verified and the error persists, reaching out to Azure support may be necessary to ensure external API access is indeed enabled for your subscription. This approach not only rectifies the 401 error by addressing key setup and endpoint validation but also aligns with best practices to mitigate potential intermittent DNS or connector issues.
I hope this is helpful! Do not hesitate to let me know if you have any other questions or clarifications.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.