Calling APIs from Javascript/Node.js

This is one of the most common things nowadays developers are doing more than before. Raise of Microservices based architecture and distributed software development needs more inter-service communication i.e more codes for API consuming stuff, sometimes more than actual business logic. It is good that business logic coding is now less redundant.

Consuming API with nodeJS

Node.js comes with native HTTP and https to request APIs. http.get() or https.get() is good when you have simple API GET calls. No third-party module needed just wrap with promises. It outputs as a stream of API response.

Using “request” module: This library is here since 2011. it’s the very popular one. Request library has no support for promises. so you have to wrap it with promise, some promise library like ‘when‘ and ‘deferred‘ can be perfect for complex promises.

Using “axios” module: axios returns a Promise so you don’t have to wrap with a Promise. It parses JSON response by default. since I like to build SDK module for APIs where I have to wrap with another promise. axios has relatively less dependency. works with webpack for your react-redux project. here is an example of axios

let apiClient = axios.create({
    baseURL: 'your-api-base-url-here',
    timeout: 10000,
    headers: {'Authorization': 'Bearer your-jwt-here' }
    .then(function (response) {
      if(response.status == 200){
    .catch(function (error) {

Using “superagent“: superagent is another nice library. As the documentation says it was built for front-end ajax based use case. it works with browserify and webpack. but it works with server-side node.js. superagent is easy has lots of community plugins