Skill Level: Any Skill Level

This recipe shows you step-by-step how to run and test chaincodes or smart contracts in Hyperledger Fabric. You also learn how to use Fabric Command Line Interface for managing smart contracts.


To follow and complete this recipe, you need to have good knowledge of blockchain, Hyperledger transactions, JavaScript as well as basic knowledge of Linux.


  1. Hyperledger Fabric Overview and Recipe Outline

    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 JavaScript, Java, Go, and Node.js. Hyperledger Fabric is also operating under a governance model to build trust between participants on a shared network.
    We have written two sets of tutorials to explore Hyperledger Fabric in depth. First set covered the following six recipes:
    It started with installing Hyperledger Fabric on an AWS EC2 virtual machine,  setting up the first Hyperledger Fabric network and working with Hyperledger Fabric Command Line Interface or CLI. We learned the following:

    • Generating the crypto/certificate using cryptogen
    • Generating the configuration transaction using configtxgen
    • Bring up the nodes based on what is defined in the docker-compose file
    • Using the CLI to set up the first network
    • Using the CLI to install and instantiate the chaincode
    • Using the CLI to invoke and query the chaincode

    We moved on to show you how to Add New Network to a Channel, Use CouchDB as a State Database for Hyperledger Fabric, and Create a Smart Contract and then Deploy it into the Blockchain.
    In short, in the previous recipes, we learned about how to set up and configure Hyperledger Fabric. We explored its key components, including channels, Membership Service Providers (MSPs), the ordering service, and Fabric Certificate Authority (CA).
    The second set, we will show you how to build a simple device asset management DApp. It consists of 6 recipes as follows:

    In summary, in the second set of recipes, we are going to build a simple device asset management DApp. We will exploit this example by writing chaincode implemented by various programming languages and we’ll also build, test, and deploy our DApp.

    IMPORTANT: Understanding and completing the first set of recipes are required prior to working on second set of recipes.

    In our last recipe, we have opened two Terminals so far and deployed the chaincode to the peer node. It is time to install and test our chaincode function.

  2. Work with Chaincode Container via CLI

    Open the third container to issue the CLI command and test our smart contract. In this Terminal, we will start the CLI container and issue the CLI command to interact with the chaincode container. Launch an example CLI container as follows:

    docker exec -it cli bash

    Installing Assermgr Chaincode
    Install the assermgr chaincode through the CLI container by running the following command:

    peer chaincode install -p chaincodedev/chaincode/assetmgr -n mycc -v 0

    Here is the result after the chaincode is installed:
    Hyperledger Fabric Smart Contracts


    Instantiating Assermgr Chaincode
    Next, we will instantiate the assermgr chaincode. As we discussed earlier, in order to create an asset record, we need to pass assetId, assetType, and deviceId. Let’s assume that the school needs to trace an ipad with the 0e83ff device ID and the 100 asset ID. We can instantiate our ipad asset by running the following command:

    peer chaincode instantiate -n mycc -v 0 -c ‘{“Args”:[“100″,”ipad”, “0e83ff”]}’ -C myc

    The result is as follows:
    Hyperledger Fbaric


    We have now successfully installed and instantiated our assetmgr chaincode.


    Invoking Assermgr Chaincode

    Next, we can start to invoke the remaining chaincode methods: Order, Ship, and Distribute.

    1. To order the device from OEM, we need to pass three parameters to the chaincode—assetId, Comment, and Location. Here, assetId is 100, and we will assume that Location is New York.

    2. Now, issue invoke to call the Order method in the assetmgr chaincode. The command is as follows:

    peer chaincode invoke -n mycc -c ‘{“Args”:[“Order”, “100”, “initial order from

    school”, “New York”]}’ -C myc

    3. If all goes well, you should see the following result. The log shows that the chaincode has been invoked successfully. We can see that the result is successfully saved to blockchain:

    Hyperledger Fabric smart contracts


    4. In our assetmgr, we have defined a query method. We can invoke this method to verify whether the records have been saved in the Fabric blockchain. Issue the following query command with assetId as 100:

    peer chaincode query -C myc -n mycc -c ‘{“Args”:[“query”,”100″]}’

    We can find the asset with an assetId of 100 from the Fabric ledger:
    Hyperledger Fabric smart contracts


    5. Once the OEM receives the order, it starts to work and produce the iPad device. Then, the OEM ships the device to the school. To do this, issue the following Ship command with assetId, Comment, and Location:

    peer chaincode invoke -n mycc -c ‘{“Args”:[“Ship”, “100”, “OEM deliver ipad to school”, “New Jersey”]}’ -C myc
    The following screenshot will be the output of the previous code:
    Hyperledger Fabric smart contracts


    6. Once the device is received, the school will distribute the device to the student. Issue the following Distribute command with assetId, Comment, and Location:

    peer chaincode invoke -n mycc -c ‘{“Args”:[“Distribute”, “100”, “Distribute device to student”, “New York”]}’ -C myc

    We should see the following result:

    Hyperledger Fabric chaincode


    7. We have now completed the entire process for our demo use case. As we discussed earlier, blockchain is a ledger system; it will keep track of all transactions. Once records are saved to the blockchain, they cannot be altered. We should be able to see this historical transaction data. In our asset manager example, we issued the Order, Ship, and Distribute commands and the related chaincode was invoked. All related asset transaction records should be kept in the blockchain. Let’s verify this by issuing the getHistory command:

    peer chaincode query -C myc -n mycc -c

    This command will provide the following results:
    Hyperledger fabric chaincode


    As we can see, the getHistory command returns all of the transaction records we invoked from Fabric blockchain.

  3. Put Things together

    The Fabric command-line interface is built using Fabric SDK Go. The CLI provides various commands to run a peer node, interact with the channel and the chaincode, and to query blockchain data. Here are some functionalities provided by the CLI:



    Example command


    Creates a channel

    peer channel join -b myc.block


    Joins a peer to a

    peer channel join -b myc.block






    Installs chaincode

    peer chaincode install -p



    chaincodedev/chaincode/assetmgr -n mycc -v 0



    peer chaincode instantiate -n mycc -v 0 –c



    ‘{“Args”:[“100″,”ipad”, “0e83ff”]}’ -C myc


    Invokes the

    peer chaincode invoke -n mycc -c


    ‘{“Args”:[“Order”, “100”, “initial order from


    chaincode function


    school”, “New York”]}’ -C myc





    Queries chaincode

    peer chaincode query -C myc -n mycc -c




    With these supported CLI commands, we can test our chaincode in the development environment.

    Next, we will write client-side code and interact with the assetmgr chaincode in Hyperledger Fabric.

    This recipe is written in collaboration with Brian Wu who is a senior Hyperledger instructor at Coding Bootcamps school in Washington DC.

Join The Discussion