Request API node.js helper module

A convenient helper module for node.js and request when you regularly work with an API in your projects.

I tend to write node.js applications that rely heavily on a separate API layer (regardless of if thats written in node itself or not). Because of this I tend to use the same convention when consuming those API endpoints. Over time I’ve written a fairly handy helper module that makes working with API endpoints a breeze!

The code itself is hosted over at GitHub which you can find here: https://github.com/jkat98/api_helper

Heres the example code from the repo on how to work with it:

var api = require('../api_helper'),
    config = require('./config.json');

// call one-time during server bootstrap:
api.configure(config);

// call anytime/anywhere:
api.callAPI('users/list', 'GET', {}, function(err, body) {
    if (!err) {
        console.log(body);  // => data returned from API endpoint!
    }
});

Where config.json contains the following configuration settings for the API itself:

{
    "api": {
        "scheme": "http",
        "host": "api.somedomain.com",
        "port": "8080",
        "version": "v1"
    }
}

Now, whenever I want to consume an API endpoint in my project I simply use this helper module and it makes my life so much easier!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s