Abstracting out Environment Variables for Bluemix

Share: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditEmail this to someonePrint this page

ThinkstockPhotos-180481342 [Converted]

Abstracting Variables

When developing an application that will be deployed to Bluemix, it’s nice to run locally and connect to the same services and backends you’ll be using in production (instead of faking data or responses). The goal is to abstract out the differences between accessing them on Bluemix and accessing them locally. Here is an easy way to do that without using a proxy and without updating your local environment variables for every different project you’re working on.

Note: this post will cover the Node.js and Liberty runtimes – for a more in depth view on just Node.js, refer to this blog post.

If you navigate over to your environment variables for a given application in Bluemix, you’ll see something like:

In your local environment, make a VCAP_SERVICES.json file whose content and structure is identical to the environment variables listed above.


For the Node.js runtime, make a module vcapServices.js whose content is:

This creates a singleton module that is a JSON representation of your environment variables. For example, to acess mongolab credentials, you would:


For the Liberty runtime, you can make a class with a singleton object that has getters for your environment variables. We make it a singleton to avoid parsing the JSON every time. The call to get this object calls initialize() if the object has not yet been initialized. This method gets the JSON object from either the “VCAP_SERVICES” environment variable (if running on Bluemix) or from the VCAP_SERVICES.json file we created earlier, parses it, and sets values that can be retrieved from getter methods.

To access your environment variables in other classes, all you need to do is:

Just make sure to add path/to/VCAP_SERVICES.json to both your .gitignore and .cfignore!

Share: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditEmail this to someonePrint this page
Jon Kaufman

Jon Kaufman

Developer Advocate at IBM
Jon Kaufman
Jon Kaufman

Latest posts by Jon Kaufman (see all)

Jon Kaufman


  • Lee Surprenant

    Jon, a number of the Node.js boilerplates and samples on Bluemix are using the following Node.js package for abstracting VCAP_SERVICES: https://www.npmjs.com/package/cfenv

    Would love to hear how your approach relates to that.

  • The cfenv package requires you to manually enter the VCAP_SERVICES when running locally. Once you do this, using cfenv to access the services will prevent the need for the code I posted for the Node.js runtime. A way to combine my approach with using the cfenv package would be:

    var cfenv = require('cfenv');
    var appEnv;
    // if we're in CF or Bluemix, don't need to specify services manually
    if (process.env.VCAP_SERVICES) {
    appEnv = cfenv.getAppEnv();
    // otherwise, specify the service values when creating the appEnv
    } else {
    try {
    appEnv = cfenv.getAppEnv({
    vcap: {
    services: require('./VCAP_SERVICES.json')
    } catch (e) {

    This way you don’t have to hard code in the values to your code and can still keep a config file, and you can use the helper methods cfenv provides for accessing/parsing the vcap services.

Leave a Reply