Sample BASH script of reading and writing data to the Lynx blockchain via the Logware API

The following code can be copy/pasted into a .sh file on your Linux command line. You must update the apiUsername and apiPassword with your own credentials (which you can get here). Be sure to make the file executable.

#!/bin/bash
#
echo "Logware - Demo of reading and writing data to the Lynx blockchain via the Logware API with Linux BASH scripting."
#
# In order to access the API, please contact the Logware team
# at https://logware.io/#contact
#
apiUsername="{Update me with a Username}"
apiPassword="{Update me with a Password}"
#
# By default, use of the test environment is free. Understand that at any time
# the environment can be destroyed and all data will be lost.
#
apiEnvironment="test"
#apiEnvironment="prod"
#
# Support for passing a param via CLI to the script. A default value is set too.
#
content="$1"
[ -z "$1" ] && content="https://www.npr.org/2019/07/23/744582053/senate-approves-bill-to-prevent-sept-11-victims-fund-from-running-out-of-money"
echo "Content being written: '${content}'"
#
# Write section goes to https://api.logware.io/ and requires AUTH.
# Log in and get token
#
TOKEN=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data '{"login":"'"$apiUsername"'","password":"'"$apiPassword"'"}' https://api.logware.io/api/users/authenticate | jq -r '.token')
echo "Logged in as '$apiUsername'"
#
# Now that we have the AUTH token, let's send the write request.
#
WRITE=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer $TOKEN" --data '{"env":"'"$apiEnvironment"'","data":"'"$content"'"}' https://api.logware.io/api/data)
WRITE="${WRITE%\"}" && WRITE="${WRITE#\"}" # Strip quotes from body
echo "WRITE Logware TaskId is '$WRITE'"
echo "Waiting for write to complete..."
#
# Sometimes a delay occurs, so let's wait for write. Now that was have the
# TaskId, we can query for the Lynx Transaction Id for future reference -
# both via Logware or offline directly with Lynx.
#
TXID="null"
while [ "$TXID" = "null" ] ; do
	# Get the results of the write request
	TXID=$(curl -s -H 'Accept: application/json' -H "Authorization: Bearer $TOKEN" https://api.logware.io/api/data/$apiEnvironment/${WRITE} | jq -r '.Content[0]')
	echo "Lynx Transaction Id is '$TXID'"
	sleep 5
done
#
# Read section goes to https://api.logware.io/ and is anonymous. Notice the URL
# is different from the API endpoint for WRITE.
#
READ=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data '{"env": "'"$apiEnvironment"'", "txid": "'"$TXID"'"}' https://api.logware.io/api/data/retrieve)
echo "READ TaskId is '$READ'"
echo "Waiting for read..."
#
# Due to an occasional delay, let's query until we get a response for the
# original data with this READ request.
#
DATA="null"
while [ "$DATA" = "null" ] ; do
	# Get the results of the read request
	DATA=$(curl -s -H 'Accept: application/json' https://api.logware.io/api/data/retrieve/$apiEnvironment/${READ} | jq -r '.Content[0]')
	echo "DATA returned is '$DATA'"
	sleep 5
done
#