How to start writing your Hyperledger Fabric Nodejs chaincode

Writing chaincode shouldn’t be a hassle. Luckily Hyperledger Fabric has introduced Nodejs chaincode in v1.1.0 (official release should be out by the end of this month). Compared to Golang, this is a language more people understand and find easier to use, Including us. To be honest, I got to like Golang (when I understood it eventually). But in my opinion, it’s much more of an overhead to write chaincode than Nodejs. We made the switch when we couldn’t access the ABAC (cid) feature in Golang because the dependency of this package didn’t want to be resolved correctly.

All examples are written in Typescript since all of our packages are also developed in typescript. But by removing the types, you can just use it in your non-typescript application as well. We recommend using typescript because, without typings, your chaincode could more easily introduce bugs.


We created a package which contains a set of helper classes in order for you to write code faster. Compared to Golang, your functions can be reduced from 10 lines to 4.

yarn add @theledger/fabric-chaincode-utils


The Chaincode class is a base class containing handlers for the Invoke() and Init() functions which are required by fabric-shim. The Init() function can be overwritten by just implementing it in your MyChaincode class, this function should be called init and will also be injected with the same arguments as your custom functions.

export class MyChaincode extends Chaincode {

The Chaincode base class also implements the Invoke() method. It will search in your class for existing chaincode methods with the function name you sent. It will also automatically wrap and serialize the response with shim.success()and shim.error(). You can just return the javascript object and it will do the rest, BUT returning a Buffer is still supported as well. So for example, if we invoke our chaincode with function queryCar, the function below will be executed.

export class MyChaincode extends Chaincode {

async queryCar(stubHelper: StubHelper, args: string[]) {
      // You can just return a javascript object
return car;
      // Or you can serialize it yourself
return Buffer.from(JSON.stringify(car));



The StubHelper is a wrapper around the fabric-shim stub. It’s a helper to automatically serialize and deserialize data being saved/retrieved. The full API can be found here.


The Transform class is a helper to provide data transformation to and from the formats required by Hyperledger Fabric. The full API can be found here.

Writing chaincode

Writing chaincode isn’t that hard in Nodejs. In the next examples, we’ll be documenting how to write chaincode with our package. We’ll be using the fabcar example for this.

Checking arguments

We wrote a helper function to easily check arguments using Yup. Besides checking arguments, it can also parse your arguments from string to your required data format. It will automatically parse the array of string arguments to the object schema in the order the schema is in. So, arg[0] would be mapped to key , arg[1] to make and so on.

const verifiedArgs = await Helpers.checkArgs(args, yupSchema);

An example validation would look like this. The reason we went with yup is because it’s easily extendible. More documentation can be found on the Yup’s Github.

key: Yup.string().required(),
make: Yup.string().required(),
model: Yup.string().required(),
color: Yup.string().required(),
owner: Yup.string().required(),

Interaction with Fabric

Like mentioned above, the StubHelper will allow you to easily interact with the state of Fabric. This wrapper will allow you to write chaincode without having to handle serialization and deserialization. This makes your code cleaner and lets you focus on the application. The full API can be found here or in the README.


A complete example of a chaincode implementation can be found here.

Query a car

Query by range

Query using rich queries (more info about rich queries here)

Creating new objects

Updating objects

Interested in starting your own blockchain project, but don’t know how? Do you need help starting your token sale or having one audited? Get in touch with TheLedger.


We demonstrated here, how easy it is to write chaincode using our package. You don’t have to, but it speeds things up quit a bit. These examples can help you get started, if you want to read our documentation, you can checkout our github. We would also encourage you to contribute and make this package better. 👌

Where to go next

Curated list of Hyperledger Fabric resources

Our Hyperledger Fabric REST server Typescript boilerplate

Network boilerplate including Nodejs chaincode example

— — — — — — — — — — — — — — — — — — — — — — — — — — — — -

NEXT ARTICLE IN THIS SERIESHow to start testing your Hyperledger Fabric Nodejs chaincode