How to Invoke the Watson Language Translator API From an Appery.io App

DZone 's Guide to

How to Invoke the Watson Language Translator API From an Appery.io App

In this tutorial, you'll learn how to invoke the Watson Language Translator API from an app in Appery.io, a cloud platform for mobile and hybrid apps.

· Mobile Zone ·
Free Resource

In this blog post, you will learn how to invoke the Watson Language Translator API from an Appery.io app. Before you will learn how to do that, let me give you a quick overview of the Appery.io platform.

Appery.io is a cloud platform for building and running mobile web and hybrid apps. The platform provides the following major benefits:

  • A visual, drag and drop UI builder (based on jQuery Mobile, Ionic or Bootstrap)
  • A cloud database for storing app data.
  • 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 a SQL database, web service or a REST API.
  • Sending Push Notifications.

If you are looking for more Appery.io content, simply look for posts tagged with Appery.io.

Appery.io makes it very easy to invoke any 3rd party REST APIs using Server Code or API Express. In this blog post, you will learn how to invoke the Watson Language Translator API from Server Code. Let's start.

Watson Language Translator API

In this section, you will learn how to start with the IBM Cloud and launch the Watson Language Translator API. The Language Translator API allows you to translate one language to another language. Other API features include detecting the what language was entered. To learn more about the API here.

Getting an IBM Cloud Account

To get started, create a new IBM Cloud account:

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

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

Launching the Watson Language Translator API

To launch the Watson Language Translator API:

  1. From the dashboard, under Products and services you will see IBM Bluemix. Click the Launch button.
  2. Expand the left-side menu, locate and click on Watson.
  3. Click Create Watson service button.
  4. Find the Language Translator service and click on it.
  5. On service creation screen, change the Service name to Language Translator Appery.io.
  6. Keeping all other values default, click the Create button.
  7. The service has been created and you should see the Getting started tab. On the same page, you will see a curl command which you can use to test the service.
  8. Click Service credentials tab (on the left).
  9. There you will see the default created username/password when you expend View credentials. You will use the credentials when invoking the API from Appery.io.

Image title

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 Appery.io.


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

Getting Appery.io Account

To get started, create a new Appery.io account:

  1. Register for an Appery.io 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 3rd party external API. From the Appery.io dashboard:

  1. Click on Server Code tab.
  2. Click Create new script button.
  3. A new script will be created. For script name enter WatsonTranslator.
  4. Delete the default script code and copy and paste the following script:
var url = "https://gateway.watsonplatform.net/language-translator/api/v2/translate";

// 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: is the Watson Language Translator API endpoint.

Lines 5-7: are optional parameters. If parameters are not specified, then the default values will be used.

Lines 10-11: are 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.

Image title

That's it for the script. Next step is to test the script.

Testing the Script

Testing the script is very simple. Simply switch to the Run tab and click Save and run button. You didn't specify any parameters so the default values are used.

Image title

To add parameters:

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

Image title

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

Image title

What's Next?

In this blog post, you learned how to create a server-side script (or really a mobile backend) that calls the Watson Language Translator API. The next step would be to create the mobile app UI. I will cover that in another post. If you want to learn how to do that now, check the Appery.io 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.

api, appery.io, integration, mobile, mobile app development, watson

Published at DZone with permission of Max Katz , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}