Prompt
How do I translate text between languages with an API?
Latest observation
Nov 2, 2026
OpenAI APIWeb search: off
You can translate text with an API by sending your text and target language to a translation service endpoint, then reading the translated text from the response.
Typical flow
-
Choose a translation API
- Google Cloud Translate
- DeepL
- Microsoft Translator
- Amazon Translate
- Open-source/self-hosted options like LibreTranslate
-
Get credentials
- Usually an API key, OAuth token, or service account.
-
Make a request
- Send:
- source text
- source language (optional if auto-detect is supported)
- target language
- authentication headers
- Send:
-
Parse the response
- Extract the translated text.
Example with a generic REST API
Request
curl -X POST https://api.example.com/translate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello, world!",
"source ভাষા": "en",
"target_language": "es"
}'
Response
{
"translated_text": "¡Hola, mundo!"
}
Python example
import requests
url = "https://api.example.com/translate"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"text": "How are you?",
"source_language": "en",
"target_language": "fr"
}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
data = response.json()
print(data["translated_text"])
JavaScript example
fetch("https://api.example.com/translate", {
method: "POST",
headers: {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
},
body: JSON.stringify({
text: "Good morning",
source_language: "en",
target_language: "de"
})
})
.then(res => res.json())
.then(data => console.log(data.translated_text));
Important notes
- Encoding: Use UTF-8 for non-English text.
- Language codes: APIs usually use ISO codes like
en,es,fr,de,ja,zh. - Auto-detection: Many APIs can detect the source language automatically.
- Batch translation: Some APIs let you translate multiple strings at once.
- Rate limits: Check quotas and request limits.
- Formatting: Preserve HTML/Markdown if the API supports it, otherwise plain text may be safer.
If you want a real provider example
Tell me which API you want to use, and I can give you a ready-to-run example for:
- Google Translate
- DeepL
- Microsoft Translator
- LibreTranslate
- OpenAI-style API integration