Saltearse al contenido

Uso de la API

Los siguientes ejemplos usan Javascript, pero puedes usar cualquier lenguaje de programación. También hay enlaces de lenguaje disponibles para muchos lenguajes de programación.

Solicitud:

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: "Hello!",
source: "en",
target: "es",
api_key: "xxxxxx" // puede ser opcional si lo alojas tú mismo
}),
headers: { "Content-Type": "application/json" },
});
console.log(await res.json());

Respuesta:

{
"translatedText": "¡Hola!"
}

Solicitud:

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());

Respuesta:

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

Solicitud:

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());

Respuesta:

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

Solicitud:

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());

Respuesta:

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

Solicitud:

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());

Respuesta:

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

https://libretranslate.com/languages

Puedes usar la API de LibreTranslate con los siguientes enlaces: