![]() |
VOOZH | about |
REST, or Representational State Transfer, is an architectural style for building web services. It is based on a set of principles that define how web resources should be defined, accessed, and manipulated. One of the key principles of REST is the use of the HTTP protocol for communication between clients and servers. This means that REST APIs are based on the same principles as the web itself, which makes them easily accessible and widely supported. The article focuses on discussing REST API in Hyperledger.
The following topics will be discussed here:
Let's start discussing each of these topics in detail.
REST API allows client applications to interact with a Hyperledger blockchain network. This allows the client application to perform operations on the blockchain, such as querying the state of the ledger, submitting transactions, or querying the history of a specific asset on the ledger. It makes it possible for developers to build applications that can access and manipulate data on the blockchain in a convenient and standardized way.
REST APIs are used for a wide variety of purposes, including:
REST APIs are particularly useful for building web-based applications that need to access data or functionality from other services, such as a mobile app that retrieves data from a server, or a web-based tool that integrates with other services to perform a specific task.
Overall, REST APIs are an important tool for building modern web applications and enabling communication between different systems and platforms.
To install the REST server, first, make sure that the Hyperledger platform is installed on the system.
1. Once Hyperledger is installed, use the npm package manager to install the fabric-rest package, which provides a REST server for the Hyperledger platform. To do this, run the following command:
npm install fabric-rest
2. After the package has been installed, use the fabric-rest command to start the REST server. Provide the configuration settings for the server, such as the location of your Hyperledger network and the credentials for accessing it.
1. Once the package is installed, use the fabric-rest command to start the server. This command takes a number of options and arguments, which can be used to configure the server and specify the location of your Hyperledger network and the credentials for accessing it.
Here is an example of how to start the REST server with the fabric-rest command:
fabric-rest --port 3000 --network ./my-hyperledger-network --keystore ./keystore --cacert ./cacert.pem
2. This command will start the REST server on port 3000, using the specified Hyperledger network, Keystore, and CA certificate. One can access the server by making HTTP requests to the specified port on your local machine.
1. Once it is confirmed that the fabric-rest package is installed, update it to the latest version by running the following command:
npm update fabric-rest
This will update the fabric-rest package to the latest version that is available on the npm registry.
2. After the update is complete, use the fabric-rest command to start the REST server and use it as usual.
In REST APIs, the most common methods that are used are:
These methods are typically used in combination with a URL that specifies the location of the resource that is being accessed or modified.
Example:
GET request to the URL https://example.com/users/1 might retrieve the data for a user with id 1 from the server
POST request to the same URL might create a new user with id 1.
In addition to these common methods, some APIs may also support other methods, such as PATCH for partially updating a resource, or HEAD for retrieving only the headers of a response. The specific methods and their uses will depend on the design of the API.
Below are two examples of implementing code for REST API:
Example 1: Let's consider, the client application might make a request to the REST API to query the current state of the ledger. The REST API would then communicate with the blockchain network, retrieve the relevant information from the ledger, and return it to the client application in a format that the client can understand, such as JSON.
GET /ledger HTTP/1.1
Host: blockchain.example.com
Content-Type: application/json
In this case, the REST API would communicate with the blockchain network, retrieve the current state of the ledger, and return it to the client in a JSON format, like this:
HTTP/1.1 200 OK
Content-Type: application/json{
"ledger": {
"version": 1,
"assets": [
{
"id": "asset1",
"owner": "Alice",
"value": 100
},
{
"id": "asset2",
"owner": "Bob",
"value": 200
}
]
}
}
In this example, the client application has queried the current state of the ledger and received a response containing information about the assets on the ledger and their current owners.
In summary, a REST API allows client applications to interact with a Hyperledger blockchain network in a convenient and standardized way. This makes it possible for developers to build applications that can access and manipulate data on the blockchain in a straightforward and efficient manner
Example 2: Here is another example of how a client application might use a REST API to interact with a Hyperledger blockchain network.
POST /transactions HTTP/1.1
Host: blockchain.example.com
Content-Type: application/json{
"transaction": {
"assetId": "asset1",
"newOwner": "Carol"
}
}
In this case, the REST API would receive the transaction request from the client, validate the transaction according to the rules of the blockchain network, and then submit the transaction to the ledger if it is valid. The REST API would then return a response to the client indicating the status of the transaction, like this:
HTTP/1.1 200 OK
Content-Type: application/json{
"status": "success",
"message": "Transaction successfully submitted to the ledger"
}
In this example, the client application has submitted a transaction to the ledger to transfer ownership of an asset from one user to another. The REST API has received the transaction, validated it, and then submitted it to the ledger. The REST API has then returned a response indicating that the transaction was successful.
In summary, a REST API allows client applications to interact with a Hyperledger blockchain network and submit transactions to the ledger in a convenient and standardized way. This makes it possible for developers to build applications that can access and manipulate data on the blockchain in a straightforward and efficient manner.