# First we need to import the needed libraries,
# make sure you install the requests library with pip
#("pip3 install json requests" or "python3 -m pip install requests")
import json
import requests
# Now we make our first call to the API to make sure it's online and working
r = requests.get("https://api.whatsonchain.com/v1/bsv/main/woc")
if str(r.content) == ("b'Whats On Chain'"):
print("API is up and running.")
else:
print("API is down.")
# Now let's do something more complicated,
# for example viewing info about the state of the chain
# Making the GET call
r = requests.get("https://api.whatsonchain.com/v1/bsv/main/chain/info")
# The call with return as json, that's why we need to parse the data with the
# json library and format it into readable variables.
data = json.loads(r.content)
# Now the data is parse into the variable: "data"
# Now we can call info from the variable data using [] after the variable name,
# for example: print(data["headers"]) or print(data["chain"])
# now we will print all of the needed info.
print("Chain: " + data["chain"])
print("Block headers: " + str(data["headers"]))
print("Best block hash: " + str(data["bestblockhash"]))
print("Difficulty: " + str(data["difficulty"]))