Aller au contenu

Utilisation de l'API

Les exemples ci-dessous utilisent Javascript, mais vous pouvez utiliser n’importe quel langage de programmation. Des bindings pour de nombreux langages de programmation sont également disponibles.

Requête :

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: "Hello!",
source: "en",
target: "es",
api_key: "xxxxxx" // peut être optionnel avec l'auto-hébergé
}),
headers: { "Content-Type": "application/json" },
});
console.log(await res.json());

Réponse :

{
"translatedText": "¡Hola!"
}

Requête :

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: "Bonjour!",
source: "auto",
target: "en",
api_key: "xxxxxx"
}),
headers: { "Content-Type": "application/json" },
});
console.log(await res.json());

Réponse :

{
"detectedLanguage": {
"confidence": 90.0,
"language": "fr"
},
"translatedText": "Hello!"
}

Requête :

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: '<p class="green">Hello!</p>',
source: "en",
target: "es",
format: "html",
api_key: "xxxxxx"
}),
headers: { "Content-Type": "application/json" },
});
console.log(await res.json());

Réponse :

{
"translatedText": "<p class=\"green\">¡Hola!</p>"
}

Requête :

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: "Hello",
source: "en",
target: "it",
format: "text",
alternatives: 3,
api_key: "xxxxxx"
}),
headers: { "Content-Type": "application/json" },
});
console.log(await res.json());

Réponse :

{
"alternatives": [
"Salve",
"Pronto"
],
"translatedText": "Ciao"
}

Requête :

const res = await fetch("https://libretranslate.com/detect", {
method: "POST",
body: JSON.stringify({
q: "Bonjour!",
}),
headers: { "Content-Type": "application/json" },
});
console.log(await res.json());

Réponse :

[{
"confidence": 90.0,
"language": "fr"
}]

https://libretranslate.com/languages

Vous pouvez utiliser l’API LibreTranslate en utilisant les bindings suivants :