AI agents cannot get bank accounts, but they can get crypto wallets. Wallet API is a powerful and secure way to give agents crypto wallets and introduce the ability to transfer value to artificial intelligence. Automate complex financial transactions that would be time-consuming for humans to manage at scale, and seamlessly connect AI to the crypto ecosystem.
Replit for easy deployments
Replit is an AI-powered software development & deployment platform for building, sharing, and shipping software fast.
Coinbase has partnered with Replit to create a template that enables developers to build and deploy AI Wallet applications
in just minutes.
Natural Language Financial Transactions: Allow users to manage their finances through simple text commands, with AI interpreting and executing complex financial operations.
AI Financial Concierge: Personal AI assistants that not only recommend services but also handle payments, booking, and planning.
AI-Driven Content Monetization: Create automated systems that create, publish, and monetize content, and manage earnings as an autonomous entity.
Self-Owned Autonomous Vehicle: A self-driving vehicle that picks up drivers, receives payments, and pays for maintenance? The future may be closer than it seems.
We use the Base Sepolia network to demonstrate sending crypto from an AI agent to a user.
We will use the AI feedback app here to demonstrate the solution.
The app takes the following inputs as environment variables:
NAME: Enter the name of your downloaded CDP API key.
PRIVATE_KEY: Enter the private key of your downloaded CDP API key.
WALLET_DATA: Enter the seed data of your wallet, if you have an existing wallet. Leave it empty if you want a new wallet to be created for the agent. Refer to the persisting wallet section to see how to fetch wallet data.
The code below shows how to import wallet or create a new one if WALLET_DATA env var is not set.
app/api/route.ts
Copy
Ask AI
const { NAME, PRIVATE_KEY, WALLET_DATA } = process.env;// Check if the environment variables are setif (!NAME || !PRIVATE_KEY) { return Response.json( { message: "Environment variables are not set" }, { status: 500 } );}const body = await request.json();// Check if the address is providedif (!body?.address) { return Response.json({ message: "Address is required" }, { status: 400 });}// Create a new Coinbase instanceconst coinbase = new Coinbase({ apiKeyName: NAME as string, privateKey: PRIVATE_KEY.replaceAll("\\n", "\n") as string,});let userWallet;// Check if the wallet data is providedif (WALLET_DATA && WALLET_DATA?.length > 0) { try { // Parse the wallet data const seedFile = JSON.parse(WALLET_DATA || "{}"); // Get the wallet ids from the seed file. The seed file is a JSON object with wallet ids as keys. const walletIds = Object.keys(seedFile); // Get a random wallet id const walletId = getRandomItems(walletIds, 1)[0]; // Get the seed of the wallet const seed = seedFile[walletId]?.seed; // Import the wallet userWallet = await Wallet.import({ seed, walletId }); await userWallet.listAddresses(); } catch (e) { return Response.json( { message: "Failed to import wallet" }, { status: 500 } ); }} else { // Otherwise, create a new wallet userWallet = await Wallet.create();}
To fund the wallet with ETH on Base Sepolia, utilize the faucet method.
app/api/route.ts
Copy
Ask AI
// Fund the wallet with faucet if requiredtry { // Request funds from the faucet if it's available let faucetTx = await userWallet?.faucet() await faucetTx.wait();} catch (e) { // Log if the faucet is not available. console.log("Faucet is not available");}
Step 4. Create wallets for end-users to receive payments
Use Coinbase Smart Wallets to enable users without an existing wallet to get payments from the AI agent.
Smart Wallets provide a seamless account creation process in seconds, eliminating the need for an app or extension. This is made possible by utilizing passkeys for signing, which are securely generated and stored on users’ devices.
The code to integrate Coinbase Smart Wallet in your application can be found here.
Step 5. Send crypto from the AI agent’s wallet to the user wallet
Use createTransfer to send crypto from the AI agent’s wallet to the user wallet after the user has completed their task.
app/api/route.ts
Copy
Ask AI
// Create a transfer to the destination addressconst transfer = await userWallet?.createTransfer({ amount: 0.00000001, assetId: "eth", destination: body.address,});// Wait for transfer to settle.await transfer.wait(); // Return the transaction hash and linkreturn Response.json( { transactionHash: transfer?.getTransactionHash()?.substring(0, 10), transactionLink: transfer?.getTransactionLink(), successful: transfer.getStatus() === 'complete' }, { status: 200 });
To receive funds from other agents or users, fetch the default address of the AI agent’s wallet with the following method:
Copy
Ask AI
// Get the default address of the walletconst defaultAddress = await userWallet?.getDefaultAddress();console.log("AI agent's Wallet's Address: ", defaultAddress);