Hello Hanif Farahnak,
Thank you for posting your question in the Microsoft Q&A forum.
To authenticate with Azure AI Foundry (AI Studio) from a production server outside Azure without using az login, you must properly configure your Service Principal (SP) and SDK initialization. First, ensure your SP has the necessary permissions by assigning it the Cognitive Services User and AI Developer roles at the project or resource group level, rather than relying on broad roles like Contributor. Configure your environment with the required variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, and AZURE_SUBSCRIPTION_ID, which the DefaultAzureCredential() will automatically use for authentication.
The endpoint must be project-specific, obtained from AI Studio --> Project Settings --> API Access, following the format https://<project-name>.api.<region>.cognitive.microsoft.com. Avoid using generic OpenAI endpoints, as they won’t work with AI Foundry resources. Initialize the AIClient in Python with this endpoint and DefaultAzureCredential(), ensuring the SDK can authenticate without CLI dependencies.
If issues persist, verify the SP’s validity and permissions using az ad sp show, and enable debug logging to capture authentication errors.
If the above answer helped, please do not forget to "Accept Answer" as this may help other community members to refer the info if facing a similar issue. Your contribution to the Microsoft Q&A community is highly appreciated.