blockfrost-python
A Python SDK for Blockfrost.io API.
Getting started •
Installation •
Usage
Getting started
To use this SDK, you first need login into to blockfrost.io create your project to retrieve
your API key.
Installation
$ pip install blockfrost-python
Usage
Using the SDK is pretty straight-forward as you can see from the following examples.
Cardano
from blockfrost import BlockFrostApi, ApiError, ApiUrls
api = BlockFrostApi(
project_id='YOUR API KEY HERE',
base_url=ApiUrls.testnet.value,
)
try:
health = api.health()
print(health)
health = api.health(return_type='json')
print(health)
health = api.health(return_type='pandas')
print(health)
account_rewards = api.account_rewards(
stake_address='stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7',
count=20,
)
print(account_rewards[0].epoch)
print(len(account_rewards))
account_rewards = api.account_rewards(
stake_address='stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7',
count=20,
gather_pages=True,
)
print(account_rewards[0].epoch)
print(len(account_rewards))
address = api.address(
address='addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz')
print(address.type)
for amount in address.amount:
print(amount.unit)
except ApiError as e:
print(e)
IPFS
from blockfrost import BlockFrostIPFS, ApiError
ipfs = BlockFrostIPFS(
project_id='YOUR API KEY HERE'
)
file_hash = None
try:
ipfs_object = ipfs.add('./README.md')
file_hash = ipfs_object.ipfs_hash
print(file_hash)
except ApiError as e:
print(e)
try:
with open('./README_downloaded.md', 'w') as file:
file_data = ipfs.gateway(IPFS_path=file_hash).text
file.write(file_data)
except ApiError as e:
print(e)
Verifying Secure Webhook signature
Webhooks enable Blockfrost to push real-time notifications to your application. In order to prevent malicious actor from pretending to be Blockfrost every webhook request is signed. The signature is included in a request's Blockfrost-Signature
header. This allows you to verify that the events were sent by Blockfrost, not by a third party.
To learn more about Secure Webhooks, see Secure Webhooks Docs.
You can verify the signature using verifyWebhookSignature
function.
Example:
from flask import Flask, request, json
from blockfrost import verify_webhook_signature, SignatureVerificationError
SECRET_AUTH_TOKEN = "SECRET-WEBHOOK-AUTH-TOKEN"
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
request_bytes = request.get_data()
try:
verify_webhook_signature(
request_bytes, request.headers['Blockfrost-Signature'], SECRET_AUTH_TOKEN)
except SignatureVerificationError as e:
print('Webhook signature is invalid.', e)
return 'Invalid signature', 403
event = request.json
print('Received request id {}, webhook_id: {}'.format(
event['id'], event['webhook_id']))
if event['type'] == "block":
print('Received block hash {}'.format(event['payload']['hash']))
elif event['type'] == "...":
else:
print('Unexpected event type {}'.format(event['type']))
return 'Webhook received', 200
else:
return 'POST Method not supported', 405
if __name__ == "__main__":
app.run(host='0.0.0.0', port=6666)
Development
Install dependencies
pip install -r requirements.txt
pip install -r rest-requirements.txt
Install package
pip install .
Run integration and unit tests:
pytest
For integration tests you need to set env variable BLOCKFROST_PROJECT_ID_MAINNET
Release workflow
To release the package create a new release via GitHub releases.
This action triggers the automated release workflow that packages and uploads the distribution to PyPI.