Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Consume Bluemix Services in LoopBack Applications

DZone's Guide to

How to Consume Bluemix Services in LoopBack Applications

With the LoopBack framework, you can easily build REST APIs for your own business objects. Additionally, Bluemix services can be consumed (for example, the cognitive Watson services). Read on for a sample of how all this works.

· Cloud Zone ·
Free Resource

Insight into the right steps to take for migrating workloads to public cloud and successfully reducing cost as a result. Read the Guide.

With the LoopBack framework, you can easily build REST APIs for your own business objects. You can also build web and mobile clients, and much more. The LoopBack backend applications and the AngularJS web clients can be hosted on Bluemix. Additionally, Bluemix services can be consumed (for example, the cognitive Watson services). Below is a sample of how all this works.

Get the code of the sample application from GitHub.

When Bluemix services are created and bound to Bluemix applications, they provide credentials and endpoints that are needed to invoke their APIs. The credentials are stored in an environment variable VCAP_SERVICES.

loopbackbluemixservices

The following snippet shows how to use the Watson Language Translation service. The credentials are read from the environment variable via the library cfenv-wrapper.


var watson = require('watson-developer-cloud');
var cfenv = require('../../server/cfenv-wrapper');
var appEnv = cfenv.getAppEnv();           
var translationConfig = appEnv.getService(/translation.*/);
if (translationConfig) {                           
  var language_translation = watson.language_translation({
    username: translationConfig.credentials.username,
    password: translationConfig.credentials.password,
    version: 'v2'
  });

  language_translation.translate({
    text: output.approvalRequest.title, source : 'en', target: 'es' },
    function (err, translation) {
      if (!err) {
        output.translatedTitle = translation;

In order to run the exact same code locally, you need to put the credentials into a property file. Via the command 'cf env collaboration' you can receive the credentials or you can simply copy and paste them from the Bluemix dashboard.


{
 "VCAP_SERVICES": {
  "language_translation": [
   {
    "credentials": {
     "password": "xxx",
     "url": "https://gateway.watsonplatform.net/language-translation/api",
     "username": "xxx"
    },
    "label": "language_translation",
    "name": "collab-translation",
    "plan": "standard",
    "tags": [
     "watson",
     "ibm_created",
     "ibm_dedicated_public"
    ]
   }
  ]
 }
}

TrueSight Cloud Cost Control provides visibility and control over multi-cloud costs including AWS, Azure, Google Cloud, and others.

Topics:
ibm bluemix ,cloud ,loopback

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}