Author avatar

Matthew Sedaghatfar

Using the Tierion Hash API with Python

Matthew Sedaghatfar

  • Jan 10, 2019
  • 2 Min read
  • 2,496 Views
  • Jan 10, 2019
  • 2 Min read
  • 2,496 Views
Python

How to connect to the Tierion Hash API with Python.

Tierion makes it easy to create applications that record any data or business process in the blockchain. Tierion’s Hash API lets you anchor data to the blockchain while keeping your data private. Use of the Hash API is free up to 3 records per second or 1,000 records per hour.

In this guide we will be using Python 2.7

Import Libaries

1import requests
2import hashlib
python

Authentication

Tierion's Hash API uses a JSON Web Token as an access token for authentication. Every request must include a valid access token.

To obtain your access token, submit your Tierion credentials to the token endpoint.
1apiStem = 'https://hashapi.tierion.com/v1'
2
3login_creds = {
4    'username': '[email protected]',
5    'password': 'yourpassword',
6}
7
8reqToken = requests.post(apiPath + '/auth/token', json=login_creds)
9reqToken.json()
10
11token = reqToken.json()['access_token']
12auth1 = {'Authorization': 'Bearer ' + token}
python

The variable auth1 now contains your access token which is active for one hour.

Submit a string to Hash

Now we would like to send a string to get encrypted using SHA-256

1hashText = hashlib.sha256("Matthew Sedaghatfar Using the BlockChain").hexdigest()
2hashItem = '/hashitems'
3myHash = {'hash': hashText}
python

Then we send this hash to Tierion and get back a receiptId and timestamp.

1respHash = requests.post(apiStem + hashItem, json= myHash, headers = auth1)
2receipt = respHash.json()['receiptId']
3
4respHash.json()
python

Retreive the receipt

1getReceipts = '/receipts/'
2
3getRec = requests.get(apiStem + getReceipts + receipt,headers = auth1)
4getRe.json()
python

Conclusion

Some use cases of a Hash API is to store predictions or keeping records of Lease aggrements.