Command Line Deskera API Client in Node.js | Part 3: Call an API Endpoint

Engineering Jul 10, 2020

This is Part 3 of the series of posts on writing a basic Command Line Deskera API client in Node.js.

Part 1 — Hello, World!

Part 2 — Hello, You!

Part 3 — Call an API Endpoint (this post)

Part 4 — OAuth 2.0 Authentication

Part 5 — Show Me the Data!

(Note: The GitHub links for this part are: Browse, Zip, Diff)

Add Support for Sending and Retrieving Data from an API

Install axios module as a dependency:

npm install axios

Call an API Endpoint

Now, update the bin/index.js file with the following code:

#!/usr/bin/env node

const yargs = require("yargs");
const axios = require("axios");

const args = yargs
 .usage("Usage: -n <name>")
 .option("n", { alias: "name", describe: "Your name", type: "string", demandOption: true })
 .argv;

const sal = `Hello, ${args.name}!`;
console.log(sal);

console.log("Here is a random useless fact for you:");

const url = "https://uselessfacts.jsph.pl/random.json";

axios.get(url).then(res => {
   console.log(res.data.text);
 });

We are now fetching a random useless fact from the API — https://www.programmableweb.com/api/random-useless-facts-rest-api-v10.

Install the updated script globally:

npm install -g .

Our script now will not only greet, it will output a random useless fact too. Let’s try:

% hello -n Deskera
Hello, Deskera!
Here is a random useless fact for you:
You can sail all the way around the world at latitude 60 degrees south.

Try again:

% hello -n Deskera
Hello, Deskera!
Here is a random useless fact for you:
Sean Connery verdiente phasenweise Geld durch das Polieren von Särgen.

This one is in German language. Let’s take the language as a script argument. Update the bin/index.js file with the following code:

#!/usr/bin/env node

const yargs = require("yargs");
const axios = require("axios");

const args = yargs
 .usage("Usage: -n <name>")
 .option("n", { alias: "name", describe: "Your name", type: "string", demandOption: true })
 .option("l", { alias: "lang", describe: "Language", type: "string" })
 .argv;

const sal = `Hello, ${args.name}!`;
console.log(sal);

if (args.lang) {
 console.log(`Here is a random useless fact in ${args.lang}...`)
} else {
 console.log("Here is a random useless fact for you:");
}

const url = args.lang ? `https://uselessfacts.jsph.pl/random.json?language=${escape(args.lang)}` : "https://uselessfacts.jsph.pl/random.json";

axios.get(url).then(res => {
    console.log(res.data.text);
  }).catch(err => {
    console.log(err.response.data.message);
  });

Let’s try:

% npm install -g .

% hello -n Deskera
Hello, Deskera!
Here is a random useless fact for you:
Die Funktechnik Bluetooth ist nach einem dänischen Wikingerkönig benannt

% hello -n Deskera -l en
Hello, Deskera!
Here is a random useless fact in en...
Lorne Greene had one of his nipples bitten off by an alligator while he was host of "Lorne Greene`s Wild Kingdom."

% hello -n Deskera -l en
Hello, Deskera!
Here is a random useless fact in en...
Non-dairy creamer is flammable.

% hello -n Deskera -l de
Hello, Deskera!
Here is a random useless fact in de...
Fünf Prozent aller Ehen in den USA werden in Las Vegas geschlossen

% hello -n Deskera -l de
Hello, Deskera!
Here is a random useless fact in de...
Der am häufigsten fotografierte Ort auf Instagram ist ein Einkaufszentrum in Bangkok: Siam Paragon.

% hello -n Deskera -l es
Hello, Deskera!
Here is a random useless fact in es...
You requested an invalid language: es. Allowed languages are de, en

Show Text and Error in Color

While showing plain text on the console gets the job done, it will be good to have content stand out. For example, we may want to display error messages in red.

We will use chalk module to do this

npm install chalk

Now, add the dependency in the  bin/index.js  file

const chalk = require("chalk");

And, update the console output with the following code:

axios.get(url).then(res => {
    console.log( chalk.green.bold( res.data.text ) );
  }).catch(err => {
    console.log( chalk.red( err.response.data.message ) );
  });

Let's try the output now

Delightful!

Next up: Part 4 — OAuth 2.0 Authentication

Brajesh Sachan

Brajesh, drives direction of Deskera’s future technology and shapes Deskera as the technology leader. With his expertise and over 15 years of experience, he has significantly contributed to Deskera

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.