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

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 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 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 Lunch 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.
watsonservice_credentials
Language Translator API credentials page

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.

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:
// API URL
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.

ServerCode_script_watson_translate_api
Server Code script to invoke Language Translator API

There is one more think 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 on Server Code main tab.
  2. Click on Plugins tab.
  3. Find EncodeBase64 library and click the import link.
  4. The library containing this script will be added under Libraries.
  5. Open the script again. Once the editor loads, open the Dependencies tab.
  6. Check the EncodeBase64_base64_library. The library and all its functions are now available to be used inside the script.
  7. You can now go back to the editor by switching to the Script tab.

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.

servercodescript_parameters
Server Code script parameters

To add parameters:

  1. Open 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. Go back to Run tab and click Save and run button to invoke the script with parameters data.
ServerCode_script_test
Testing the script

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.

servercode_test_browser
Invoking the script from the browser

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.


If you are in the Bay Area and want to learn about AI, Blockchain, IoT, Machine Learning, APIs, Data Science and other technologies, then attend our free developer workshops.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s