7.2 SDK and REST API Patterns
Key Takeaways
- All Azure AI SDKs follow a consistent pattern: create a client with endpoint + credential, call a method, process the response.
- REST API authentication uses the Ocp-Apim-Subscription-Key header for key-based auth or the Authorization: Bearer header for Entra ID tokens.
- Long-running operations (custom model training, batch processing) return a poller object — call poller.result() to wait for completion.
- Error handling should check for specific error codes: 401 (authentication), 403 (authorization), 429 (rate limit), and content_filter (safety).
- The Azure AI Translator REST API uniquely requires the Ocp-Apim-Subscription-Region header in addition to the subscription key.
SDK and REST API Patterns
Quick Answer: Azure AI SDKs follow a consistent pattern: create client → call method → process response. REST APIs use Ocp-Apim-Subscription-Key header. Long-running operations use pollers. Handle 401 (auth), 429 (rate limit), and content_filter errors.
Universal SDK Pattern
Every Azure AI service SDK follows the same structure:
# 1. Import the client and credential classes
from azure.ai.<service> import <ServiceClient>
from azure.core.credentials import AzureKeyCredential
# OR for Entra ID:
from azure.identity import DefaultAzureCredential
# 2. Create the client
client = <ServiceClient>(
endpoint="https://<resource>.cognitiveservices.azure.com/",
credential=AzureKeyCredential("<key>")
# OR credential=DefaultAzureCredential()
)
# 3. Call a method
result = client.<method>(input_data)
# 4. Process the response
for item in result:
print(item.property)
REST API Pattern
POST https://<resource>.cognitiveservices.azure.com/<service-path>?api-version=<version>
Headers:
Ocp-Apim-Subscription-Key: <your-key>
Content-Type: application/json
Body:
{
// Service-specific request body
}
Service-Specific Endpoint Patterns
| Service | Endpoint Pattern |
|---|---|
| AI Vision | https://<resource>.cognitiveservices.azure.com/computervision/... |
| AI Language | https://<resource>.cognitiveservices.azure.com/language/... |
| AI Speech | https://<region>.api.cognitive.microsoft.com/sts/v1.0/... |
| AI Translator | https://api.cognitive.microsofttranslator.com/... |
| Document Intelligence | https://<resource>.cognitiveservices.azure.com/documentintelligence/... |
| Content Safety | https://<resource>.cognitiveservices.azure.com/contentsafety/... |
| AI Search | https://<search-service>.search.windows.net/... |
| Azure OpenAI | https://<resource>.openai.azure.com/openai/... |
On the Exam: Note that Translator uses a different base URL (api.cognitive.microsofttranslator.com) than other services. AI Search uses a different domain (search.windows.net). Azure OpenAI uses (openai.azure.com). Knowing the correct endpoint pattern is tested.
Long-Running Operations
Operations like model training and batch processing are asynchronous:
# SDK pattern — returns a poller
poller = client.begin_analyze_document(model_id="prebuilt-invoice", body=file)
result = poller.result() # Blocks until complete
# REST pattern — returns an operation URL
# POST returns 202 Accepted with Operation-Location header
# Poll the operation URL until status is "succeeded"
operation_url = response.headers["Operation-Location"]
Error Handling
from azure.core.exceptions import (
HttpResponseError,
ClientAuthenticationError,
ResourceNotFoundError
)
try:
result = client.analyze_text(request)
except ClientAuthenticationError:
print("401: Invalid key or unauthorized")
except ResourceNotFoundError:
print("404: Resource or endpoint not found")
except HttpResponseError as e:
if e.status_code == 429:
print("429: Rate limit exceeded — retry after backoff")
elif "content_filter" in str(e):
print("Content blocked by safety filter")
else:
print(f"Error {e.status_code}: {e.message}")
Common HTTP Status Codes
| Code | Meaning | Action |
|---|---|---|
| 200 | Success | Process response |
| 202 | Accepted (async) | Poll operation URL |
| 400 | Bad request | Fix request payload |
| 401 | Unauthorized | Check API key or token |
| 403 | Forbidden | Check RBAC permissions |
| 404 | Not found | Check endpoint URL |
| 429 | Rate limited | Implement exponential backoff retry |
| 500 | Internal error | Retry with backoff |
Which HTTP header is used for API key authentication with Azure AI services?
An Azure AI service API call returns HTTP 429. What does this mean and what should you do?
Which Azure AI service uses a different base URL (api.cognitive.microsofttranslator.com) than other Azure AI services?