In this post, you’ll learn how to invoke the Watson Language Translator API from a mobile back-end powered by

Before we start, let me give you a quick overview of the platform. is a low-code cloud platform for quickly building and running mobile web and hybrid apps. You can build a complete mobile app with little or no coding. The major benefits are:

  • A visual, drag-and-drop UI builder (based on jQuery Mobile, Ionic or Bootstrap).
  • A cloud database for storing app data.
  • A Node.js-like environment (called Server Code) for running any custom app code (written in JavaScript).
  • API Express for connecting to any external/enterprise data sources such as an SQL database, web service, or a REST API.
  • Sending push notifications.

If you’re looking for more content, simply look for posts tagged with makes it easy to invoke any third-party REST APIs using Server Code or API Express. In this post, you’ll learn how to invoke the Watson Language Translator API from Server Code. So let’s get started!

Watson Language Translator API

In this section, you’ll learn how to get started with the IBM Cloud (formerly IBM Bluemix) and launch the Watson Language Translator API. The Watson Language Translator API enables you to translate one language to another and detect which language was entered. You can learn more on the Watson Language Translator API page.

Create an IBM Cloud account

To get started, you need to create a new IBM Cloud account:

  1. Register for a free IBM Cloud account or sign into your existing account.
  2. When the account is created, go to the IBM Cloud dashboard.

Next you are going to launch the Watson Language Translator API.

Watson service credentials
Watson service credentials.

Launching the Watson Language Translator API

To launch the Watson Language Translator API:

  1. On the dashboard, under Products and services, you will see IBM Bluemix. Click the Launch button.
  2. Expand the left menu and click Watson.
  3. Click Create Watson service.
  4. Click the Language Translator service.
  5. On the service creation screen, change the Service name to Language Translator Leave all other values as they are.
  6. Click Create. The service is created. You will now see the Getting started tab. On the same page you will see a curl command that you can use to test the service.
  7. Click the Service credentials tab (on the left). You will see the default created username/password when you expand View credentials. You will use the credentials when invoking the API from

That’s all you need to do to create a Language Translator API. If you want, you can test the service using the curl command. Next you are going to invoke this API from

In this section, you will learn how to get started with and create a Server Code script to invoke the Language Translator API.

Getting Account

To get started, register for an account (free trial) or sign into an existing account.

Script to Invoke API

Server Code is a server-side JavaScript runtime (like Node.js) where you can write any app logic and also invoke any third-party external API. From the dashboard, complete the following steps:

  1. Click on Server Code tab.
  2. Click the Create new script button. A new script is created.
  3. Enter WatsonTranslator as the script name.
  4. Delete the default script code and copy and paste the following script:
var url = "";
// Optional parameters
var text = request.get("text") || "Hello";
var source = request.get("source") || "en";
var target = request.get("target") || "es";
// Credentials
var username = "Username from service credentials";
var password = "Password from service credentials";
// HTTP POST request
var XHRResponse = XHR2.send("POST", url, {
   "headers": {
      Authorization: "Basic " + encodeBase64(username + ":" + password),
      "Content-Type": "application/json",
      "Content-Accept": "application/json"
   "body": {
      "text": text,
      "source": source,
      "target": target
// Setting service response
Apperyio.response.success(XHRResponse.body, "application/json");

  • Line 1: The Watson Language Translator API endpoint.
  • Lines 5-7: Optional parameters. If parameters are not specified, the default values will be used.
  • Lines 10-11: API credentials. Copy the username/password values from the Watson Language Translator API credentials page.
  • Line 14: Invokes the API (POST request).
  • Line 27: Setting script result.
Server Code script
Server Code script.

There is one more thing you need to do before testing the script. Line 16 encodes the username and password using a function. The library that has this function needs to be imported into your account and then added as dependency to this script.

  1. Click the Server Code main tab.
  2. Click the Plugins tab.
  3. In the EncodeBase64 library, click the import link. The library containing this script will be added under Libraries.
  4. Open the script again. When the editor loads, click the Dependencies tab.
  5. Check the EncodeBase64_base64_library. The library and all its functions are now available to be used inside the script.
  6. Go back to the editor by clicking the Script tab.

That’s it for the script. The next step is to test it.

Testing the Script

It’s simple to test the script. Click the Run tab and then click Save and run. You didn’t specify any parameters, so the default values are used.

Script parameters
Script parameters.

To add parameters:

  1. Click the Script parameters tab.
  2. Add two parameters:
    • text
    • target
  3. For text, enter a text such as Where is the restaurant?
  4. For target enter: fr (for French).
  5. Return to the Run tab and click Save and run to invoke the script with the parameters data.
Script test
Script test.

The script you just created is also a REST API. To see the endpoint, click the API Information tab (on the left). There you will see the REST API URL. Because the API works with default parameters, you can copy the URL, put it in the web browser and invoke it that way.

Invoking the script from the browser
Invoking the script from the browser.

What’s next?

In this post, you’ve learned how to create a server-side script (or, really, a mobile back-end) that calls the Watson Language Translator API. The next step is to create the mobile app UI. I’ll cover that in a future post. If you want to learn how to do that now, check the YouTube channel. You will find many videos on how to build the app UI and connect it to a mobile backend and external REST APIs.

If you are in the Bay Area and want to learn about AI, blockchain, IoT, machine learning, APIs, data science, and other technologies, why not attend our free developer workshops? We’d love to meet you!

Join The Discussion

Your email address will not be published. Required fields are marked *