How to Get Azure OpenAI Key

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.

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 TypeUse CaseSecurity LevelBest For
API KeyDirect API accessMediumDevelopment, testing
Azure AD TokenEnterprise authenticationHighProduction environments
Managed IdentityAzure-native servicesHighestAzure-hosted applications
Service PrincipalAutomated workflowsHighCI/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:

  1. Navigate to your newly created Azure OpenAI resource
  2. In the left sidebar, click “Keys and Endpoint”
  3. You’ll see two keys displayed:
    • Key 1: Primary API key
    • Key 2: Secondary API key (for key rotation)
  4. Copy either key for your applications using the copy buttons next to the respective keys. Refer to the screenshot below for the complete steps.
How to Get Azure OpenAI Key

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 tsv
az cognitiveservices account keys list --name 'azurelessons' --resource-group 'myresgrp' --query "key1" --output tsv

After executing the above query, I got the expected output as shown in the screenshot below.

how to find azure open ai key

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_GROUP
az cognitiveservices account keys list --name 'azurelessons' --resource-group 'myresgrp' --output tsv

After executing the above query, I got the expected output as shown in the screenshot below.

how to get azure openai api key

Method 3: Using PowerShell

We can also use the PowerShell script for this purpose.