Quickstart for Microsoft Translator Text API with Node.js

This article shows you how to use the Translate method to translate text from one language to another. For information on how to use the other Translator Text APIs, see this Github repository.


You will need Node.js 6 to run this code.

You must have a Cognitive Services API account with Microsoft Translator Text API. You will need a paid subscription key from your Azure dashboard.

Translate text

The following code translates source text from one language to another, using the Translate method.

  1. Create a new Node.js project in your favorite IDE.
  2. Add the code provided below.
  3. Replace the key value with an access key valid for your subscription.
  4. Run the program.
'use strict';

let https = require ('https');

// **********************************************
// *** Update or verify the following values. ***
// **********************************************

// Replace the subscriptionKey string value with your valid subscription key.
let subscriptionKey = 'ENTER KEY HERE';

let host = 'api.microsofttranslator.com';
let path = '/V2/Http.svc/Translate';

let target = 'fr-fr';
let text = 'Hello';

let params = '?to=' + target + '&text=' + encodeURI(text);

let response_handler = function (response) {
    let body = '';
    response.on ('data', function (d) {
        body += d;
    response.on ('end', function () {
        console.log (body);
    response.on ('error', function (e) {
        console.log ('Error: ' + e.message);

let Translate = function () {
    let request_params = {
        method : 'GET',
        hostname : host,
        path : path + params,
        headers : {
            'Ocp-Apim-Subscription-Key' : subscriptionKey,

    let req = https.request (request_params, response_handler);
    req.end ();

Translate ();

Translate response

A successful response is returned in XML, as shown in the following example:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Salut</string>

Next steps

See also

Translator Text overview
API Reference