Rick
Rick

Reputation: 43

How to get customAPI url from node.js app?

I am just starting with BlueMix and in my space I have:

From the Cloud Integration service> API view, I can get the URLs for the different resources (for instance http://endpoint_ip:endpoint_port/api/version/path_to_resource), so I can hardcode these URLs in my node.js application and it works.

But if I bind the Cloud Integration service and even the customAPI to my node.js application, I don't get any information in VCAP_SERVICES about the endpoint URL; but I have seen examples of VCAP_SERVICES where the API URL is available.

Below is my VCAP_SERVICES

{"CloudIntegration": [
      {
         "name": "Cloud Integration-b9",
         "label": "CloudIntegration",
         "plan": "cloudintegrationplan",
         "credentials": {
            "userid": "apiuser@CloudIntegration",
            "password": "S!2w3e40",
            "apis": [
               {
                  "name": "Catalog Manager API",
                  "desc": "Catalog Manager API",
                  "resource": ""
               }
            ]
         }
      }
   ]
} 



What I am trying to achieve is to avoid hardcoding URLs in my application, since I can bind a BlueMix service to it, and perhaps get info from the environment.
Am I doing something wrong? Or is that not the way it is supposed to work?
Also I don't really get why there is nothing in the VCAP_SERVICES.CloudIntegration[0].credentials.apis[0].resource even though I have my customAPI specifies resources.

Upvotes: 0

Views: 177

Answers (2)

Andrew L
Andrew L

Reputation: 5496

Since your goal is to "to avoid hardcoding URLs in my application, since I can bind a BlueMix service to it, and perhaps get info from the environment." I would like to suggest using a user provided service.

This will create a user provided service and start interactive input for you to enter the api url and a password. You can add more parameters if you need.

cf cups servicename -p "url, password"

Bind this service to your application and restage. You can access these parameters in your Node.js application easily with the cfenv module.

var cfenv = require("cfenv");
var appEnv = cfenv.getAppEnv();
var myService = appEnv.getService("servicename");

//use myService.credentials.url to access the url value.
//use myService.credentials.password to access the password value. 

The user provided services VCAP_SERVICES looks like:

{
  "user-provided": [
    {
      "name": "servicename",
      "label": "user-provided",
      "credentials": {
        "url": "myURL",
        "password": "myPassword"
      }
    }
  ]
}

Upvotes: 0

@Rick

Make sure you "publish" your API after configuring the Cloud Integration service. Then service credentials will reflect the changes:

"CloudIntegration": [
  {
     "name": "Cloud Integration-v5",
     "label": "CloudIntegration",
     "plan": "cloudintegrationplan",
     "credentials": {
        "userid": "apiuser@CloudIntegration",
        "password": "S!2w3e40",
        "apis": [
           {
              "name": "SwaggerPetStore",
              "desc": "SwaggerPetStore",
              "resource": "http",
              "baseurl": "http://mypypatchank.mybluemix.net"
           }
        ]
     }
  }
]

in the same way, if you use the API management service, you will have a corresponding VCAP_SERVICES entry

"Swagger Petstore v1 : Sandbox 551b2dcf0cf2521d98d061d4 prod": [
  {
     "name": "Swagger Petstore v1 : Sandbox prod-w0",
     "label": "Swagger Petstore v1 : Sandbox 551b2dcf0cf2521d98d061d4 prod",
     "plan": "plan1 : Sandbox prod",
     "credentials": {
        "clientID": "55cfe3fa-ff59-474c-a1b6-46d3cc9871",
        "clientSecret": "uK3xM3eF4cA1qF7yW8mC2lP6wS6aG7sQ5cL2yJ4sC6iS1dE7",
        "url": "https://api.eu.apim.ibmcloud.com/garciatemx1ibmcom/sb/api"
     }
  }
]

Upvotes: 0

Related Questions