How to translate text with Azure AI Translator in Python
Automatically translating text is one of the most common tasks in modern applications — from chatbots to e-commerce platforms — and Azure AI Translator solves it with just a few lines of code. This guide shows, step by step, how to translate text with Azure AI Translator in Python, from creating the resource in Azure to your first successful call to the REST API v3.0.
Prerequisites
- An active Azure subscription (the Translator free F0 tier is enough for testing).
- Python 3.9 or later installed on your machine.
- The
requestslibrary, which we install below. - Basic command-line knowledge.
Step 1: Create the Translator resource in the Azure portal
Start by creating the service. In the Azure portal, search for "Translator" and create a new resource. Choose a resource group, a region close to your users (for example, West Europe), and the F0 pricing tier, which is free and ideal for learning. Once the resource is ready, open the Keys and Endpoint section and copy two essential values: the key and the region (the Location/Region field). You will use both in the Step 3 code.
Store the key somewhere safe. Never write it directly in code you share or push it to public repositories.
Step 2: Set up the Python environment
Create a folder for the project and install the only dependency you need, the requests library, which handles the HTTP requests for you:
pip install requests
To avoid leaving the key hard-coded (a poor security practice), store it in an environment variable. On Windows and on macOS/Linux, respectively:
# Windows (PowerShell)
$env:TRANSLATOR_KEY = "YOUR_TRANSLATOR_KEY"
$env:TRANSLATOR_REGION = "westeurope"
# macOS / Linux
export TRANSLATOR_KEY="YOUR_TRANSLATOR_KEY"
export TRANSLATOR_REGION="westeurope"
Step 3: Make your first translation request
Azure AI Translator provides the global endpoint https://api.cognitive.microsofttranslator.com. To translate, you send a POST request to the /translate path, with the api-version=3.0 parameter and the target language in to. The text to translate goes in the request body, in JSON format. The following example translates a sentence from Portuguese (from=pt) to English (to=en):
import os
import requests
key = os.environ["TRANSLATOR_KEY"]
region = os.environ["TRANSLATOR_REGION"]
endpoint = "https://api.cognitive.microsofttranslator.com"
params = {"api-version": "3.0", "from": "pt", "to": ["en"]}
headers = {
"Ocp-Apim-Subscription-Key": key,
"Ocp-Apim-Subscription-Region": region,
"Content-Type": "application/json",
}
body = [{"Text": "Olá, bem-vindo ao Azure AI Translator!"}]
response = requests.post(endpoint + "/translate", params=params, headers=headers, json=body)
result = response.json()
print(result[0]["translations"][0]["text"])
If everything is correct, the console prints Hello, welcome to Azure AI Translator!. The Ocp-Apim-Subscription-Region header is required when you use a regional resource; including it avoids the most common authentication error, 401000.
Step 4: Translate into several languages at once
A major advantage of the API is translating into several languages in a single request: simply provide more than one value in to. You can also omit from so the service automatically detects the source language:
params = {"api-version": "3.0", "to": ["en", "es", "fr"]}
body = [{"Text": "A tradução automática poupa muito tempo."}]
response = requests.post(endpoint + "/translate", params=params, headers=headers, json=body)
for t in response.json()[0]["translations"]:
print(t["to"], "->", t["text"])
The service returns one translation for each requested language and, when you do not provide from, it adds a detectedLanguage field with the identified language and its confidence score.
Verify the result
To confirm everything works, check three signals. First, the response status code should be 200 (you can print response.status_code). Second, the returned JSON contains a translations list with the already-translated text. Third, when you omit from, the detectedLanguage field also appears. If you get a 401 or 403 error, review the key and the region; if it is a 400, check the body format — it must be a list of objects, each with the Text property.
Conclusion
With one Azure resource and fewer than twenty lines of Python, you can already translate text into dozens of languages through Azure AI Translator. From here, try integrating the function into a web API with Flask, translating files in batch, or training your own glossary with Custom Translator to respect your company's terminology. What will be the first language you translate your content into?