This guide will help you create your first smart account using the Squads API. For a complete overview of the API capabilities, see the API Introduction.
Create a basic smart account with a single signer. For more details about smart account creation and management, see the Create Smart Account documentation.
That’s it! Your smart account is ready to use immediately. No transaction submission or fee payment required. You can later add more signers or set up spending limits as needed.
First, import the required dependencies and create the token transfer instruction:
Copy
Ask AI
import { createTransferInstruction } from "@solana/spl-token";import { Connection, PublicKey, TransactionMessage, VersionedTransaction} from "@solana/web3.js";// Initialize connection to Solanaconst connection = new Connection("https://api.devnet.solana.com");// Create the token transfer instructionconst transferInstruction = createTransferInstruction( new PublicKey("SOURCE_TOKEN_ACCOUNT"), // The smart account's token account new PublicKey("DESTINATION_TOKEN_ACCOUNT"), new PublicKey(smart_account_address), // The smart account is the authority 1000000n // Amount (e.g., 1 USDC));
Next, create a V0 transaction message with the latest blockhash:
Copy
Ask AI
// Get the latest blockhashconst latestBlockhash = await connection.getLatestBlockhash();// Create a V0 transaction messageconst message = new TransactionMessage({ payerKey: new PublicKey(smart_account_address), recentBlockhash: latestBlockhash.blockhash, instructions: [transferInstruction]}).compileToV0Message();// Create and serialize the transactionconst transaction = new VersionedTransaction(message);const serializedTransaction = Buffer.from( transaction.serialize()).toString('base64');
Finally, sign the prepared transaction with your wallet and submit it to Solana:
Copy
Ask AI
// Decode the prepared transactionconst decodedTransaction = VersionedTransaction.deserialize( Buffer.from(preparedTransaction, "base64"));// Sign with your walletdecodedTransaction.sign([wallet]);// Submit to Solanaconst signature = await connection.sendRawTransaction( decodedTransaction.serialize());console.log(`Transaction sent: ${signature}`);// Wait for confirmationconst confirmation = await connection.confirmTransaction({ signature, blockhash: latestBlockhash.blockhash, lastValidBlockHeight: latestBlockhash.lastValidBlockHeight});console.log("Transfer complete!");
Each step builds on the previous one to create a complete token transfer flow. The Squads API handles the complexity of smart account execution, allowing you to focus on building your application logic.