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

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

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"
    ]
   }
  ]
 }
}

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:
ibm bluemix ,cloud ,loopback

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}