AIWallet API

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.

Get started with our AI Wallet Replit template. If you plan to deploy this template publicly, read Securing a Wallet to learn how to protect your wallets.

Key Benefits

  • Financial Autonomy for AI: Enable AI agents to make financial decisions and transactions on your behalf.
  • Enhanced Security: With Wallet API’s 2-of-2 configuration, utilize MPC technology to ensure AI operations remain controlled and secure.
  • Scalability: Effortlessly handle millions of transactions.

Example Use Cases

  • 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.

Overview

After you install the CDP SDK, the steps below walk through the AI agent sample app to do the following:

  1. Import required modules.
  2. Create the AI agent’s wallet.
  3. Fund the AI agent’s wallet with faucet.
  4. Create wallets for end-users to receive payments.
  5. Send crypto from the AI agent’s wallet to the user wallet.
  6. Receive funds from other agents or users.
  7. Display the AI agent’s wallet balance after sending crypto to the user.

Prerequisites

installation
npm install @coinbase/coinbase-sdk

Send crypto from the AI agent’s wallet to a user

Step 1. Import required modules

app/api/route.ts
import { Coinbase, Wallet } from "@coinbase/coinbase-sdk"; // Use CDP SDK

Step 2. Create the AI agent’s wallet

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.

Code Reference

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
const { NAME, PRIVATE_KEY, WALLET_DATA } = process.env;

// Check if the environment variables are set
if (!NAME || !PRIVATE_KEY) {
  return Response.json(
    { message: "Environment variables are not set" },
    { status: 500 }
  );
}

const body = await request.json();

// Check if the address is provided
if (!body?.address) {
  return Response.json({ message: "Address is required" }, { status: 400 });
}

// Create a new Coinbase instance
const coinbase = new Coinbase({
  apiKeyName: NAME as string,
  privateKey: PRIVATE_KEY.replaceAll("\\n", "\n") as string,
});

let userWallet;

// Check if the wallet data is provided
if (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();
}

Step 3. Fund the AI agent’s Wallet with faucet

To fund the wallet with ETH on Base Sepolia, utilize the faucet method.

app/api/route.ts
// Fund the wallet with faucet if required
try {
  // 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
// Create a transfer to the destination address
const 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 link
return Response.json(
  {
    transactionHash: transfer?.getTransactionHash()?.substring(0, 10),
    transactionLink: transfer?.getTransactionLink(),
    successful: transfer.getStatus() === 'complete'
  },
  { status: 200 }
);

Step 6. Receive funds from other agents or users

To receive funds from other agents or users, fetch the default address of the AI agent’s wallet with the following method:

// Get the default address of the wallet
const defaultAddress = await userWallet?.getDefaultAddress();
console.log("AI agent's Wallet's Address: ", defaultAddress);

Step 7. Display the AI agent’s wallet balance after sending crypto to the user

Use balances to get the balance of the AI agent’s wallet after sending crypto to the user.

// Get the balance of the wallet
const balances = await userWallet?.balances();
console.log("Balances: ", balances);

To see the full code, refer to the AI agent sample app.