With the LoopBack framework you can easily build REST APIs for your own business objects, you can 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 how 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.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"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"
]
}
]
}
}