Over a million developers have joined DZone.

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

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! 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"
    ]
   }
  ]
 }
}

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! 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 best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}