Basic Chat with Azure AI Foundry - End-to-End Example
July 2, 2026 · View on GitHub
This example is a simple Spring Boot application that connects to an Azure AI Foundry model using keyless authentication (Microsoft Entra ID) and tests your setup. It uses Spring AI's ChatClient.
Table of Contents
- Prerequisites
- Quick Start
- How Authentication Works
- Running the Application
- Configuration Reference
- Troubleshooting
- Next Steps
- Resources
Prerequisites
Before running this example, ensure you have:
- An Azure AI Foundry resource with a
gpt-4o-minideployment — provision it withazd upor manually via the Azure AI Foundry setup guide - The Cognitive Services OpenAI User role on that resource (the Bicep templates assign this for you)
- The Azure CLI (
az), signed in withaz login - Java 21+ and Maven 3.9+
No API key required — authentication is keyless via Microsoft Entra ID.
Quick Start
# 1. Navigate to project
cd 02-SetupDevEnvironment/examples/basic-chat-azure
# 2. Sign in so keyless auth can get a token
az login
# 3. Configure the endpoint
# - If you ran `azd up`, .env was written for you (skip this).
# - Otherwise copy the template and set AZURE_OPENAI_ENDPOINT:
cp .env.example .env
# 4. Run the application
mvn spring-boot:run
How Authentication Works
This example authenticates with Microsoft Entra ID — there is no API key.
When only spring.ai.azure.openai.endpoint is set (and no api-key), Spring AI builds the Azure OpenAI client with DefaultAzureCredential. That credential automatically finds a token from your az login session locally, or from a managed identity when running in Azure — so the same code works in both places with no changes.
Running the Application
Using Maven
mvn spring-boot:run
Using VS Code
- Open the project in VS Code
- Press
F5or use the "Run and Debug" panel - Select "Spring Boot-BasicChatApplication" configuration
Note: The VS Code configuration automatically loads your .env file
Expected Output
Starting Basic Chat with Azure OpenAI...
Environment variables loaded successfully
Connecting to Azure OpenAI...
Sending prompt: What is AI in a short sentence? Max 100 words.
AI Response:
================
AI, or Artificial Intelligence, is the simulation of human intelligence in machines programmed to think and learn like humans.
================
Success! Azure OpenAI connection is working correctly.
Configuration Reference
Environment Variables
| Variable | Description | Required | Example |
|---|---|---|---|
AZURE_OPENAI_ENDPOINT | Foundry (Azure OpenAI) endpoint URL | Yes | https://my-resource.openai.azure.com/ |
AZURE_OPENAI_DEPLOYMENT | Chat model deployment name | No | gpt-4o-mini (default) |
There is no API key variable — authentication is keyless (Microsoft Entra ID via
az login).
Spring Configuration
The application.yml file configures:
- Endpoint:
${AZURE_OPENAI_ENDPOINT}- From environment variable - Deployment:
${AZURE_OPENAI_DEPLOYMENT:gpt-4o-mini}- From environment variable with fallback - Auth: keyless — no
api-keyis set, so Spring AI usesDefaultAzureCredential - Temperature:
0.7- Controls creativity (0.0 = deterministic, 1.0 = creative) - Max Tokens:
500- Maximum response length
Troubleshooting
Common Issues
Error: 401 / "PermissionDenied" / token errors
- Run
az login— keyless auth needs an active sign-in to get a token - Verify your account has the Cognitive Services OpenAI User role on the resource
- If you just assigned the role, wait a minute for it to propagate
- Confirm you're in the right tenant/subscription (
az account show)
Error: "The endpoint is not valid" / connection errors
- Ensure
AZURE_OPENAI_ENDPOINTis the full base URL (e.g.,https://your-resource.openai.azure.com/) - Check for trailing slash consistency
- Verify the endpoint matches your provisioned resource (
azd env get-values)
Error: "The deployment was not found"
- Verify
AZURE_OPENAI_DEPLOYMENTmatches a deployment name in Azure - Check that the model is successfully deployed and active
- The default deployment name is
gpt-4o-mini
VS Code: Environment variables not loading
- Ensure your
.envfile is in the project root directory (same level aspom.xml) - Try running
mvn spring-boot:runin VS Code's integrated terminal - Check that the VS Code Java extension is properly installed
Debug Mode
To enable detailed logging, uncomment these lines in application.yml:
logging:
level:
org.springframework.ai: DEBUG
com.azure: DEBUG
Next Steps
Setup Complete! Continue your learning journey:
Chapter 3: Core Generative AI Techniques