Skip to content

Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Your first transaction

This section is a quick start guide for interacting with the Algorand network using Python. This guide will help to install sandbox, which provides a node for testing and development. This guide will also help to install the Python SDK, create an account and submit your first transaction on Algorand.

Alternative Guide

If you are a visual learner, try our submitting your first transaction live demo or watch a full video that explains the following steps.
Try Live Demo
Watch Full Video

Install Sandbox

Prerequisites

Algorand provides a docker instance for setting up a node, which can be used to get started developing quickly. To install and use this instance, follow these instructions. ​

git clone https://github.com/algorand/sandbox.git
cd sandbox
./sandbox up testnet
Watch Video
More Information

This will install a Sandbox node connected to the Algorand TestNet. To read more about Algorand networks see Algorand Networks.

Warning

The sandbox installation may take a few minutes to startup in order to catch up to the current block round. To learn more about fast catchup, see Sync Node Network using Fast Catchup

Install SDK

Algorand provides an SDK for Python which is available as a pip package. To install the Python SDK, open a terminal and run the following command: ​

pip3 install py-algorand-sdk
Watch Video ​

Alternatively, choose and download a distribution file, and run

pip3 install [file name].

The GitHub repository contains additional documentation and examples.

See the Python SDK reference documentation for more information on methods.
​ The SDK is installed and can now interact with the Sandbox created earlier.​

Create an Account on Algorand

In order to interact with the Algorand blockchain, you must have a funded account. To quickly create a test account use the following code. ​

from algosdk import account, mnemonic

def generate_algorand_keypair():
    private_key, address = account.generate_account()
    print("My address: {}".format(address))
    print("My private key: {}".format(private_key))
    print("My passphrase: {}".format(mnemonic.from_private_key(private_key)))
Watch Video
More Information

Tip

Make sure to save your account's address and passphrase in a seperate place, as they will be used later on.

​

Warning

Never share mnemonic and private key. Production environments require stringent private key management. For more information on key management in community Wallets, click here. For the Algorand open source wallet, click here.

Fund the Account

Before sending transactions to the Algorand network, the account must be funded to cover the minimal transaction fees that exist on Algorand. To fund the account use the Algorand faucet. ​ Watch Video
​

Info

All Algorand accounts require a minimum balance to be registered in the ledger. To read more about Algorand minimum balance, see Account Overview.

Connect Your Client

Client must be instantiated prior to making calls to the API endpoints. You must provide values for <algod-address> and <algod-token>. The CLI tools implement the client natively. Code beyond this point will be put into the first_transaction_example function to create a single script.

from algosdk.v2client import algod

def first_transaction_example(private_key, my_address):
    algod_address = "http://localhost:4001"
    algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    algod_client = algod.AlgodClient(algod_token, algod_address)
Watch Video

Info

The example code connects to the sandbox Algod client. If you want to connect to a Purestake client, see Purestake's code samples.

Check Your Balance

Before moving on to the next step, make sure your account has been funded by the faucet.

    account_info = algod_client.account_info(my_address)
    print("Account balance: {} microAlgos".format(account_info.get('amount')) + "\n")
Watch Video

Build First Transaction

Transactions are used to interact with the Algorand network. To create a payment transaction use the following code. ​

# build transaction
from algosdk.future.transaction import PaymentTxn

    params = algod_client.suggested_params()
    # comment out the next two (2) lines to use suggested fees
    params.flat_fee = True
    params.fee = 1000
    receiver = "HZ57J3K46JIJXILONBBZOHX6BKPXEM2VVXNRFSUED6DKFD5ZD24PMJ3MVA"
    note = "Hello World".encode()

    unsigned_txn = PaymentTxn(my_address, params, receiver, 1000000, None, note)
Watch Video
​

Info

Algorand supports many transaction types. To see what types are supported see Transactions.

Sign First Transaction

Before the transaction is considered valid, it must be signed by a private key. Use the following code to sign the transaction. ​

    # sign transaction
    signed_txn = unsigned_txn.sign(private_key)
Watch Video
​

Info

Algorand provides many ways to sign transactions. To see other ways see Authorization.

Submit the Transaction

The signed transaction can now be submitted to the network. wait_for_confirmation is called after the transaction is submitted to wait until the transaction is broadcast to the Algorand blockchain and is confirmed. For more information, see Wait for Confirmation. ​

import json
import base64

    #submit transaction
    txid = algod_client.send_transaction(signed_txn)
    print("Successfully sent transaction with txID: {}".format(txid))

    # wait for confirmation 
    try:
        confirmed_txn = wait_for_confirmation(algod_client, txid, 4)  
    except Exception as err:
        print(err)
        return

    print("Transaction information: {}".format(
        json.dumps(confirmed_txn, indent=4)))
    print("Decoded note: {}".format(base64.b64decode(
        confirmed_txn["txn"]["txn"]["note"]).decode()))

    # utility function for waiting on a transaction confirmation
def wait_for_confirmation(client, transaction_id, timeout):
    """
    Wait until the transaction is confirmed or rejected, or until 'timeout'
    number of rounds have passed.
    Args:
        transaction_id (str): the transaction to wait for
        timeout (int): maximum number of rounds to wait    
    Returns:
        dict: pending transaction information, or throws an error if the transaction
            is not confirmed or rejected in the next timeout rounds
    """
    start_round = client.status()["last-round"] + 1
    current_round = start_round

    while current_round < start_round + timeout:
        try:
            pending_txn = client.pending_transaction_info(transaction_id)
        except Exception:
            return 
        if pending_txn.get("confirmed-round", 0) > 0:
            return pending_txn
        elif pending_txn["pool-error"]:  
            raise Exception(
                'pool error: {}'.format(pending_txn["pool-error"]))
        client.status_after_block(current_round)                   
        current_round += 1
    raise Exception(
        'pending tx not found in timeout rounds, timeout value = : {}'.format(timeout))
Watch Video

Complete Example

The complete example below illustrates how to quickly submit your first transaction. ​

import json
import base64
from algosdk import account, mnemonic
from algosdk.v2client import algod
from algosdk.future.transaction import PaymentTxn

def generate_algorand_keypair():
    private_key, address = account.generate_account()
    print("My address: {}".format(address))
    print("My private key: {}".format(private_key))
    print("My passphrase: {}".format(mnemonic.from_private_key(private_key)))

# Write down the address, private key, and the passphrase for later usage
generate_algorand_keypair()

def first_transaction_example(private_key, my_address):
    algod_address = "http://localhost:4001"
    algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    algod_client = algod.AlgodClient(algod_token, algod_address)

    print("My address: {}".format(my_address))
    account_info = algod_client.account_info(my_address)
    print("Account balance: {} microAlgos".format(account_info.get('amount')))

    # build transaction
    params = algod_client.suggested_params()
    # comment out the next two (2) lines to use suggested fees
    params.flat_fee = True
    params.fee = 1000
    receiver = "HZ57J3K46JIJXILONBBZOHX6BKPXEM2VVXNRFSUED6DKFD5ZD24PMJ3MVA"
    note = "Hello World".encode()

    unsigned_txn = PaymentTxn(my_address, params, receiver, 1000000, None, note)

    # sign transaction
    signed_txn = unsigned_txn.sign(private_key))

    # submit transaction
    txid = algod_client.send_transaction(signed_txn)
    print("Signed transaction with txID: {}".format(txid))

    # wait for confirmation 
    try:
        confirmed_txn = wait_for_confirmation(algod_client, txid, 4)  
    except Exception as err:
        print(err)
        return

    print("Transaction information: {}".format(
        json.dumps(confirmed_txn, indent=4)))
    print("Decoded note: {}".format(base64.b64decode(
        confirmed_txn["txn"]["txn"]["note"]).decode()))

    account_info = algod_client.account_info(my_address)
    print("Account balance: {} microAlgos".format(account_info.get('amount')) + "\n")

# utility function for waiting on a transaction confirmation
def wait_for_confirmation(client, transaction_id, timeout):
    """
    Wait until the transaction is confirmed or rejected, or until 'timeout'
    number of rounds have passed.
    Args:
        transaction_id (str): the transaction to wait for
        timeout (int): maximum number of rounds to wait    
    Returns:
        dict: pending transaction information, or throws an error if the transaction
            is not confirmed or rejected in the next timeout rounds
    """
    start_round = client.status()["last-round"] + 1
    current_round = start_round

    while current_round < start_round + timeout:
        try:
            pending_txn = client.pending_transaction_info(transaction_id)
        except Exception:
            return 
        if pending_txn.get("confirmed-round", 0) > 0:
            return pending_txn
        elif pending_txn["pool-error"]:  
            raise Exception(
                'pool error: {}'.format(pending_txn["pool-error"]))
        client.status_after_block(current_round)                   
        current_round += 1
    raise Exception(
        'pending tx not found in timeout rounds, timeout value = : {}'.format(timeout))

#replace private_key and my_address with your private key and your address
first_transaction_example(private_key, my_address)​
Run Code
Watch Video
Go to Github
​​

Warning

In order for this transaction to be successful, the account must be funded.

View the Transaction

To view the transaction, open AlgoExplorer or Goal Seeker and paste the transaction ID into the search bar.

Watch Video

Set Up Your Editor/Framework

The Algorand community provides many editors, frameworks, and plugins that can be used to work with the Algorand Network. Tutorials have been created for configuring each of these for use with Algorand. Select your Editor preference below.