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.

Python SDK: Your first transaction

This section is a quick start guide for interacting with the Algorand network using Python. This guide will help to install Algorand 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.

Install Sandbox

Info

This step is only required if you are not using AlgoKit. If you are using AlgoKit, you can spin up a sandbox using the LocalNet, see AlgoKit getting started guide for more information.

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 dev 

This will install and start private network. To read more about Algorand networks see Algorand Networks.

More Information about the sandbox and how to use it.

Install Python 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

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

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

private_key, address = account.generate_account()
print(f"address: {address}")
print(f"private key: {private_key}")
print(f"mnemonic: {mnemonic.from_private_key(private_key)}")
Snippet Source

More Information

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. In this example, we'll be using prefunded accounts available in the Sandbox. To fund an account on Testnet account use the Algorand faucet. ​

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

An Algod 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. By default, the algod_token for each sandbox is set to its aaa... value and the algod_address corresponds to http://localhost:4001.

# Create a new algod client, configured to connect to our local sandbox
algod_address = "http://localhost:4001"
algod_token = "a" * 64
algod_client = algod.AlgodClient(algod_token, algod_address)

# Or, if necessary, pass alternate headers

# Create a new client with an alternate api key header
special_algod_client = algod.AlgodClient(
    "", algod_address, headers={"X-API-Key": algod_token}
)
Snippet Source

Info

The example code connects to the sandbox Algod client. If you want to connect to a public API client, change the host, port, and token parameters to match the API service. See some service available here

Info

If you are connecting to the Testnet, a dispenser is available here

Check Your Balance

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

account_info: Dict[str, Any] = algod_client.account_info(address)
print(f"Account balance: {account_info.get('amount')} microAlgos")
Snippet Source

Build First Transaction

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

# grab suggested params from algod using client
# includes things like suggested fee and first/last valid rounds
params = algod_client.suggested_params()
unsigned_txn = transaction.PaymentTxn(
    sender=address,
    sp=params,
    receiver=address2,
    amt=1000000,
    note=b"Hello World",
)
Snippet Source

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 the transaction
signed_txn = unsigned_txn.sign(private_key)
Snippet Source

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 SDK Method is called after the transaction is submitted to wait until the transaction is broadcast to the Algorand blockchain and is confirmed.

# submit the transaction and get back a transaction id
txid = algod_client.send_transaction(signed_txn)
print("Successfully submitted transaction with txID: {}".format(txid))

# wait for confirmation
txn_result = transaction.wait_for_confirmation(algod_client, txid, 4)

print(f"Transaction information: {json.dumps(txn_result, indent=4)}")
print(f"Decoded note: {b64decode(txn_result['txn']['txn']['note'])}")
Snippet Source

View the Transaction

To view the transaction we submitted to the sandbox Algod, open Lora and choose LocalNet configuration option, then search for the transaction ID.

To view a transaction submitted to public network like testnet, open Lora or Pera Explorer and paste the transaction ID into the search bar.