Rust SDK
The Rust 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 Rust SDK
Section titled “Installing the Rust SDK”Aptos publishes the official Rust SDK in the
aptos-rust-sdk GitHub repository
and on crates.io. Add it to your
Cargo.toml:
[dependencies]aptos-sdk = "0.7"tokio = { version = "1", features = ["full"] }anyhow = "1"To use the latest unreleased changes:
[dependencies]aptos-sdk = { git = "https://github.com/aptos-labs/aptos-rust-sdk", package = "aptos-sdk" }The SDK requires Rust 1.95+. Unlike the legacy crate in aptos-core, this
SDK does not need git patches or a .cargo/config.toml tokio_unstable flag.
Feature flags
Section titled “Feature flags”| Feature | Default | Description |
|---|---|---|
ed25519 | Yes | Ed25519 signature scheme |
secp256k1 | Yes | Secp256k1 ECDSA signatures |
secp256r1 | Yes | Secp256r1 (P-256) ECDSA signatures |
mnemonic | Yes | BIP-39 mnemonic phrase support |
indexer | Yes | GraphQL indexer client |
faucet | Yes | Faucet integration for testnets |
bls | No | BLS12-381 signatures |
macros | No | Proc macros for type-safe contract bindings |
Minimal build:
[dependencies]aptos-sdk = { version = "0.7", default-features = false, features = ["ed25519"] }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 Crate tests that exercise the SDK APIs
Transfer APT from the tested example suite
Section titled “Transfer APT from the tested example suite”The snippet below is pulled from
transfer.rs
in the SDK repository. Fund well above the transfer amount: the default
max_gas_amount is 2_000_000.
37 collapsed lines
//! Example: Basic APT transfer//!//! This example demonstrates how to://! 1. Create an Aptos client//! 2. Generate or load an account//! 3. Fund the account using the faucet//! 4. Transfer APT to another account//!//! Run with: `cargo run --example transfer --features "ed25519,faucet"`
use aptos_sdk::{Aptos, AptosConfig, account::Ed25519Account};
#[tokio::main]async fn main() -> anyhow::Result<()> { // Create client for devnet let aptos = Aptos::new(AptosConfig::devnet())?; println!("Connected to devnet");
// Generate sender account let sender = Ed25519Account::generate(); println!("Sender address: {}", sender.address());
// Fund sender using faucet println!("Funding sender account..."); aptos.fund_account(sender.address(), 100_000_000).await?;
// Wait for funding to complete tokio::time::sleep(std::time::Duration::from_secs(2)).await;
// Check sender balance let balance = aptos.get_balance(sender.address()).await?; println!("Sender balance: {} APT", balance as f64 / 100_000_000.0);
// Generate recipient account let recipient = Ed25519Account::generate(); println!("Recipient address: {}", recipient.address());
// Transfer 0.1 APT (10_000_000 octas) println!("Transferring 0.1 APT..."); let result = aptos .transfer_apt(&sender, recipient.address(), 10_000_000) .await?;31 collapsed lines
let success = result .data .get("success") .and_then(serde_json::value::Value::as_bool); if success == Some(true) { println!("Transfer successful!");
// Check balances let sender_balance = aptos.get_balance(sender.address()).await?; let recipient_balance = aptos.get_balance(recipient.address()).await?;
println!( "Sender balance: {} APT", sender_balance as f64 / 100_000_000.0 ); println!( "Recipient balance: {} APT", recipient_balance as f64 / 100_000_000.0 ); } else { let vm_status = result .data .get("vm_status") .and_then(|v| v.as_str()) .unwrap_or("unknown"); println!("Transfer failed: {vm_status}"); }
Ok(())}