Google Translate API Wrapper (Unofficial)

2.3.0 · active · verified Wed Apr 22

The `google-translate-api` package provides a free and unlimited programmatic interface to Google Translate services. Unlike Google's official Cloud Translation APIs, this library functions as a wrapper around the public Google Translate website's frontend. It offers features such as automatic language detection, spelling correction, and language correction. The current stable version is 2.3.0, with recent updates focused on bug fixes, like handling `null` responses, and adding support for new languages. The package generally follows a patch and minor release cadence for ongoing maintenance. Its key differentiator is providing translation functionality without requiring API keys or incurring costs associated with Google Cloud Translate, making it suitable for hobby projects or those with budget constraints. However, developers should be aware that its reliance on reverse-engineering the Google Translate website means it can be susceptible to unannounced breaking changes if Google alters its front-end structure.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic text translation with automatic language detection, handling of spelling corrections, and retrieving the raw API response for debugging or advanced parsing.

const translate = require('google-translate-api');

console.log('Starting translation examples...');

// Example 1: Auto-detect language and translate to English
translate('Ik spreek Engels', { to: 'en' })
    .then(res => {
        console.log('--- Example 1: Auto-detect to English ---');
        console.log('Original Text: "Ik spreek Engels"');
        console.log('Translated Text:', res.text);
        console.log('Detected Source Language (ISO):', res.from.language.iso);
        console.log('-------------------------------------------\n');
    })
    .catch(err => {
        console.error('Error in Example 1:', err);
    });

// Example 2: Translate from English to Dutch with a typo, demonstrating auto-correction
translate('I spea Dutch!', { from: 'en', to: 'nl' })
    .then(res => {
        console.log('--- Example 2: English to Dutch with Typo ---');
        console.log('Original Text: "I spea Dutch!"');
        console.log('Translated Text:', res.text);
        console.log('Source Text Auto-Corrected:', res.from.text.autoCorrected);
        if (res.from.text.autoCorrected || res.from.text.didYouMean) {
            console.log('Corrected/Suggested Source Value:', res.from.text.value);
        }
        console.log('-------------------------------------------\n');
    })
    .catch(err => {
        console.error('Error in Example 2:', err);
    });

// Example 3: Demonstrating raw output option
translate('Hello World', { to: 'es', raw: true })
    .then(res => {
        console.log('--- Example 3: Raw Output ---');
        console.log('Translated Text (from raw):', res.text);
        console.log('Raw API Response (truncated):', res.raw.substring(0, 100), '...');
        console.log('-------------------------------------------\n');
    })
    .catch(err => {
        console.error('Error in Example 3:', err);
    });

view raw JSON →