Skip to content Skip to footer

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="For blockchain to be considered a secure platform for business in today’s global marketplace, it must be created with global sustainability in mind. As an eco-friendly decentralized ledger technology (DLT), LYNX strives to solve this problem. LYNX is a decentralized blockchain that prioritizes environmental sustainability and ease-of-use for the everyday, non-technical user. Unlike Bitcoin’s business rules which promote a consolidated, competitive, inefficient, and profit-driven mining strategy that create an over-reliance on fossil fuels, LYNX business rules promote the opposite. The Lynx code discourages high-volume mining rigs because the code purposefully lacks incentives to mine it for profit. In addition, the entire LYNX network is designed to operate on a collaboration of low power devices that anyone can run, resulting in a collective global mining cost of only dollars a day. This includes mining the coin and confirming transactions. As a result, LYNX is more user-friendly to mine and more eco-friendly than other blockchain projects that have unsustainable energy requirements. And, with an abundance of coins yet to be mined, the LYNX network can be sustained for thousands of years."
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' -H "Authorization: Bearer $TOKEN" --data '{"env": "'"$apiEnvironment"'", "txid": "'"$TXID"'"}' https://api.logware.io/api/data/retrieve)
READ="${READ%\"}" && READ="${READ#\"}" # Strip quotes from body
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' -H 'Content-Type: application/json' -H "Authorization: Bearer $TOKEN" https://api.logware.io/api/data/retrieve/$apiEnvironment/${READ} | jq -r '.Content[0]')
	echo "DATA returned is '$DATA'"
	sleep 5
done
#