fabric-samples/asset-transfer-basic/rest-api-typescript
James Taylor f1a9fea77d Fix lint errors
Signed-off-by: James Taylor <jamest@uk.ibm.com>
2021-12-14 14:31:23 +00:00
..
scripts Improve Docker support 2021-12-14 14:31:21 +00:00
src Fix lint errors 2021-12-14 14:31:23 +00:00
.dockerignore docker file for app 2021-12-14 14:31:17 +00:00
.env.sample Improve Docker support 2021-12-14 14:31:21 +00:00
.eslintrc.json Fix dist output 2021-12-14 14:31:15 +00:00
.gitignore Initial REST API skeleton 2021-12-14 14:14:56 +00:00
.prettierrc.json Initial REST API skeleton 2021-12-14 14:14:56 +00:00
demo.http Update api key header 2021-12-14 14:31:13 +00:00
docker-compose.yaml Improve Docker support 2021-12-14 14:31:21 +00:00
Dockerfile Improve Docker support 2021-12-14 14:31:21 +00:00
jest.config.ts Add config spec tests 2021-12-14 14:31:20 +00:00
package-lock.json Initial API tests 2021-12-14 14:31:21 +00:00
package.json Improve Docker support 2021-12-14 14:31:21 +00:00
README.md Update readmes 2021-12-14 14:31:23 +00:00
tsconfig.json Fix dist output 2021-12-14 14:31:15 +00:00

Asset Transfer REST API Sample

Prototype sample REST server to demonstrate good Fabric Node SDK practices

The primary aim of this sample is to show how to write a long running client application using the Fabric Node SDK

The REST API is intended to work with the basic asset transfer example

To install the basic asset transfer chaincode on a local Fabric network, follow the Using the Fabric test network tutorial

Usage

Note: these instructions should work with the release-2.2 branch of fabric-samples but later versions require some changes

To build and start the sample REST server, you'll need to download and install an LTS version of node

Clone this repository and change to the fabric-rest-sample/asset-transfer-basic/rest-api-typescript directory before running the following commands

Install dependencies

npm install

Build the REST server

npm run build

Create a .env file to configure the server for the test network (make sure TEST_NETWORK_HOME is set to the fully qualified test-network directory)

TEST_NETWORK_HOME=$HOME/fabric-samples/test-network npm run generateEnv

Start a Redis server

npm run start:redis

Start the sample REST server

npm run start:dev

Docker image

Alternatively, run the following commands in the fabric-rest-sample/asset-transfer-basic/rest-api-typescript directory to start the sample in a Docker container

Build the Docker image

docker build -t fabric-rest-sample .

Create a .env file to configure the server for the test network (make sure TEST_NETWORK_HOME is set to the fully qualified test-network directory and AS_LOCAL_HOST is set to false so that the server works inside the Docker Compose network)

TEST_NETWORK_HOME=$HOME/fabric-samples/test-network AS_LOCAL_HOST=false npm run generateEnv

Start the sample REST server and Redis server

docker-compose up -d

REST API

If everything went well, you can now make basic asset transfer REST calls!

The examples below require a SAMPLE_APIKEY environment variable which must be set to an API key from the .env file created above.

For example, to use the ORG1_APIKEY...

SAMPLE_APIKEY=$(grep ORG1_APIKEY .env | cut -d '=' -f 2-)

Get all assets...

curl --header "X-Api-Key: ${SAMPLE_APIKEY}" http://localhost:3000/api/assets

Check whether an asset exists...

curl --include --header "X-Api-Key: ${SAMPLE_APIKEY}" --request OPTIONS http://localhost:3000/api/assets/asset7

Create an asset...

curl --include --header "Content-Type: application/json" --header "X-Api-Key: ${SAMPLE_APIKEY}" --request POST --data '{"id":"asset7","color":"red","size":42,"owner":"Jean","appraisedValue":101}' http://localhost:3000/api/assets

Read transaction status...

curl --header "X-Api-Key: ${SAMPLE_APIKEY}" http://localhost:3000/api/transactions/__transaction_id__

Read an asset...

curl --header "X-Api-Key: ${SAMPLE_APIKEY}" http://localhost:3000/api/assets/asset7

Update an asset...

curl --include --header "Content-Type: application/json" --header "X-Api-Key: ${SAMPLE_APIKEY}" --request PUT --data '{"id":"asset7","color":"red","size":11,"owner":"Jean","appraisedValue":101}' http://localhost:3000/api/assets/asset7

Transfer an asset...

curl --include --header "Content-Type: application/json" --header "X-Api-Key: ${SAMPLE_APIKEY}" --request PATCH --data '[{"op":"replace","path":"/owner","value":"Ashleigh"}]' http://localhost:3000/api/assets/asset7

Delete an asset...

curl --include --header "X-Api-Key: ${SAMPLE_APIKEY}" --request DELETE http://localhost:3000/api/assets/asset7