In the last blog, we created and deployed our own business network “Trading Card” and tested it on our network. Progressing further on the same, in this blog, we will take a step ahead and learn how to enhance the experience of interaction with the business network by using REST API server. Further ahead, we will read about how to generate a REST API server and use an angular application with it.
What is a REST API Server?
An API is a code program that allows two software programs to communicate with each other. REST API is used by browsers for web services to connect and interact with cloud services. REST (Representational State Transfer Technology) is considered as the language of the internet and with cloud technology on the rise, REST is the most precise choice to connect with cloud services. Also referred as RESTful web service, a REST API is based on the architectural approach that is used by web services development. It takes benefit of HTTP methodologies defined by RFC 2016 protocol, GET retrieves a resource, PUT changes or updates a resource, POST creates a resource and DELETE removes it.
Generating a REST API Server
The application we deployed earlier was on the playground, which is nice but not great. Though, through this user interface, we can configure, test and manage the security of a business network but it is not an optimal solution to provide the best user interface for business users. Playground is for tech savvy people, but not for generic users, who are not aware of nuances of underlying blockchain technology.
The UI experience can be enhanced and made seamless by using composer-rest-server module.
Before generating a REST API server, let us have a look at configuring REST server using environment variables.
It is used to specify discovery business card that REST server should use to connect to business network.
This environment variable specifies if a REST server should generate a REST API with namespaces or not.
It helps enabling or disabling REST server authentication. The acceptable values are true and false.
With the help of this environment variable REST server can toggle between enabling and disabling multiple user mode.
This aids in defining passport strategies that must be used by REST server to authenticate clients to REST API.
This specifies LoopBack data sources and LoopBack connector information.
Using this environment variable the certificate file can be specified that can be used while using HTTPS and TLS.
This specifies private key that should be used by REST server when HTTPS and TLS are enabled.
Composer_TLS environment variable gives direction to REST server to enable/disable TLS and HTTPS
The environment variable specifies the API key that should be acceptable by server for each request.
Setting this environment variable to “true” will help you enable the REST server to publish the event.
Let us get started and type composer-rest-server in the terminal and specify the network that you have created, in our case it is admin@cards-trading-network. Next, select “never use namespaces” and go ahead with the default options.
Now, you can access automatically generate REST API by opening http://localhost:3000/explorer/ in the browser. This will open a documented version of the REST API.
Generating an Angular Application Which Uses the REST API
We have worked previously with yo hyperledger-composer, and we will have to use the same command to create a skeleton angular application.
The flow using an angular application is as below:
Start with typing yo hyperledger-composer in the terminal, and choose to select your business network to connect to an existing business network using card admin@cards-trading-network. Once the connection is successful, connect to the existing REST API.
The yo hyperledger-composer command will run npm Install and npm start. The process should take a minute or so and once completed.
In the process of installation make sure to enter correctly when the command composer-rest-server is executed. A sample of same is provided in above screenshot, which you can refer to match your output.
- Specify your network, in our case it was admin@cards-trading-network
- When prompted for namespaces give “Namespaces are not used”
- Select “No” when prompted to secure the generated API.
- Enable event publication by responding “Yes” to prompt.
- You can disable security at this stage of application build by selecting “No” when prompted to enable TLS security.
The REST server will be generated and available on local host http://localhost:4200/ will be loaded.
We were greeted with the below page, you should be greeted something similar with the details of your application that you have mentioned in the above steps.
Now the application will be up and running and you can access your network from this newly built application. This app will communicate with the network through REST server running on port 3000.
You’ve just completed setting up your first blockchain business app using Hyperledger fabric and Hyperledger composer.
More features can be added to your trading network and more transactions can be performed which allow traders to toggle the value of forTrade. This can be integrated with non-blockchain applications and users can buy new cards which can be used for future trades. You can add/update hundreds of such requirements.
In the next blog, we will discuss some issues which you might face during development and how to resolve them.
Let us know in the comments down below what features you are planning to add to make your application more effective. Happy Coding! 🙂