Connect a Node.js Application to the Skycoin Explorer API

iaufmc
Skyfleet Captain’s Log
6 min readMay 16, 2019

Ever wanted to code your own Skycoin Explorer? Well, you can! In this tutorial, I will show you how to connect the Skycoin Explorer API to Node.js.

Like that? Keep reading and you will find out how to do that yourself!

You should already have a Skycoin Node running. If not visit my previous tutorial (you just need a Skycoin Wallet running on your machine).

You already have Node.js installed and an IDE? Scroll down to Step 4!

What is the Skycoin Explorer API?

API stands for application programming interface and Wikipedia describes it as a set of subroutine definitions, communication protocols, and tools for building software. Pretty much nailed it: Using the Skycoin Explorer API enables you to make specific calls to get information from the Skycoin Blockchain.

Some example calls are:

  • /api/coinSupply: Returns metadata about the coin distribution
  • /api/richlist: returns top N rich wallets by coin count
  • /api/addresscount: returns count number of unique address with unspent outputs

Why should you connect the Skycoin Explorer API to a Node.js server?

Do you remember the official explorer website https://explorer.skycoin.net ? This is a regular website and it’s making API calls to some Skycoin node’s Explorer API. You could create your own implementation of a Skycoin Explorer or you could create a website that keeps track of Skycoins circulating supply.

Why am I choosing Node.js?

Node.js is a server-side JavaScript environment. It is super easy to run a server and can be done locally.

If you chose any other approach (Apache, Nginx, PHP) you could do that too and still follow this tutorial with just minor changes. I would also love to read your tutorial on doing so!

Step 1: The Skycoin node

I already mentioned it before: Please follow the instructions here and make sure to test the first calls using your browser. If that works — great! Go to step 2. If it doesn’t, leave me a message or join the Skycoin Telegram communities!

Step 2: Install Node.js

Installing Node.js is pretty straight forward. For this tutorial, I use OS X but Windows or Linux is fine too.

Remember: I am on an OS X (macOS) machine. If you are using Windows, select the Windows download.
  1. Go to https://nodejs.org and hit that download button
  2. Double-click the downloaded file and let the installer do the rest for you
  3. Verify the installation by typing
node -v

into the Terminal (Command Prompt for Windows). If it returns a version number, you are good to go!

node -v returns a version number? You did it!

Step 3: Setup your development environment

Now we should decide which IDE we’re using. IDE stands for “integrated development environment,” think of it as a text editor that helps you to code better (basic stuff like syntax highlighting but also a lot of other stuff).

My first choice for web development is the Atom Editor: https://atom.io/

However, you can choose any you want like Sublime, Brackets, Visual Studio or just the default text editor on your machine.

Step 4: Create your first Node.js application

Create a folder on your machine and call it “SkycoinNodejs” (that is how I call that folder, you can name it how you like) and use the Terminal / Command Prompt to navigate into that folder:

cd /path/to/the/folder/SkycoinNodejs
you can use the command “pwd” to get your present working directory

Add the same folder as a Project Folder in Atom:

Select the folder you just created and add it as a Project Folder

Now right-click the Project Folder on the left tab and select “Create File”. Call that file Main.js and add the following code:

add the main.js directly to the folder (and ignore the .git folder in this screenshot)
console.log("Hello, World!")

Save the file ( Control + s ), go to your Terminal and execute the file typing

$ node main.js
Hello, World!

Congratulations! You created your first Node.js application (which does nearly nothing). Until that point, this tutorial was nothing new for most people and had nothing to do with the Skycoin Explorer API. So with the next step, we will make our first API call!

Step 5: Our first Skycoin Explorer API call using Node.js

In order to make a call to the API we will do an HTTP request. This is just the same request our browser makes but we can make that from our Node.js application.

There are different approaches on how to do this with Node.js. To keep it simple, I am using the default HTTP request that comes with Node.js. If you are more experienced or would like to use another approach, have a look at Request.

We will edit our Main.js file and delete our “Hello, World!” console.log.

Add the following code (and remember to add the correct SkycoinPort:

const http = require(‘http’);
const apiURL = “http://localhost:SkycoinPort/api/v1/addresscount"
http.get(apiURL, res => {
let body = “”;
res.on(“data”, data => {
body += data;
});
res.on(“end”, () => {
body = JSON.parse(body);
console.log(body);
});
});

Before we dig into that code, go into the terminal and execute the file using the command

$ node main.js

and the result will be:

Node.js returns the current Skycoin address count

Understanding the code

What are these 12 lines of code doing?

First of all, we declare the two Variables http and apiURL as const. const variables cannot be reassigned.

const http = require(‘http’);

creates a custom http.Agent instance and

const apiURL = “http://localhost:SkycoinPort/api/v1/addresscount"

makes the following code better readable by putting the long url into one variable.

http.get(url, res => { 
...
}

Our request to the Skycoin Explorer API is a GET request. We could also use http.request() but http.get() is already set to GET and req.end() gets called automatically. Basically, as we know that we are using a GET request, we use http.get().

The res function gets called when the connection is established.

let body = “”;

The stream we receive gets saved into the body variable. We use let to declare it so we can later redeclare it if necessary.

res.on(“data”, data => {
body += data;
});

res.on(…) gets called when we receive data. We put that data into the just declared body variable.

res.on(“end”, () => {
body = JSON.parse(body);
console.log(body);
});

res.on(“end”…) is called after we receive the last data from http.get(). As I mentioned before, http.get() gets a stream. To work with that stream object we parse it as JSON using

body = JSON.parse(body);

The last step is our actual API output using the console.log function.

Bonus: Make a quick command line application

Editing code is fun but how about making the call customizable using the command line? You can find out how to do that on GitHub: https://github.com/iaufmc/SkycoinNodejs

What is next?

Today we learned how to make a request to the Skycoin Explorer API using Node.js. With that knowledge, we can start developing web applications that utilize or display the information the API provides us.

If you like this tutorial you can tip me in Sky: 2BAEQ9tdibebL5XrozW4nQ7ciu4JnFXhcAS or feel free to comment and follow me on Twitter: https://twitter.com/iaufmc

Thanks for reading!

--

--