Getting started overview
Getting started with Azure AI Services, formerly known as Microsoft Cognitive Services, involves a sequence of steps that begin with setting up an Azure account. Once an account is established, developers provision a specific Azure AI Service resource, such as Azure AI Vision or Azure AI Speech. This provisioning process generates the necessary API keys and an endpoint URL, which are critical for authenticating and routing API requests to the chosen service. After obtaining these credentials, developers can make their first API call using one of the available SDKs or by directly sending HTTP requests to the service endpoint. The process is designed to integrate with existing Azure infrastructure, making it suitable for developers already familiar with the Microsoft ecosystem.
Azure AI Services offer a range of capabilities, from natural language processing to computer vision, each with its own set of APIs and usage patterns. The comprehensive Azure AI Services documentation provides detailed guides for each specific service, including language-specific SDK examples for Python, C#, Java, JavaScript, and Go.
The following table outlines the foundational steps to begin interacting with Azure AI Services:
| Step | What to do | Where |
|---|---|---|
| 1. Create Azure account | Sign up for a free Azure account or sign in with an existing one. | Azure Free Account page |
| 2. Provision resource | Create a new Azure AI Services resource (e.g., Azure AI Vision, Azure AI Language) in the Azure portal. | Azure portal: Create resource |
| 3. Retrieve API keys | Locate and copy the subscription keys and endpoint for your newly created resource from the Azure portal. | Azure portal: Resource Keys and Endpoint |
| 4. Configure environment | Set up your development environment, install necessary SDKs or client libraries, and configure environment variables. | Azure AI Services client library installation |
| 5. Make first request | Execute a sample API call using an SDK or direct HTTP request with your obtained keys and endpoint. | Azure AI Services authentication guide |
Create an account and get keys
To access Azure AI Services, you must first have an Azure account. Microsoft offers a free Azure account, which includes credits and free access to certain services for 12 months, along with perpetual free access to other services up to specific limits. After signing up or logging in, navigate to the Azure portal.
Provisioning an Azure AI Service resource
Within the Azure portal, you will create an Azure AI Services resource. This resource acts as your entry point to the desired AI capabilities. Follow these steps:
- Log in to the Azure portal.
- In the search bar, type "Azure AI services" and select "Create Azure AI services" from the results, or go directly to the Create Cognitive Services resource page.
- Select the specific service you wish to use, such as "Azure AI Vision" or "Language Service", and then click "Create".
- Fill in the required details:
- Subscription: Choose your Azure subscription.
- Resource Group: Create a new one or select an existing resource group. Resource groups logically organize Azure resources.
- Region: Select a geographical region where your service will be hosted. This impacts latency and data residency.
- Name: Provide a unique name for your service instance.
- Pricing Tier: Select a pricing tier. Many services offer a "F0" (Free) tier for evaluation purposes, or standard tiers with pay-as-you-go pricing. Review the Azure AI Services pricing details for more information.
- Review and create the resource. Deployment usually takes a few moments.
Retrieving API keys and endpoint
Once your Azure AI Service resource is deployed, you need to retrieve the API keys and endpoint, which are essential for authentication and communication with the service. These are found in the resource's management blade:
- Navigate to your newly created Azure AI Service resource in the Azure portal.
- In the left-hand navigation pane, under "Resource Management", select "Keys and Endpoint".
- You will see two API keys (Key 1 and Key 2) and the Endpoint URL. Copy one of the keys and the endpoint URL. It is recommended to store these securely, for example, as environment variables, rather than hardcoding them directly into your application.
Your first request
This section demonstrates how to make a basic API request to an Azure AI Service using Python, specifically targeting the Azure AI Vision service for image analysis. This example assumes you have Python and pip installed.
Prerequisites
- Python 3.7 or newer.
- An Azure AI Vision resource provisioned in your Azure account.
- The API key and endpoint URL for your Azure AI Vision resource.
Install the client library
First, install the Azure AI Vision client library for Python:
pip install azure-ai-vision
Example: Analyze an image with Azure AI Vision
This Python script sends an image URL to the Azure AI Vision service and prints the detected features (like descriptions and tags). Replace YOUR_VISION_ENDPOINT and YOUR_VISION_KEY with your actual credentials.
import os
from azure.ai.vision.imageanalysis import ImageAnalysisClient
from azure.ai.vision.imageanalysis.models import VisualFeatures
from azure.core.credentials import AzureKeyCredential
# --- Configuration ---
# Set your Azure AI Vision endpoint and key as environment variables
# export VISION_ENDPOINT="YOUR_VISION_ENDPOINT"
# export VISION_KEY="YOUR_VISION_KEY"
# Alternatively, uncomment and set them directly (NOT recommended for production)
# vision_endpoint = "https://YOUR_VISION_RESOURCE_NAME.cognitiveservices.azure.com/"
# vision_key = "YOUR_VISION_API_KEY"
vision_endpoint = os.environ.get("VISION_ENDPOINT")
vision_key = os.environ.get("VISION_KEY")
if not vision_endpoint or not vision_key:
raise ValueError("Please set the VISION_ENDPOINT and VISION_KEY environment variables.")
# Initialize the client
client = ImageAnalysisClient(
endpoint=vision_endpoint,
credential=AzureKeyCredential(vision_key)
)
# Image to analyze (replace with your desired image URL or local path)
image_url = "https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png"
print(f"Analyzing image from URL: {image_url}")
try:
# Analyze the image for various visual features
result = client.analyze(
image_url=image_url,
visual_features=[VisualFeatures.CAPTION, VisualFeatures.TAGS, VisualFeatures.OBJECTS]
)
print("Image analysis results:")
if result.caption is not None:
print(f" Caption: '{result.caption.text}' (confidence: {result.caption.confidence:.2f})")
if result.tags is not None:
print(" Tags:")
for tag in result.tags.list:
print(f" - '{tag.name}' (confidence: {tag.confidence:.2f})")
if result.objects is not None:
print(" Objects:")
for obj in result.objects.list:
print(f" - '{obj.name}' (confidence: {obj.confidence:.2f}) at bounding box {obj.bounding_box}")
except Exception as e:
print(f"An error occurred: {e}")
Running the script
- Save the code as a
.pyfile (e.g.,vision_quickstart.py). - Set your environment variables
VISION_ENDPOINTandVISION_KEYin your terminal before running the script. On Linux/macOS:
On Windows (Command Prompt):export VISION_ENDPOINT="https://YOUR_VISION_RESOURCE_NAME.cognitiveservices.azure.com/" export VISION_KEY="YOUR_VISION_API_KEY"
On Windows (PowerShell):set VISION_ENDPOINT="https://YOUR_VISION_RESOURCE_NAME.cognitiveservices.azure.com/" set VISION_KEY="YOUR_VISION_API_KEY"$env:VISION_ENDPOINT="https://YOUR_VISION_RESOURCE_NAME.cognitiveservices.azure.com/" $env:VISION_KEY="YOUR_VISION_API_KEY" - Execute the script:
python vision_quickstart.py
The output will show the detected caption, tags, and objects from the specified image, demonstrating a successful interaction with the Azure AI Vision API.
Common next steps
After successfully making your first request, several common next steps can help you further integrate and optimize your use of Azure AI Services:
- Explore other services: Azure AI Services encompass a broad range of capabilities. Explore other services like Azure AI Language for text analytics or Azure AI Speech for speech-to-text and text-to-speech functionalities. Each service has its own specific APIs and use cases.
- Implement secure credential management: For production environments, avoid storing API keys directly in code or environment variables. Utilize Azure Key Vault to securely store and manage your keys and secrets. This practice enhances security and simplifies key rotation. Microsoft provides documentation on Azure Key Vault for secure credential management.
- Understand pricing and monitoring: Familiarize yourself with the pricing model for the services you are using to manage costs effectively. Set up monitoring and alerts in the Azure portal to track API usage, performance, and potential issues.
- Error handling and retry logic: Implement robust error handling in your application to gracefully manage API errors, such as rate limiting (HTTP 429), authentication failures (HTTP 401), or invalid requests (HTTP 400). Incorporate retry logic with exponential backoff for transient errors to improve application resilience. The Azure Architecture Center provides guidance on retry patterns.
- Integrate with other Azure services: Leverage other Azure services like Azure Functions for serverless execution of your AI calls, Azure Logic Apps for workflow automation, or Azure Storage for storing input/output data. This allows for building scalable and robust AI-powered applications.
- Review service limits and quotas: Each Azure AI Service has specific service limits and quotas for requests per second, data size, and other parameters. Be aware of these limits to design your application appropriately and prevent throttling.
Troubleshooting the first call
When making your first API call to Azure AI Services, you might encounter issues. Here are common problems and their solutions:
-
Authentication errors (HTTP 401, 403): Incorrect API Key or Endpoint
- Issue: The most frequent error is an invalid or missing API key or endpoint.
- Solution: Double-check that you have copied the correct API key (Key 1 or Key 2) and the exact endpoint URL from the "Keys and Endpoint" section of your Azure AI Service resource in the Azure portal. Ensure there are no leading or trailing spaces. The API key must be sent in the
Ocp-Apim-Subscription-Keyheader for most services or as anAuthorization: Bearertoken for others, like Azure OpenAI. Always refer to the Azure AI Services authentication documentation for the specific service you are using.
-
Resource Not Found (HTTP 404): Incorrect Endpoint URL or Resource Name
- Issue: The service cannot be reached, often due to a typo in the endpoint or an incorrect resource name in the URL.
- Solution: Verify that the endpoint URL exactly matches the one provided in the Azure portal. Ensure the resource name embedded in the URL (e.g.,
https://YOUR_VISION_RESOURCE_NAME.cognitiveservices.azure.com/) is correct.
-
Bad Request (HTTP 400): Malformed Request Body or Parameters
- Issue: The API request body is incorrectly formatted (e.g., invalid JSON, missing required fields) or parameters are outside expected ranges.
- Solution: Consult the API reference documentation for the specific service and operation you are calling. Ensure your request body adheres to the expected schema and all required parameters are present and correctly typed. For example, image URLs must be accessible by the service.
-
Too Many Requests (HTTP 429): Rate Limiting Exceeded
- Issue: You are sending requests faster than the service's allowed rate limit, especially common on free or low-tier subscriptions.
- Solution: Implement retry logic with exponential backoff. Wait for a period before retrying the request. If this persists, consider upgrading your pricing tier or distributing your workload across multiple resources. Rate limiting is a standard practice for API providers to manage system load.
-
Service Unavailable (HTTP 503) or Internal Server Error (HTTP 500)
- Issue: These typically indicate a temporary issue on the Azure service side.
- Solution: Implement retry logic. If the problem persists, check the Azure Service Health dashboard for outages or degraded performance in your region.
-
SDK Installation Issues
- Issue: Problems installing or importing the Python (or other language) client library.
- Solution: Ensure your Python environment is correctly set up. Use a virtual environment to avoid dependency conflicts. Verify your
pipversion is up-to-date (python -m pip install --upgrade pip). For other SDKs, refer to their specific installation instructions in the Azure AI Services documentation.