An Implementation of API Server for Hyperledger Fabric Network



Setup the Fabric Node

cd fabric-samples/fabcar
cd javascript
npm install
node enrollAdmin.js
node registerUser.js
ls wallet/user1

API Server: Design

Setup the API Server Node

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
mkdir apiserver
cd apiserver
# localhost (update your own IP of the two servers)# temp is an empty directory
cd temp
scp -i ~/Downloads/aws.pem ubuntu@[Fabric-Node-IP]:/home/ubuntu/fabric-samples/first-network/connection-org1.json .scp -i ~/Downloads/aws.pem ubuntu@[Fabric-Node-IP]:/home/ubuntu/fabric-samples/fabcar/javascript/package.json .scp -r -i ~/Downloads/aws.pem ubuntu@[Fabric-Node-IP]:/home/ubuntu/fabric-samples/fabcar/javascript/wallet/user1/ .scp -r -i ~/Downloads/aws.pem * ubuntu@[API-Server-Node-IP]:/home/ubuntu/apiserver/
cd apiserver
mkdir wallet
mv user1 wallet/user1
sed -i 's/localhost/[Fabric-Node-IP]/g' connection-org1.json localhost
npm install
npm install express body-parser --save
node apiserver.js

Access the API

curl http://[API-Server-Node-IP]:8080/api/queryallcars
Here we see the 10 preloaded car records in the ledger
curl -d '{"carid":"CAR12","make":"Honda","model":"Accord","colour":"black","owner":"Tom"}' -H "Content-Type: application/json" -X POST http://[API-Server-Node-IP]:8080/api/addcarcurl http://[API-Server-Node-IP]:8080/api/query/CAR12
We see the newly added car record in ledger.
curl http://[API-Server-Node-IP]:8080/api/query/CAR4curl -d '{"owner":"KC"}' -H "Content-Type: application/json" -X PUT http://[API-Server-Node-IP]:8080/api/changeowner/CAR4curl http://[API-Server-Node-IP]:8080/api/query/CAR4
We see the owner of a specified car updated in the ledger.





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store