Google Translator with node.js

How to use google translator API with node.js

30 Days of Node | Node.js Tutorial series


Google translator is a service which helps to convert one language to the other. This has many practical applications in day to day life. Internationalization of a web application is one such application. Internationalization or i18n is process of creating a web application in a way that it can be served in multiple local languages across the world. This only makes the app more accessible to a wider audience using different languages. In the following article we'll see how we can use google translator in a node.js based application.


  1. Nodejs [ LTS ]
  2. NPM

Steps to Follow

  • Create a project on google cloud
  • Add node sdk for translator
  • Run the app
  • Let's go through each of these steps and understand in detail all the steps mentioned above.

    Create a project on google cloud

    1. Navigate to and login using your google credentials.

    2. Create a new Project, name it as per your requirements. In this tutorial we've named it node-translator

    3. Once the project is created, you'll be navigated to the app dashboard screen and there you'll see different APIs which you can add to your project.Just click on the Cloud Translation API and you'll be taken to the cloud translation API page.

    4. On the Cloud Translation API page just click on enable button

    5. After this on the API details page , click on the create credentials button to create your API key

    6. On the Add Credentials to your project page , just click on the API Key to create an API Key.

    7. Then on the API key page, all you've to do is choose a name for your api key or keep it default and click on create API key keeping all other options as default [ atleast in test phase ]

    8. Great! now you'll land on your API key page where you can copy your API key for consumption in the next section where we set up our nodejs project.


    Add node sdk for translator

    In the first section we've created a project on google cloud and we've created the required API Key. In this section we'll see what needs to be done on the node.js side. Note that we're showing you the application using a very basic node script. You can easily use the same or equivalent code on more complex and advanced application e.g. web application written using frameworks like express, sails or hapi.

    Let's get going on the nodejs part

    • Step-1 Install the required package: We are using the google-translate package from npm for using google translator API. We can install it from npm using the following command :
      npm install google-translate 										

    • Step-2 Code: Create a file named node-translate.js and add the following code to it. Don't forget to replace the api key with your actual api key.
      var googleTranslate = require('google-translate')(api);
      var text = 'I am using google translator to convert this text to spanish'
      console.log("English :>",text);
      googleTranslate.translate(text, 'es', function(err, translation) {
        console.log("Spanish :>",translation.translatedText);

    Run the app

    We can run the code using the following command and we will get the output shown below:

    G:\google-translator>node node-translate.js
    English :> I am using google translator to convert this text to spanish
    Spanish :> Estoy usando el traductor de google para convertir este texto al espa´┐Żol

    So , we've done it. We converted a string of text written in English to Spanish. You can play around with as many different language as supported by Google.


    In this article we learned how to integrate google translate api with a nodejs application.