Blog

Jul 23, 2019

Building in the Borderless Economy: Announcing Algorand’s Python SDK Availability

By: Algorand

With capacity over 1000 Transactions Per Second (TPS) and less than five second latency in its first month, Algorand’s MainNet is providing unprecedented network performance that can handle use cases that haven’t previously been possible. 

Algorand’s community also benefits from its robust set of developer tools, making the opportunity to build in the new borderless economy an achievable reality.  Algorand’s developer team has previously released SDKs for Go, Java, and JavaScript (available at developer.algorand.org). We also provide REST endpoints defined using the Open API Specification which can be used by virtually any language to integrate into the Algorand network. 

Today, we are excited to share an additional effort to continue expanding options and ease of use for developers: The support for our Python SDK!

The Python SDK provides simple API methods for creating, backing up and restoring accounts. Additionally the SDK can be used to create and sign transactions as well as creating and signing with multisignature accounts. These methods can be used in online and offline operation, offering extensive integration options. Finally the SDK also provides REST endpoint clients for the two primary node process (kmd for node wallet management and algod for protocol processing).  These can be used to create node wallets, accounts, submit transactions, get block information, take accounts online and many other node operations. You can checkout the documentation on developer.algorand.org or take a look at the github repository. 

For those new to the Algorand community, please check out more resources and details on the open source software at developer.algorand.org

SDK Quick Start

$ pip3 install py-algorand-sdk

Sample code 

To create an account and get the backup passphrase copy the following code into a file called createaccount.py

from algosdk.account import generate_account
from algosdk.mnemonic import from_private_key

# Generate a new account
sk, pk = generate_account()
# Convert sk to mnemonic format
sk_mnemonic = from_private_key(sk)

print("Public Key: {}".format(pk))
print("Private Key Mnemonic: {}".format(sk_mnemonic))	

To run it, open a terminal and enter the following:

$ python3  createaccount.py

If you have a node running and have access to the REST port and API token you can retrieve network status and block information using code similar to:

from algosdk.algod import *

# These constants represent the algod REST endpoint and the corresponding                                                                                                             
# API Token. You can retrieve these from the `algod.net` and `algod.token`                                                                                                            
# files in the algod data directory.                                                                                                                                                  

algodAddress = "http://yourhost:yourport"
algodToken = "yourAPItoken"

def main():
    algodclient = AlgodClient(algodToken, algodAddress)

    try:
        status = algodclient.status()
    except Exception as e:
        print("Failed to get algod status: {}".format(e))

    if status:
        print("algod last round: {}".format(status.get("lastRound")))
        print("algod time since last round: {}".format(status.get("timeSinceLastRound")))
        print("algod catchup: {}".format(status.get("catchupTime")))
        print("algod latest version: {}".format(status.get("lastConsensusVersion")))

    # Retrieve latest block information                                                                                                                                               
    lastround = algodclient.status().get("lastRound")
    print("####################")
    block = algodclient.block_info(lastround)
    print(block)
main()    

Conclusion

We would like to say a special thanks to Michelle Jiang for creating the SDK and the associated examples. This SDK offers great benefits to Python developers that want to build applications on the Algorand platform and her tireless work made it possible. Be sure to send us feedback and suggestions for improvements using the SDK github issues or on our community forum.