How to fix 401 Unauthorized error when using Azure AI Translator API?

Anushri Deshmukh - Solace 5 Reputation points
2025-09-16T10:40:51.9966667+00:00

HI @VasaviLankipalle-MSFT I am using the ‘AzureATranslator’ API for text translation. After a few days of usage, I am getting this error:

StatusCode: 401, ReasonPhrase: 'Unauthorized'

I tried the following steps to get it to work but still getting the 'Unauthorized' error.

  1. I initially used Key 1 for text translation. I changed it to Key 2. Keys Regenerated both the keys.  

endpoint = "https://api.cognitive.microsofttranslator.com/"

headers = {

"Ocp-Apim-Subscription-Key": "-------",

"Ocp-Apim-Subscription-Region": "-----"

}

Issue: Even after updating and regenerating the keys, I still get a 401 Unauthorized error when calling the API.

Request: Please help me identify why my API requests are being rejected and guide me on how to restore proper access.

The pricing tier I am currently working on is 'F0 Free'

Azure AI Translator
Azure AI Translator
An Azure service to easily conduct machine translation with a simple REST API call.
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Aryan Parashar 1,850 Reputation points Microsoft External Staff Moderator
    2025-09-17T07:48:58.65+00:00

    Hi Anushree,

    I tried the Translator API, and it worked fine for me. The documentation I used is:
    https://free.blessedness.top/en-us/azure/ai-services/translator/text-translation/quickstart/rest-api?tabs=python#translate-text

    Below is the code. Please paste your API key and try running it to ensure that your resource is working correctly:

    import requests, uuid, json
    
    key = "<API-KEY>"
    endpoint = "https://api.cognitive.microsofttranslator.com"
    
    location = "eastus"
    
    path = '/translate'
    constructed_url = endpoint + path
    
    params = {
        'api-version': '3.0',
        'from': 'en',
        'to': ['fr', 'zu']
    }
    
    headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Ocp-Apim-Subscription-Region': location,
        'Content-type': 'application/json',
        'X-ClientTraceId': str(uuid.uuid4())
    }
    
    body = [{
        'text': 'I would really like to drive your car around the block a few times!'
    }]
    
    request = requests.post(constructed_url, params=params, headers=headers, json=body)
    response = request.json()
    
    print(json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))
    

    Apart from the API key, please also verify:

    Request headers

    Region

    Feel free to accept it as an answer.
    Thankyou for reaching out to the Microsoft QNA Portal.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.