Hyperledger Composer NodeJS SDK - Submiting Transactions

## Create Asset Definition

As the first step, let’s create an asset say “Car” in our example, which has set of properties. Define them in your model file of composer (.cto)

asset Car identified by id {
  o String id
  o Integer name
  --> User owner

The above defined asset Car has three properties named id, name, owner. And in order to create a Car we need to have a transaction, thus define a transaction model as below.

transaction addNewCard {
  o String id
  o String name

As you can notice, here we’re not mentioning the owner while creating the car, since we’ll be using the getCurrentParticipant() method inside transaction processor function to get the current user and define the owner.

 * A transaction to send good to an organization
 * @param {org.acme.addNewCard} name A human description of the parameter
 * @transaction
function  addNewCard(transaction) {
  var newCard;
  var factory = getFactory();
  var cId = getRandomId();
  var owner = getCurrentParticipant();
  return getParticipantRegistry("org.acme.Car").then(function(carRegistry) {
    newCar = factory.newResource("org.acme", "Car", cId);
    newCar.name = transaction.name;
    newCar.owner = transaction.owner;
    return carRegistry.add(newCar);

Now since we’ve everything in our network, let’s update the network with the following commands.

composer archive create -t dir -n .
composer network update -a acme-network@0.0.1.bna -c admin@acme-network

Creating EndPoint For Transaction

Next up is to create an endpoint for transaction submission. We do it with express js as follows.

app.post('/api/createCard', function (req, res) {
  var transactionData = req.body.transactionData;
  var cardName = req.headers.authorization;
  var mynetwork = new MyNetwork(cardName);
  mynetwork.init().then(function () {
      return mynetwork.createCar(transactionData)
  }).then(function () {
      res.json({ success: true })
  }).catch(function (error) {
      res.status(500).json({error: error.toString()})    

This is very similar to the ping method, we’re getting the transaction data from the request and passing it to createCar method in our MyNetwork Class.

createCar(transactionData) {
    var _this = this;
    var resource;
    var transactionData;
    transactionData['$class'] = "org.acme.createCar";
    return this.connection.getTransactionRegistry("org.acme.createCar")
      .then(function(createProductTransactionRegistry) {
        serializer = _this.businessNetworkDefinition.getSerializer()
        resource = serializer.fromJSON(transactionData);
        return _this.connection.submitTransaction(resource);

This returns a null if the transaction is submitted successfully, else catch block is called with the captured error.

The only thing to have in mind is that we need to specify the $class in transactionData and convert it to resource format from JSON before passing it to submitTransaction function.

This is a companion discussion topic for the original entry at https://www.skcript.com/svr/hyperledger-composer-nodejs-sdk-submiting-transactions/

Thank you for this post. I also implemented this but when I declare a Transaction that has a relationship, I also forced to use in my script the method for declare a relationship (factory.newRelationship()). Can I avoid this with the serializer object?

Hi @mespejo,

The relationship in the sense, nested data kinds? you can do that just by passing the unique identifier right?

Hi @varun,

 I want to call java method to hyperledger composer (instead of saving the data in my local database i want to save the data in blockchain) . Could you please guide me how to do that call?


You need to create a Client with Java SDK for communicating with Hyperledger Fabric Network and access the ledger.

Hi, is it okay to wait until the createcar is responded and give a success message from middleware as it might take time.

You mean while summiting a transaction?