Get started with Smart Accounts in minutes
mainnet
devnet
npm install axios @solana/web3.js # or yarn add axios @solana/web3.js
import axios from "axios"; import { Keypair } from "@solana/web3.js"; // Initialize API client const api = axios.create({ baseURL: "https://developer-api.squads.so/api/v1", headers: { Authorization: `Bearer YOUR_API_KEY`, "x-squads-network": "devnet", "Content-Type": "application/json", }, }); // Generate or import your signers const signer1 = Keypair.generate(); const signer2 = Keypair.generate(); const signer3 = Keypair.generate(); // Create a Smart Account async function createSmartAccount() { const response = await api.post("/smart-accounts", { smart_account_signers: [ { address: signer1.publicKey.toString(), permissions: ["CAN_INITIATE", "CAN_VOTE", "CAN_EXECUTE"], }, { address: signer2.publicKey.toString(), permissions: ["CAN_VOTE"], }, { address: signer3.publicKey.toString(), permissions: ["CAN_VOTE"], }, ], threshold: 2, }); return response.data; }
async function createSmartAccountIdempotent() { const response = await api.post( "/smart-accounts", { // ... same configuration as above }, { headers: { "x-idempotency-key": "unique-random-string", }, } ); if (response.data.status === "pending") { // Account creation in progress return; } return response.data.smart_account_address; }
async function addTimeLock(smartAccountAddress: string) { const response = await api.patch(`/smart-accounts/${smartAccountAddress}`, { time_lock: 86400, // 24 hours in seconds transaction_signers: [ signer1.publicKey.toString(), signer2.publicKey.toString(), ], }); // Sign and submit the transaction const { transaction } = response.data; // ... sign with signer1 and signer2 }
async function setAdmin(smartAccountAddress: string, adminAddress: string) { const response = await api.patch(`/smart-accounts/${smartAccountAddress}`, { admin_address: adminAddress, transaction_signers: [ signer1.publicKey.toString(), signer2.publicKey.toString(), ], }); // Sign and submit the transaction const { transaction } = response.data; // ... sign with signer1 and signer2 }
async function createSpendingLimit(smartAccountAddress: string) { const USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; const response = await api.post( `/smart-accounts/${smartAccountAddress}/spending-limits`, { amount: "1000000000", // 1000 USDC (6 decimals) token_address: USDC_MINT, period: "DAILY", spending_limit_signers: [signer1.publicKey.toString()], destinations: ["ALLOWED_DEST1", "ALLOWED_DEST2"], transaction_signers: [ signer1.publicKey.toString(), signer2.publicKey.toString(), ], } ); // Sign and submit the transaction const { transaction } = response.data; // ... sign with signer1 and signer2 }
async function getSmartAccount(smartAccountAddress: string) { const response = await api.get(`/smart-accounts/${smartAccountAddress}`); // Example response data // { // smart_account_address: string; // signers: Array<{ // address: string; // permissions: Array<"CAN_INITIATE" | "CAN_VOTE" | "CAN_EXECUTE">; // }>; // threshold: number; // time_lock?: number; // admin_address?: string; // } return response.data; } // Check spending limits async function getSpendingLimits(smartAccountAddress: string) { const response = await api.get( `/smart-accounts/${smartAccountAddress}/spending-limits` ); return response.data; }
Was this page helpful?