Python SDK

The Python SDK is available on GitHub and provides core functionality to interact with the Algorand blockchain using Python. The Python SDK provides Classes with 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). The kmd and algod processes are described in the Node Overview documentation. These can be used to create node wallets, accounts, submit transactions, get block information, take accounts online and many other node operations. Many examples are available in the GitHub readme.

The documentation for the API is available here.

Start the algod and kmd Processes

When using the SDK, it can interact with the algod, kmd or both processes. The algod process starts automatically when nodes are started. The kmd process automatically starts when called using the goal command line tool but with a 60-second timeout. This means it will shut down when no activity is detected for 60 seconds. To make sure the kmd is available for use with the SDK you can use the goal kmd command to start the kmd process with an indefinite timeout. Typically this will be just entering the command:

goal kmd start -d <data-dir>

Once the kmd is started, it will create  kmd.net and kmd.token files within the data directory for kmd. If you are using an RPM or DEB install these files will be in the ${HOME}/.algorand/kmd-version directory. The algod data directory also has the algod.net and algod.token files. These files contain the server and port number for the REST endpoints and API tokens that the SDK will need to communicate with their respective processes.

Install the SDK

Make sure you have Python3 installed, open a terminal and run the following command:

pip3 install py-algorand-sdk

Example

Below is an example of getting the latest block using the Python SDK.

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.                                                                                                                                                  

algod_address = "http://yourhost:yourport"
algod_token = "yourAPItoken"

def main():
    algod_client = AlgodClient(algod_token, algod_address)

    try:
        status = algod_client.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                                                                                                                                               
    last_round = algod_client.status().get("lastRound")
    print("####################")
    block = algod_client.block_info(last_round)
    print(block)
main()