Code Examples
Practical code examples for using the TranslatePlus API in various programming languages.
Python
Basic Translation
import requests
url = "https://api.translateplus.io/v2/translate"
headers = {
"X-API-KEY": "your_api_key",
"Content-Type": "application/json"
}
data = {
"text": "Hello, world!",
"source": "en",
"target": "fr"
}
response = requests.post(url, json=data, headers=headers)
result = response.json()
print(result["translations"]["translation"])Batch Translation
import requests
url = "https://api.translateplus.io/v2/translate/batch"
headers = {
"X-API-KEY": "your_api_key",
"Content-Type": "application/json"
}
data = {
"texts": ["Hello", "World", "Translate"],
"source": "en",
"target": "fr"
}
response = requests.post(url, json=data, headers=headers)
results = response.json()
for translation in results["translations"]:
print(f"{translation['text']} → {translation['translation']}")JavaScript
Basic Translation
async function translateText(text, source, target) {
const response = await fetch('https://api.translateplus.io/v2/translate', {
method: 'POST',
headers: {
'X-API-KEY': 'your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text,
source,
target
})
});
const data = await response.json();
return data.translations.translation;
}
// Usage
translateText('Hello, world!', 'en', 'fr')
.then(translation => console.log(translation));Error Handling
async function translateWithErrorHandling(text, source, target) {
try {
const response = await fetch('https://api.translateplus.io/v2/translate', {
method: 'POST',
headers: {
'X-API-KEY': 'your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ text, source, target })
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail);
}
const data = await response.json();
return data.translations.translation;
} catch (error) {
console.error('Translation error:', error.message);
throw error;
}
}Node.js
Using Axios
const axios = require('axios');
async function translate(text, source, target) {
try {
const response = await axios.post(
'https://api.translateplus.io/v2/translate',
{
text,
source,
target
},
{
headers: {
'X-API-KEY': 'your_api_key',
'Content-Type': 'application/json'
}
}
);
return response.data.translations.translation;
} catch (error) {
if (error.response) {
console.error('API Error:', error.response.data);
} else {
console.error('Request Error:', error.message);
}
throw error;
}
}cURL
Basic Request
curl https://api.translateplus.io/v2/translate \
-H "X-API-KEY: your_api_key" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello, world!",
"source": "en",
"target": "fr"
}'With Error Handling
response=$(curl -s -w "\n%{http_code}" \
-H "X-API-KEY: your_api_key" \
-H "Content-Type: application/json" \
-d '{"text":"Hello","source":"en","target":"fr"}' \
https://api.translateplus.io/v2/translate)
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq 200 ]; then
echo "$body" | jq '.translations.translation'
else
echo "Error: $http_code"
echo "$body" | jq '.detail'
fi