Python SDK - Examples
For sample code which explains the core concepts of how to use the SDK, see:
Below are additional resources which may be more suited for your individual use case.
Code Snippets
Section titled “Code Snippets”The v2/examples folder
in the SDK repo has many code snippets you can customize to your needs. When you
install aptos-sdk from PyPI, import the same APIs from aptos_sdk.v2 instead
of aptos_sdk_v2.
| Example | Description |
|---|---|
transfer_apt.py | Basic APT transfer between two accounts |
different_key_types.py | Ed25519 vs Secp256k1 accounts |
multi_agent_transfer.py | Multi-agent transaction with two signers |
sponsored_transaction.py | Fee-payer sponsored transaction |
orderless_transfer.py | Orderless transaction with a replay-protection nonce |
batch_orderless_transfers.py | Parallel orderless transfers |
simulate_transaction.py | Simulate a transaction before submitting |
fetch_on_chain_data.py | Ledger info, blocks, resources, and view functions |
mnemonic_key_recovery.py | BIP-39 mnemonic generation and key derivation |
custom_coin_fungible_asset.py | Coin API vs Fungible Asset API |
script_transaction.py | Script payload construction |
v1 examples such as transfer_coin.py and fee_payer_transfer_coin.py live in
the top-level examples folder.
How to run examples
Section titled “How to run examples”To run one of the v2 example scripts:
-
Clone the repository.
Terminal window git clone https://github.com/aptos-labs/aptos-python-sdk.gitcd aptos-python-sdk -
Install the package and dependencies.
Terminal window pip install . -
Run an example. From the
v2/directory the examples importaptos_sdk_v2:Terminal window cd v2python examples/transfer_apt.py
Helpful Reference Code
Section titled “Helpful Reference Code”- SDK source code - This has in-line comments explaining what each function does.
- v1 to v2 migration guide - API mapping for existing v1 code.