Beginner 15 minutes

Translate Text with LibreTranslate

Build a simple text translator using the free and open-source LibreTranslate API. Supports dozens of languages.

What You'll Build

A translation tool that converts text between languages with auto-detection of the source language.

Prerequisites

  • Basic JavaScript knowledge
  • Understanding of POST requests
1

Choose your LibreTranslate instance

LibreTranslate is open source and has multiple public instances. You can use libretranslate.com (requires API key) or self-host for unlimited free use.

2

List available languages

Fetch the list of supported languages to see what translations are available.

javascript
const response = await fetch('https://libretranslate.com/languages');
const languages = await response.json();

console.log('Supported languages:');
languages.forEach(lang => {
  console.log(`  ${lang.code} - ${lang.name}`);
});
3

Translate text

Send a POST request with the text, source language, and target language.

javascript
const response = await fetch('https://libretranslate.com/translate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    q: 'Hello, how are you?',
    source: 'en',
    target: 'es',
    api_key: 'YOUR_API_KEY'
  })
});
const data = await response.json();

console.log(`Original: Hello, how are you?`);
console.log(`Translated: ${data.translatedText}`);
4

Auto-detect source language

Set the source to 'auto' to let the API detect the input language automatically.

javascript
async function translate(text, targetLang) {
  const response = await fetch('https://libretranslate.com/translate', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      q: text,
      source: 'auto',
      target: targetLang,
      api_key: 'YOUR_API_KEY'
    })
  });
  const data = await response.json();
  return data.translatedText;
}

// Translate from any language to English
const result1 = await translate('Bonjour le monde', 'en');
console.log(result1); // Hello world

const result2 = await translate('Guten Morgen', 'en');
console.log(result2); // Good morning

Next Steps

  • Build a bilingual chat interface
  • Add speech-to-text for voice translation
  • Create a browser extension for page translation