Agentkit
AgentKit is a framework for easily enabling AI agents to take actions onchain. It is designed to be framework-agnostic, so you can use it with any AI framework, and wallet-agnostic, so you can use it with any wallet.
Table of Contents
Getting Started
Prerequisites:
Installation
npm install @coinbase/agentkit
Usage
Create an AgentKit instance. If no wallet or action providers are specified, the agent will use the CdpWalletProvider and WalletProvider action provider.
const agentKit = await AgentKit.from({
cdpApiKeyName: "CDP API KEY NAME",
cdpApiKeyPrivate: "CDP API KEY PRIVATE KEY",
});
Create an AgentKit instance
If no wallet or action provider are specified, the agent will use the CdpWalletProvider and WalletActionProvider action provider by default.
const agentKit = await AgentKit.from({
cdpApiKeyName: "CDP API KEY NAME",
cdpApiKeyPrivate: "CDP API KEY PRIVATE KEY",
});
Create an AgentKit instance with a specified wallet provider.
import { CdpWalletProvider } from "@coinbase/agentkit";
const walletProvider = await CdpWalletProvider.configureWithWallet({
apiKeyName: "CDP API KEY NAME",
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
networkId: "base-mainnet",
});
const agentKit = await AgentKit.from({
walletProvider,
});
Create an AgentKit instance with a specified action providers.
import { cdpApiActionProvider, pythActionProvider } from "@coinbase/agentkit";
const agentKit = await AgentKit.from({
walletProvider,
actionProviders: [
cdpApiActionProvider({
apiKeyName: "CDP API KEY NAME",
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
}),
pythActionProvider(),
],
});
Use the agent's actions with a framework extension. For example, using LangChain + OpenAI.
Prerequisites:
npm install @langchain @langchain/langgraph @langchain/openai
import { getLangChainTools } from "@coinbase/agentkit-langchain";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";
const tools = await getLangChainTools(agentKit);
const llm = new ChatOpenAI({
model: "gpt-4o-mini",
});
const agent = createReactAgent({
llm,
tools,
});
Action Providers
Basename
register_basename | Registers a custom .base.eth or .basetest.eth domain name for the wallet address. |
CDP Wallet
deploy_contract | Deploys a custom smart contract using specified Solidity version and constructor arguments. |
deploy_nft | Deploys a standard ERC-721 NFT contract with configurable name, symbol, and metadata URI. |
deploy_token | Deploys a standard ERC-20 token contract with configurable name, symbol, and initial supply. |
trade | Executes a token swap between two assets at current market rates on mainnet networks. |
ERC20
get_balance | Retrieves the token balance for a specified address and ERC-20 contract. |
transfer | Transfers a specified amount of ERC-20 tokens to a destination address. |
ERC721
get_balance | Retrieves the NFT balance for a specified address and ERC-721 contract. |
mint | Creates a new NFT token and assigns it to a specified destination address. |
transfer | Transfers ownership of a specific NFT token to a destination address. |
Farcaster
account_details | Fetches profile information and metadata for the authenticated Farcaster account. |
post_cast | Creates a new cast (message) on Farcaster with up to 280 characters. |
Morpho
deposit | Deposits a specified amount of assets into a designated Morpho Vault. |
withdraw | Withdraws a specified amount of assets from a designated Morpho Vault. |
Pyth
fetch_price | Retrieves current price data from a specified Pyth price feed. |
fetch_price_feed_id | Retrieves the unique price feed identifier for a given token symbol. |
Twitter
account_details | Fetches profile information and metadata for the authenticated Twitter account. |
account_mentions | Retrieves recent mentions and interactions for the authenticated account. |
post_tweet | Creates a new tweet on the authenticated Twitter account. |
post_tweet_reply | Creates a reply to an existing tweet using the tweet's unique identifier. |
Wallet
get_wallet_details | Retrieves wallet address, network info, balances, and provider details. |
native_transfer | Transfers native blockchain tokens (e.g., ETH) to a destination address. |
WETH
wrap_eth | Converts native ETH to Wrapped ETH (WETH) on Base Sepolia or Base Mainnet. |
WOW
buy_token | Purchases WOW tokens from a contract using ETH based on bonding curve pricing. |
create_token | Creates a new WOW memecoin with bonding curve functionality via Zora factory. |
sell_token | Sells WOW tokens back to the contract for ETH based on bonding curve pricing. |
Jupiter
swap | Swap tokens on Solana using the Jupiter DEX aggregator. |
Creating an Action Provider
Action providers are used to define the actions that an agent can take. They are defined as a class that extends the ActionProvider abstract class.
import { ActionProvider, WalletProvider, Network } from "@coinbase/agentkit";
class MyActionProvider extends ActionProvider<WalletProvider> {
constructor() {
super("my-action-provider", []);
}
supportsNetwork = (network: Network) => true;
}
Adding Actions to your Action Provider
Actions are defined as instance methods on the action provider class with the @CreateAction decorator. Actions can use a wallet provider or not and always return a Promise that resolves to a string.
Required Typescript Compiler Options
Creating actions with the @CreateAction decorator requires the following compilerOptions to be included in your project's tsconfig.json.
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
Steps to create an action
- Define the action schema. Action schemas are defined using the
zod library.
import { z } from "zod";
export const MyActionSchema = z.object({
myField: z.string(),
});
import { ActionProvider, WalletProvider, Network, CreateAction } from "@coinbase/agentkit";
class MyActionProvider extends ActionProvider<WalletProvider> {
constructor() {
super("my-action-provider", []);
}
@CreateAction({
name: "my-action",
description: "My action description",
schema: MyActionSchema,
})
async myAction(args: z.infer<typeof MyActionSchema>): Promise<string> {
return args.myField;
}
supportsNetwork = (network: Network) => true;
}
export const myActionProvider = () => new MyActionProvider();
Adding Actions to your Action Provider that use a Wallet Provider
Actions that use a wallet provider can be defined as instance methods on the action provider class with the @CreateAction decorator that have a WalletProvider as the first parameter.
class MyActionProvider extends ActionProvider<WalletProvider> {
constructor() {
super("my-action-provider", []);
}
@CreateAction({
name: "my-action",
description: "My action description",
schema: MyActionSchema,
})
async myAction(walletProvider: WalletProvider, args: z.infer<typeof MyActionSchema>): Promise<string> {
return walletProvider.signMessage(args.myField);
}
supportsNetwork = (network: Network) => true;
}
Adding an Action Provider to your AgentKit instance.
This gives your agent access to the actions defined in the action provider.
const agentKit = new AgentKit({
cdpApiKeyName: "CDP API KEY NAME",
cdpApiKeyPrivate: "CDP API KEY PRIVATE KEY",
actionProviders: [myActionProvider()],
});
EVM Wallet Providers
Wallet providers give an agent access to a wallet. AgentKit currently supports the following wallet providers:
EVM:
CdpWalletProvider
The CdpWalletProvider is a wallet provider that uses the Coinbase Developer Platform (CDP) API Wallet.
Network Configuration
The CdpWalletProvider can be configured to use a specific network by passing the networkId parameter to the configureWithWallet method. The networkId is the ID of the network you want to use. You can find a list of supported networks on the CDP API docs.
import { CdpWalletProvider } from "@coinbase/agentkit";
const walletProvider = await CdpWalletProvider.configureWithWallet({
apiKeyName: "CDP API KEY NAME",
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
networkId: "base-mainnet",
});
Configuring from an existing CDP API Wallet
If you already have a CDP API Wallet, you can configure the CdpWalletProvider by passing the wallet parameter to the configureWithWallet method.
import { CdpWalletProvider } from "@coinbase/agentkit";
import { Wallet } from "@coinbase/coinbase-sdk";
const walletProvider = await CdpWalletProvider.configureWithWallet({
wallet,
apiKeyName: "CDP API KEY NAME",
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
});
Configuring from a mnemonic phrase
The CdpWalletProvider can be configured from a mnemonic phrase by passing the mnemonicPhrase parameter to the configureWithWallet method.
import { CdpWalletProvider } from "@coinbase/agentkit";
const walletProvider = await CdpWalletProvider.configureWithWallet({
mnemonicPhrase: "MNEMONIC PHRASE",
});
Exporting a wallet
The CdpWalletProvider can export a wallet by calling the exportWallet method.
import { CdpWalletProvider } from "@coinbase/agentkit";
const walletProvider = await CdpWalletProvider.configureWithWallet({
mnemonicPhrase: "MNEMONIC PHRASE",
});
const walletData = await walletProvider.exportWallet();
Importing a wallet from WalletData JSON string
The CdpWalletProvider can import a wallet from a WalletData JSON string by passing the cdpWalletData parameter to the configureWithWallet method.
import { CdpWalletProvider } from "@coinbase/agentkit";
const walletProvider = await CdpWalletProvider.configureWithWallet({
cdpWalletData: "WALLET DATA JSON STRING",
apiKeyName: "CDP API KEY NAME",
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
});
Configuring CdpWalletProvider gas parameters
The CdpWalletProvider also exposes parameters for effecting the gas calculations.
import { CdpWalletProvider } from "@coinbase/agentkit";
const walletProvider = await CdpWalletProvider.configureWithWallet({
cdpWalletData: "WALLET DATA JSON STRING",
apiKeyName: "CDP API KEY NAME",
apiKeyPrivate: "CDP API KEY PRIVATE KEY",
gas: {
gasLimitMultiplier: 2.0,
feePerGasMultiplier: 2.0,
}
});
Note: Gas parameters only impact the walletProvider.sendTransaction behavior. Actions that do not rely on direct transaction calls, such as deploy_token, deploy_contract, and native_transfer, remain unaffected.
ViemWalletProvider
The ViemWalletProvider is a wallet provider that uses the Viem library. It is useful for interacting with any EVM-compatible chain.
import { ViemWalletProvider } from "@coinbase/agentkit";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";
import { http } from "viem/transports";
import { createWalletClient } from "viem";
const account = privateKeyToAccount(
"0x4c0883a69102937d6231471b5dbb6208ffd70c02a813d7f2da1c54f2e3be9f38",
);
const client = createWalletClient({
account,
chain: baseSepolia,
transport: http(),
});
const walletProvider = new ViemWalletProvider(client);
Configuring ViemWalletProvider gas parameters
The ViemWalletProvider also exposes parameters for effecting the gas calculations.
import { ViemWalletProvider } from "@coinbase/agentkit";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";
import { http } from "viem/transports";
import { createWalletClient } from "viem";
const account = privateKeyToAccount(
"0x4c0883a69102937d6231471b5dbb6208ffd70c02a813d7f2da1c54f2e3be9f38",
);
const client = createWalletClient({
account,
chain: baseSepolia,
transport: http(),
});
const walletProvider = new ViemWalletProvider(client, {
gasLimitMultiplier: 2.0,
feePerGasMultiplier: 2.0,
});
PrivyWalletProvider
The PrivyWalletProvider is a wallet provider that uses Privy Server Wallets. This implementation extends the ViemWalletProvider.
import { PrivyWalletProvider, PrivyWalletConfig } from "@coinbase/agentkit";
const config: PrivyWalletConfig = {
appId: "PRIVY_APP_ID",
appSecret: "PRIVY_APP_SECRET",
chainId: "84532",
walletId: "PRIVY_WALLET_ID",
authorizationPrivateKey: PRIVY_WALLET_AUTHORIZATION_PRIVATE_KEY,
authorizationKeyId: PRIVY_WALLET_AUTHORIZATION_KEY_ID,
};
const walletProvider = await PrivyWalletProvider.configureWithWallet(config);
Authorization Keys
Privy offers the option to use authorization keys to secure your server wallets.
You can manage authorization keys from your Privy dashboard.
When using authorization keys, you must provide the authorizationPrivateKey and authorizationKeyId parameters to the configureWithWallet method if you are creating a new wallet. Please note that when creating a key, if you enable "Create and modify wallets", you will be required to use that key when creating new wallets via the PrivyWalletProvider.
Exporting Privy Wallet information
The PrivyWalletProvider can export wallet information by calling the exportWallet method.
const walletData = await walletProvider.exportWallet();
{
walletId: string;
authorizationKey: string | undefined;
chainId: string | undefined;
}
SVM Wallet Providers
SVM:
SolanaKeypairWalletProvider
The SolanaKeypairWalletProvider is a wallet provider that uses the API Solana web3.js.
NOTE: It is highly recommended to use a dedicated RPC provider. See here for more info on Solana RPC infrastructure, and see here for instructions on configuring SolanaKeypairWalletProvider with a custom RPC URL.
Solana Network Configuration
The SolanaKeypairWalletProvider can be configured to use a specific network by passing the networkId parameter to the fromNetwork method. The networkId is the ID of the Solana network you want to use. Valid values are solana-mainnet, solana-devnet and solana-testnet.
The default RPC endpoints for each network are as follows:
solana-mainnet: https://api.mainnet-beta.solana.com
solana-devnet: https://api.devnet.solana.com
solana-testnet: https://api.testnet.solana.com
import { SOLANA_NETWORK_ID, SolanaKeypairWalletProvider } from "@coinbase/agentkit";
const privateKey = process.env.SOLANA_PRIVATE_KEY;
const network = process.env.NETWORK_ID as SOLANA_NETWORK_ID;
const walletProvider = await SolanaKeypairWalletProvider.fromNetwork(network, privateKey);
RPC URL Configuration
The SolanaKeypairWalletProvider can be configured to use a specific RPC url by passing the rpcUrl parameter to the fromRpcUrl method. The rpcUrl will determine the network you are using.
import { SOLANA_NETWORK_ID, SolanaKeypairWalletProvider } from "@coinbase/agentkit";
const privateKey = process.env.SOLANA_PRIVATE_KEY;
const rpcUrl = process.env.SOLANA_RPC_URL;
const walletProvider = await SolanaKeypairWalletProvider.fromRpcUrl(network, privateKey);
PrivyWalletProvider (Solana)
The PrivyWalletProvider is a wallet provider that uses Privy Server Wallets.
NOTE: It is highly recommended to use a dedicated RPC provider. See here for more info on Solana RPC infrastructure, and see here for instructions on configuring PrivyWalletProvider with a custom RPC URL.
import { PrivyWalletProvider, PrivyWalletConfig } from "@coinbase/agentkit";
const config: PrivyWalletConfig = {
appId: "PRIVY_APP_ID",
appSecret: "PRIVY_APP_SECRET",
chainType: "solana",
networkId: "solana-devnet",
walletId: "PRIVY_WALLET_ID",
authorizationPrivateKey: PRIVY_WALLET_AUTHORIZATION_PRIVATE_KEY,
authorizationKeyId: PRIVY_WALLET_AUTHORIZATION_KEY_ID,
};
const walletProvider = await PrivyWalletProvider.configureWithWallet(config);
Connection Configuration
Optionally, you can configure your own @solana/web3.js connection by passing the connection parameter to the configureWithWallet method.
import { PrivyWalletProvider, PrivyWalletConfig } from "@coinbase/agentkit";
const connection = new Connection("YOUR_RPC_URL");
const config: PrivyWalletConfig = {
appId: "PRIVY_APP_ID",
appSecret: "PRIVY_APP_SECRET",
connection,
chainType: "solana",
networkId: "solana-devnet",
walletId: "PRIVY_WALLET_ID",
authorizationPrivateKey: PRIVY_WALLET_AUTHORIZATION_PRIVATE_KEY,
authorizationKeyId: PRIVY_WALLET_AUTHORIZATION_KEY_ID,
};
const walletProvider = await PrivyWalletProvider.configureWithWallet(config);
Authorization Keys
Privy offers the option to use authorization keys to secure your server wallets.
You can manage authorization keys from your Privy dashboard.
When using authorization keys, you must provide the authorizationPrivateKey and authorizationKeyId parameters to the configureWithWallet method if you are creating a new wallet. Please note that when creating a key, if you enable "Create and modify wallets", you will be required to use that key when creating new wallets via the PrivyWalletProvider.
Exporting Privy Wallet information
The PrivyWalletProvider can export wallet information by calling the exportWallet method.
const walletData = await walletProvider.exportWallet();
{
walletId: string;
authorizationKey: string | undefined;
networkId: string | undefined;
}
Contributing
See CONTRIBUTING.md for more information.