Python SDK
The Python SDK lets you connect, explore, and interact with the Aptos blockchain. You can use it to request data, send transactions, set up test environments, and more.
Installing the Python SDK
Section titled “Installing the Python SDK”Aptos publishes the official Python SDK on PyPI with source code in the aptos-python-sdk GitHub repository. The SDK requires Python 3.12+.
Install with pip:
pip install aptos-sdkOr from source:
git clone https://github.com/aptos-labs/aptos-python-sdkcd aptos-python-sdkpip install .Examples
Section titled “Examples” Quickstart Get a working transfer on devnet in a few minutes
Accounts Generate, restore, and fund accounts
Fetching Data Read ledger, account, and view-function data
Submitting Transactions Build, simulate, sign, and submit transactions
Examples Catalog of runnable examples in the SDK repository
Tests Unit and integration tests that exercise the v2 API
Transfer APT from the tested example suite
Section titled “Transfer APT from the tested example suite”The snippet below is pulled from
v2/examples/transfer_apt.py
in the SDK repository. When you install from PyPI, import the same APIs from
aptos_sdk.v2 instead of aptos_sdk_v2.
30 collapsed lines
"""Example: Transfer APT between accounts on devnet."""
import asyncio
from aptos_sdk_v2 import Account, Aptos, AptosConfig, Network
async def main(): config = AptosConfig(network=Network.DEVNET) async with Aptos(config) as aptos: # Create two accounts alice = Account.generate() bob = Account.generate() print(f"Alice: {alice.address}") print(f"Bob: {bob.address}")
# Fund Alice print("\nFunding Alice...") await aptos.faucet.fund_account(alice.address, 100_000_000)
# Fund Bob so account exists on-chain print("Funding Bob...") await aptos.faucet.fund_account(bob.address, 10_000_000)
# Check balances alice_balance = await aptos.coin.balance(alice.address) bob_balance = await aptos.coin.balance(bob.address) print(f"\nAlice balance: {alice_balance}") print(f"Bob balance: {bob_balance}")
# Transfer 1000 octas from Alice to Bob print("\nTransferring 1000 octas from Alice to Bob...") txn_hash = await aptos.coin.transfer(alice, bob.address, 1_000) result = await aptos.transaction.wait_for_transaction(txn_hash) print(f"Transaction: {txn_hash}") print(f"Success: {result['success']}")10 collapsed lines
# Check final balances alice_balance = await aptos.coin.balance(alice.address) bob_balance = await aptos.coin.balance(bob.address) print(f"\nAlice balance: {alice_balance}") print(f"Bob balance: {bob_balance}")
if __name__ == "__main__": asyncio.run(main())