Rust SDK Quickstart
-
Create a New Project
Use Cargo to scaffold a new Rust project:
Terminal window cargo new aptos-quickstart && cd aptos-quickstart -
Add Dependencies
Open
Cargo.tomland add the Aptos SDK along withtokiofor the async runtime andanyhowfor error handling:[dependencies]aptos-sdk = { git = "https://github.com/aptos-labs/aptos-rust-sdk", package = "aptos-sdk" }tokio = { version = "1", features = ["full"] }anyhow = "1" -
Set Up the Aptos Client
The
Aptosclient handles all communication with the Aptos network. Create one by passing anAptosConfigthat specifies which network to connect to.use aptos_sdk::{Aptos, AptosConfig};use aptos_sdk::account::Ed25519Account;#[tokio::main]async fn main() -> anyhow::Result<()> {println!("This example will create two accounts (Alice and Bob), fund them, and transfer APT between them.");// Connect to testnetlet aptos = Aptos::new(AptosConfig::testnet())?;Ok(())} -
Create and Fund Accounts
Generate two new Ed25519 accounts. On testnet and devnet, you can fund accounts programmatically using the faucet, which sends test APT to your new accounts. Funding an account also creates it on-chain.
// Generate two new accountslet alice = Ed25519Account::generate();let bob = Ed25519Account::generate();println!("\n=== Addresses ===\n");println!("Alice's address is: {}", alice.address());println!("Bob's address is: {}", bob.address());// Fund the accounts using the testnet faucetprintln!("\n=== Funding accounts ===\n");aptos.fund_account(alice.address(), 100_000_000).await?;aptos.fund_account(bob.address(), 100_000_000).await?;println!("Alice and Bob's accounts have been funded!"); -
Fetch Data from On-Chain
Use the
Aptosclient to query account balances. Theget_balancemethod returns the APT balance in octas.println!("\n=== Initial Balances ===\n");let alice_balance = aptos.get_balance(alice.address()).await?;let bob_balance = aptos.get_balance(bob.address()).await?;println!("Alice's balance: {} octas", alice_balance);println!("Bob's balance: {} octas", bob_balance); -
Transfer APT
Use the
transfer_aptconvenience method to send APT from one account to another. This method builds, signs, submits, and waits for the transaction in a single call.// Transfer 0.1 APT (10_000_000 octas) from Alice to Bobprintln!("\n=== Transfer transaction ===\n");let result = aptos.transfer_apt(&alice, bob.address(), 10_000_000).await?;// Verify the transaction succeededlet success = result.data.get("success").and_then(|v| v.as_bool()).unwrap_or(false);println!("Transaction success: {}", success); -
Verify the Transfer
Fetch the updated balances to confirm that the transfer went through. Alice’s balance will be lower than expected due to the gas fee paid for the transaction.
println!("\n=== Balances after transfer ===\n");let new_alice_balance = aptos.get_balance(alice.address()).await?;let new_bob_balance = aptos.get_balance(bob.address()).await?;println!("Alice's balance: {} octas", new_alice_balance);println!("Bob's balance: {} octas", new_bob_balance);
Full Quickstart Code
Section titled “Full Quickstart Code”Run Quickstart
Section titled “Run Quickstart”cargo run/// This example shows how to use the Aptos Rust SDK to create accounts,/// fund them, and transfer APT between them on testnet.use aptos_sdk::{Aptos, AptosConfig};use aptos_sdk::account::Ed25519Account;
const ALICE_INITIAL_BALANCE: u64 = 100_000_000;const BOB_INITIAL_BALANCE: u64 = 100_000_000;const TRANSFER_AMOUNT: u64 = 10_000_000;
#[tokio::main]async fn main() -> anyhow::Result<()> { println!( "This example will create two accounts (Alice and Bob), fund them, \ and transfer APT between them." );
// Setup the client let aptos = Aptos::new(AptosConfig::testnet())?;
// Generate two new accounts let alice = Ed25519Account::generate(); let bob = Ed25519Account::generate();
println!("\n=== Addresses ===\n"); println!("Alice's address is: {}", alice.address()); println!("Bob's address is: {}", bob.address());
// Fund the accounts using the testnet faucet. // Funding an account creates it on-chain. println!("\n=== Funding accounts ===\n"); aptos.fund_account(alice.address(), ALICE_INITIAL_BALANCE).await?; aptos.fund_account(bob.address(), BOB_INITIAL_BALANCE).await?; println!("Alice and Bob's accounts have been funded!");
// Look up the newly funded account balances println!("\n=== Initial Balances ===\n"); let alice_balance = aptos.get_balance(alice.address()).await?; let bob_balance = aptos.get_balance(bob.address()).await?; println!("Alice's balance: {} octas", alice_balance); println!("Bob's balance: {} octas", bob_balance);
// Transfer APT from Alice to Bob println!("\n=== Transfer transaction ===\n"); let result = aptos.transfer_apt(&alice, bob.address(), TRANSFER_AMOUNT).await?;
// Verify the transaction succeeded let success = result .data .get("success") .and_then(|v| v.as_bool()) .unwrap_or(false); println!("Transaction success: {}", success);
// Check updated balances println!("\n=== Balances after transfer ===\n"); let new_alice_balance = aptos.get_balance(alice.address()).await?; let new_bob_balance = aptos.get_balance(bob.address()).await?; println!("Alice's balance: {} octas", new_alice_balance); println!("Bob's balance: {} octas", new_bob_balance);
// Bob should have received the transfer amount assert_eq!( new_bob_balance, BOB_INITIAL_BALANCE + TRANSFER_AMOUNT, "Bob's balance after transfer is incorrect" );
// Alice should have less than her initial balance minus the transfer, // because gas fees are also deducted assert!( new_alice_balance < ALICE_INITIAL_BALANCE - TRANSFER_AMOUNT, "Alice's balance after transfer is incorrect" );
println!("\nQuickstart complete!");
Ok(())}Summary
Section titled “Summary”In this quickstart you learned how to:
- Set up a Rust project with the Aptos SDK.
- Connect to the Aptos testnet using the
Aptosclient. - Generate new Ed25519 accounts.
- Fund accounts using the testnet faucet.
- Query account balances from on-chain.
- Transfer APT between accounts using the
transfer_aptconvenience method. - Verify transaction results and updated balances.