In this comprehensive article, I’ll guide you through multiple methods for obtaining your Azure OpenAI key, sharing insights from real-world implementations. This guide will provide you with the complete roadmap to access Azure OpenAI Key.
Table of Contents
How to Get Azure OpenAI Key
Before diving into the key acquisition process, let me explain the types of Azure OpenAI Keys.
Types of Azure OpenAI Keys
| Key Type | Use Case | Security Level | Best For |
|---|---|---|---|
| API Key | Direct API access | Medium | Development, testing |
| Azure AD Token | Enterprise authentication | High | Production environments |
| Managed Identity | Azure-native services | Highest | Azure-hosted applications |
| Service Principal | Automated workflows | High | CI/CD pipelines |
Method 1: Using Azure Portal
This is the most straightforward method I use when onboarding new clients to Azure OpenAI services.
Step 1: Create an Azure OpenAI Resource
Step 2: Retrieve Your API Keys
After resource creation, here’s how I extract the API keys:
Key Retrieval Steps:
- Navigate to your newly created Azure OpenAI resource
- In the left sidebar, click “Keys and Endpoint”
- You’ll see two keys displayed:
- Key 1: Primary API key
- Key 2: Secondary API key (for key rotation)
- Copy either key for your applications using the copy buttons next to the respective keys. Refer to the screenshot below for the complete steps.

Security Best Practice I Always Implement:
{
"recommendation": "Use Key 1 for production, Key 2 for development",
"rotation_strategy": "Rotate keys quarterly for security",
"storage": "Store keys in Azure Key Vault, never in code"
}
Method 2: Using Azure CLI
We can execute the Azure CLI script below to retrieve the primary key.
# Get the primary key
az cognitiveservices account keys list \
--name $OPENAI_NAME \
--resource-group $RESOURCE_GROUP \
--query "key1" \
--output tsvaz cognitiveservices account keys list --name 'azurelessons' --resource-group 'myresgrp' --query "key1" --output tsvAfter executing the above query, I got the expected output as shown in the screenshot below.

If we need to get both keys in JSON format, we can execute the following Azure CLI command.
# Get both keys in JSON format
az cognitiveservices account keys list \
--name $OPENAI_NAME \
--resource-group $RESOURCE_GROUPaz cognitiveservices account keys list --name 'azurelessons' --resource-group 'myresgrp' --output tsvAfter executing the above query, I got the expected output as shown in the screenshot below.

Method 3: Using PowerShell
We can also use the PowerShell script for this purpose.
PowerShell Azure Module Setup
# Install Azure PowerShell module
Install-Module -Name Az -Repository PSGallery -Force
# Connect to Azure
Connect-AzAccount
# Set subscription context
Set-AzContext -SubscriptionId "your-subscription-id"
Retrieving Keys
# Define variables
$resourceGroupName = "rg-openai-production"
$location = "East US"
$openAIName = "mycompany-openai-prod"
# Retrieve API keys
$keys = Get-AzCognitiveServicesAccountKey -ResourceGroupName $resourceGroupName -Name $openAIName
Write-Output "Primary Key: $($keys.Key1)"
Write-Output "Secondary Key: $($keys.Key2)"# Define variables
$resourceGroupName = "myresgrp"
$location = "East US"
$openAIName = "azurelessons"
# Retrieve API keys
$keys = Get-AzCognitiveServicesAccountKey -ResourceGroupName $resourceGroupName -Name $openAIName
Write-Output "Primary Key: $($keys.Key1)"
Write-Output "Secondary Key: $($keys.Key2)"After executing the above query, I got the expected output as shown in the screenshot below.

Method 4: Using ARM Templates for Enterprise Deployments
For large-scale enterprise deployments, I utilize ARM (Azure Resource Manager) templates to ensure consistent and compliant deployments across multiple regions.
ARM Template for Azure OpenAI
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"openAIName": {
"type": "string",
"defaultValue": "mycompany-openai"
},
"location": {
"type": "string",
"defaultValue": "East US",
"allowedValues": [
"East US",
"South Central US",
"West Europe"
]
},
"sku": {
"type": "string",
"defaultValue": "S0"
}
},
"resources": [
{
"type": "Microsoft.CognitiveServices/accounts",
"apiVersion": "2023-05-01",
"name": "[parameters('openAIName')]",
"location": "[parameters('location')]",
"kind": "OpenAI",
"properties": {
"customSubDomainName": "[parameters('openAIName')]",
"publicNetworkAccess": "Enabled"
},
"sku": {
"name": "[parameters('sku')]"
}
}
],
"outputs": {
"endpoint": {
"type": "string",
"value": "[reference(parameters('openAIName')).endpoint]"
},
"resourceId": {
"type": "string",
"value": "[resourceId('Microsoft.CognitiveServices/accounts', parameters('openAIName'))]"
}
}
}
Deploying ARM Template
# Deploy using Azure CLI
az deployment group create \
--resource-group "rg-openai-production" \
--template-file "openai-template.json" \
--parameters openAIName="mycompany-openai-prod" location="East US"Video Tutorial
Conclusion
Getting Azure OpenAI Key is crucial for a developer. The methods and best practices I’ve shared in this comprehensive article represent the most tested approaches refined through real-world deployments.
Key Acquisition Method Summary:
| Method | Best For | Security Level | Ongoing Maintenance |
|---|---|---|---|
| Azure Portal | First-time users, small teams | Good | Low |
| Azure CLI | DevOps automation | High | Medium |
| PowerShell | Windows enterprises | High | Medium |
| ARM Templates | Enterprise scale | Highest | Automated |
My Professional Recommendations:
For individual developers and small businesses starting their AI journey, the Azure Portal method provides the most straightforward path to obtaining your first API key.
For enterprise organizations with existing Azure infrastructure, implement the Azure CLI or PowerShell approach from the beginning. This ensures consistency with your current DevOps practices and enables proper key rotation and security management.
You may also like the following articles.
- Azure OpenAI Key vs OpenAI Key
- What is a Token in Azure OpenAI
- Azure OpenAI Service Features
- Azure Copilot vs GitHub Copilot

I am Rajkishore, and I am a Microsoft Certified IT Consultant. I have over 14 years of experience in Microsoft Azure and AWS, with good experience in Azure Functions, Storage, Virtual Machines, Logic Apps, PowerShell Commands, CLI Commands, Machine Learning, AI, Azure Cognitive Services, DevOps, etc. Not only that, I do have good real-time experience in designing and developing cloud-native data integrations on Azure or AWS, etc. I hope you will learn from these practical Azure tutorials. Read more.
