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.

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.

how to get openai api key free

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:

MethodBest ForSecurity LevelOngoing Maintenance
Azure PortalFirst-time users, small teamsGoodLow
Azure CLIDevOps automationHighMedium
PowerShellWindows enterprisesHighMedium
ARM TemplatesEnterprise scaleHighestAutomated

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 Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!