Build Blockchain Applications in Hyperledger Fabric

Build Blockchain Applications in Hyperledger Fabric

Recipe ID: hsts-r27

Self-paced training

We offer blockchain introduction, Hyperledger for system admin, Ethereum, Solidity, Corda R3, Hyperledger for developers, blockchain cybersecurity and more classes in self-paced video format starting at $60. Click here to learn more and register. For complete self-paced blockchain training, visit our Complete Blockchain Development Training page.

Recipe Overview

Hyperledger Fabric is an open source enterprise-grade platform that leverages a highly-modular and configurable architecture. Hyperledger Fabric is optimized for a broad range of industry use cases, including the finance, banking, healthcare, insurance, and public sectors, as well as supply chains and digital asset management.
For those who are not familiar with Hyperledger project Intro to Hyperledger Family and Hyperledger Blockchain Ecosystem, Hyperledger Design Philosophy and Framework Architecture, The Survey of Hyperledger Fabric Architecture and Components for Blockchain Developers and Overview of Building Blockchain Smart Contracts in Hyperledger articles are strongly recommended.
Hyperledger Fabric supports Smart Contact development in general-purpose programming languages, such as JavaScriptJava, Go, and Node.js. Hyperledger Fabric is also operating under a governance model to build trust between participants on a shared network.
In our previous recipes, we have successfully installed Hyperledger Fabric on an AWS EC2 virtual machine and set up the first Hyperledger Fabric network. We learned the following:

We also learned how to Add New Network to a Channel and Use CouchDB as a State Database for Hyperledger Fabric. In this recipe, we will explore how to create a smart contract and then deploy it into the blockchain.
To run this recipe, you need to have completed the Installing Hyperledger Fabric on AWS as well as installing Hyperledger Fabric with samples and binaries on the AWS EC2 instance.

Write Blockchain Application

To write your first application, follow these steps:
1. Set up the development environment:

$ cd ~

cd fabric-samples/first-network

sudo docker ps

sudo ./ down

sudo docker rm -f $(sudo docker ps -aq)

sudo docker network prune

cd ../fabcar && ls


You will notice that there are a few Node.js file present in fabcar folder such as enrollAdmin.js, invoke.js, query.js, registerUser.js, and package.json and all others packaged into one file.

2. Install the Fabric client:

sudo npm install -g npm@5.3.0

 sudo npm update

You will notice from the following screenshot that the Fabric client 1.3.0 and Fabric CA client 1.30 packages are installed:
Hyperledger Fabric and blockchain applications


3. Execute the following command to launch the network:

sudo ./ node

4. Open a new Terminal to stream the Docker logs:

$ sudo docker logs -f

This will open the Docker file, which will look similar to the following screenshot:
Hyperledger Fabric and blockchain applications


Next, we will use the Node.js script to run, query, and update the records on Fabric network.


Accessing the API with SDK
In this step, the application uses an SDK to access the APIs that permit queries and updates to the ledger. Now we will perform the following steps:

1. Enroll an admin user with the enrollAdmin.js script:

$ sudo node enrollAdmin.js
When we launch the network, an admin user needs to be registered with certificate authority. We send an enrollment call to the CA server and retrieve the enrollment certificate (eCert) for this user. We then use this admin user to subsequently register and enroll other users:
Hyperledger Fabric and blockchain applications


2. Register and enroll a user called user1 using the registerUser.js script:

sudo node registerUser.js

 3. With the newly-generated eCert for the admin user, let's communicate with the CA server once more to register and enroll user1. We can use the ID of user1 to query and update the ledger:



4. Let's run a query against the ledger:Hyperledger Fabric and blockchain applications

sudo node query.js

5. It returns the following screenshot. You will find that there are 10 cars on the network, from CAR0 to CAR9. Each has a color, doctype, make, model, and owner:
Hyperledger Fabric and blockchain applications


6. The following chaincode constructs the query using the queryAllCars function to query all cars:

queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],

queryAllCars chaincode function - requires no arguments, ex: args: [''],

const request = {

//targets : --- letting this default to the peers assigned to the channel

chaincodeId: 'fabcar',

fcn: 'queryAllCars',

args: ['']


7. Update the ledger. To do this, we will update the invoke.js script. This time, the fabcar chaincode uses the createCar function to insert a new car, CAR10, into the ledger:

var request = {

//targets: let default to the peer assigned to the client

chaincodeId: 'fabcar',

fcn: 'createCar',

args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],

chainId: 'mychannel',

txId: tx_id


sudo node invoke.js

Here we will complete the transaction when CAR10 is created.

8. Execute a query to verify the changes made. Change query.js using the queryCar function to query CAR10:

var request = {

//targets: let default to the peer assigned to the client

chaincodeId: 'fabcar',

fcn: 'queryCar',

args: ['CAR10'],

chainId: 'mychannel',

txId: tx_id


9. Run query.js again. We can now extract CAR10 from the ledger with the response as

{"color":"Red","docType":"car","make":"Chevy","model":"Volt","o wner":"Nick"}:

sudo node query.js

This will result in the following query:
Hyperledger Fabric and blockchain applications


10. Shut down the Fabric network:

sudo docker stop $(sudo docker ps -a -q) sudo docker rm $(sudo docker ps -a -q) sudo docker ps

We have gone through the steps to query and update the transaction using smart contract chaincode. Now, let's see how it works under the hood.

Put Things Together
This concludes the recipe to create and deploy your first smart contract chaincode.

In the previous steps, we used query.js to query the key-value pair store. We can also query for the values of one or more keys, or perform complex searches on JSON data-storage formats. The following diagram shows how the query works:
Hyperledger Fabric and blockchain applications


The following is a representation of different functions in chaincode, which explains that we should first define the code functions to all the available APIs in the chaincode interface:
Hyperledger Fabric and blockchain applications

The following diagram shows the process of updating the ledger. Once an update to the ledger is proposed and endorsed, it will be returned to the application, and will in turn send the updated ledger to be ordered and written to every peer's ledger:
Hyperledger Fabric and blockchain applications

We learned how to write a small smart contract chaincode on the Fabric network to perform a transaction data query and update. In the next recipe, you will learn how to write an end-to-end Hyperledger Fabric application using all that we have learned so far.

The following recipes are excellent resources for installing other Hyperledger tools:
Blockchain Developer Guide- Overview of Hyperledger Explorer and its Development Environment
Blockchain Developer Guide- How to Install Hyperledger Indy and Indy CLI on AWS
Blockchain Hyperledger Composer Business Network Modeling and Environment Setup
Blockchain Developer Guide- How to Install Hyperledger Iroha on AWS
Blockchain Developer Guide- How to Install Hyperledger Burrow on AWS

To conclude this recipe, we like to recommend our Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs and Blockchain Hyperledger Development in 30 hours courses to those interested in pursuing a blockchain development career. Indeed, as of this writing, Hyperledger Foundation offers the following two Hyperledger certifications: The Certified Hyperledger Fabric Administrator (CHFA) and The Certified Hyperledger Sawtooth Administrator (CHSA), both of which are highly regarded in the industry. Hyperledger Foundation is in the process of creating Hyperledger Developer certification program, which may be released in early or middle of 2020. In short, by taking our hands-on online Hyperledger class, you would be able to obtain CHFA certification.

This tutorial is written by Brian Wu who is our senior Hyperledger instructor in Washington DC. His Hyperledger Cookbook with 40+ hands-on recipes is highly recommended.

Related Training Courses

Hands-on Node.JS, MongoDB and Express.js Training
Advance JavaScript, jQuery Using JSON and Ajax
Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs
Learn Blockchain Hyperledger Development & Get Certified in 30 Hrs
Become Blockchain Certified Security Architect in 30 hours
Blockchain Certified Solution Architect in 30 hours
Introduction to Python Programming
Object Oriented Programming with UML

Private and Custom Tutoring

We provide private tutoring classes online and offline (at our DC site or your preferred location) with custom curriculum for almost all of our classes for $50 per hour online or $75 per hour in DC. Give us a call or submit our private tutoring registration form to discuss your needs.

View Other Classes!