Rework: An Implementation of API Server for Hyperledger Fabric Network (Fabric v2.2)

KC Tam
5 min readAug 24, 2020


My previous work on implementation of API Server was done on Fabric v1.4 one year ago. Certain changes have been seen since v2.0. Here is a rework on the implementation. I will skip most of the explanation as the overall implementation logic is the same as the previous one. Only the difference is highlighted in this article.

You can always refer to this previous article for more detailed explanation.

Implementation Setup

Here is the setup of our implementation. The overall design remains the same. The only difference in this setup is the use of Test Network instead of First Network. Many sample applications and scripts are rewritten to use Test Network, and so is fabcar. For those who wish to know more about the difference between Test Network and First Network, you can refer to these articles (link, link).

Similar to our previous setup, we have two hosts setup for this implementation. They are AWS EC2 instances.

API Server Code in JavaScript

Here I simply reuse the previous apiserver.js with modification to reflect the difference in using Test Network and some change in SDK v2.2.

One major difference is the way to create wallet objects. In v1.4 it was handled by FileSystemWallet class. In v2.2 it is the class Wallets handling wallet in client applications. You can notice the change in the code (line 20, 65, 109 and 152).

Besides, the client applications in fabcar/javascript/ are modified. In v1.4 it was user1 as a directory, and inside the directory we see the private key, public key and certificates. Now In v2.2 it is, which is an object file containing the private key and certificate. In terms of functionality they are the same. Our API server code is updated to reflect this change.

Here is the code.

Demonstration Step

On Fabric Host

(screenshots are in black colour background)

Step 1: Bring up Test Network and deploy fabcar chaincode

We use the script to perform this step.

cd fabric-samples/fabcar/

Step 2: Generate user appUser for API server

By default, the fabcar uses SDK to generate a user appUser for client application (API server). we first install the packages. Then we execute the two scripts enrollAdmin.js and registerUser.js.

cd javascript/
npm install
node enrollAdmin.js
node registerUser.js

Upon success we will see the wallet/ directory and is generated.

For a quick test, we can use appUser to perform a chaincode function for querying all cars. We run node query.js, which performs the queryAllCars function. We will see the ten car records preloaded with initLedger function done in the script.

On API Server Host

(screenshots are in blue colour background)

Now we move to API Server. We use a Ubuntu 18.04 LTS server.

Step 3: Install NodeJS environment

sudo apt-get update
sudo apt install curl
curl -sL | sudo bash -
sudo apt install -y nodejs
sudo apt-get install build-essential
node -v
npm -v

Step 4: Copy files from Fabric Host

Create the directory in API Server Host.

mkdir apiserver
cd apiserver

The files we need on API Server are

  • wallet (identity): fabcar/javascript/wallet/
  • node package file: fabcar/javascript/package.json
  • connection profile: test-network/organizations/peerOrganizations/

Again we are using localhost as a bridge for my two AWS EC2 instances. For well structure we move the to wallet/

Here is the result.

Step 5: Copy apiserver.js to this directory.

Step 6: Modify connection profile

The connection profile when generated is using localhost. We change it from localhost to IP of Fabric Host (change to yours).

sed -i 's/localhost/' connection-org1.json

Step 7: Update /etc/hosts to reach Fabric Host

Finally we let API Server Host reach Fabric Host by resource names we are using. Here is the result.

Now configuration is ready.

Step 8: Install packages on API Server Host

npm install
npm install express body-parser --save

Step 9: Run API Server

node apiserver.js

As we have kept the console.log in our code, we will see console.log result in apiserver.js when accessing the APIs.

Testing: on my localhost

(screenshots are in red colour background)

Now the API Server is exposing the API we can access the fabcar application. We follow the same test suite in our previous articles.

Query All Cars


Add a new car and query the new car

curl -d '{"carid":"CAR12","make":"Honda","model":"Accord","colour":"black","owner":"Tom"}' -H "Content-Type: application/json" -X POST

Change owner for a car and query that car again

curl -d '{"owner":"KC"}' -H "Content-Type: application/json" -X PUT

The API Server is working well.

Hope you enjoy reading this.