Hello, Caver (BApp 101)

Klaytn is a public blockchain platform empowering you to run applications that are transparent, decentralized, and auditable. Caver is your friendly tool which helps you write Klaytn-powered applications.

This topic is prepared to guide you to write the first blockchain-based application, or BApp in short, using Caver. Note that if you are familiar with Ethereum DApps already, then this is going to be a bit redundant. You can jump to here to get started with caver-js.

BApp is simply an application that talks to a blockchain. In our case, Klaytn is the blockchain we talk to. Now when you need to talk to a blockchain, you have to follow blockchain communication protocol. In case you don’t know what protocol is, here’s a Wikipedia link: Communication protocol - Wikipedia

Each blockchain has its own protocol, and so does Klaytn. So, writing an application talking to Klaytn is equivalent to developing a software communicating with Klaytn using Klaytn-specific protocol. This is where Caver shines :sunny:

Get Blockchain Height BApp

Caver is a Software Development Kit (SDK) which pretty much has everything you need about Klaytn protocol. In this article, we will write a very simple BApp called Get Blockchain Height (GBH) which… does… one task — getting the latest block number. It sounds too simple, but this is going to be super helpful if you are new to blockchain development in general.


We will use caver-js, Javascript implementation of Caver, for GBH. Here’s a prerequisite for caver-js:

  • Familiar with terminal
  • Working knowledge of Javascript with Node.js
  • Able to install software dependencies using npm


All good? Good. Assuming you already have installed Node.js, open your terminal and install caver-js with npm using the following command:

$ npm install caver-js

This will automatically installs the latest version of caver-js. In case you want to install a specific version of caver-js, type the following with the exact version information:

$ npm install caver-js@X.Y.Z

Connecting to Baobab

Once you are done installing caver-js, run node to enter node console. We will connect to an Endpoint Node (EN) of Klaytn testenet — Baobab.

$ node
> const Caver = require('caver-js');
> const caver = new Caver('https://api.baobab.klaytn.net:8651/');

At this point, you are connected to Baobab (specifically saying, an EN forming the Baobab blockchain network). Now you can ask blockchain information via caver.

Calling APIs

We will use getBlockNumber to retrieve the latest block number (or height). Enter the following to the console:

> caver.klay.getBlockNumber((err, result) => console.log(result));

This will output the latest block number of the Baobab blockchain network.

Putting All Together

Now, open your favourite editor and type the following in one file called GBH.js.

const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651/');
caver.klay.getBlockNumber((err, result) => {
    if (!err) {
    } else {

Saved? Then let’s run GBH as follows:

$ node GBH.js

The number shown above is the result I received on Feb 3rd, 2020. If you run this code, you’ll get some number higher than that.


That’s it for GBH, your first BApp!:confetti_ball: You have successfully connected to Baobab and inquired the latest block number. BTW, Baobab is a great place to test your idea, we will dig deeper later. I’ll write articles for somewhat more sophisticated BApp examples later. So, stay tuned :wink: