Let's start by setting up a basic AI agent using SendAI's Solana Agent Kit that can perform actions on the Solana blockchain and that we can chat with:
Install dependencies
A modern IDE is recommended for this guide. We will use Cursor for this example which has integrated AI features and can help you with any errors you may encounter.
You will need to have node with version 23.x.x installed. Open an empty folder using vscode or cursor and run the following command in the terminal:
This guide requires Node.js version `23.x.x`. Install it using [nvm](https://github.com/nvm-sh/nvm):
node --version # Check current version
nvm install 23 # Install Node.js 23 if needed
nvm use 23 # Switch to Node.js 23
Note that we encode the private key to base58 before we parse it into the solana agent constructor in the script so you can just put the byte array [34,2,34...] here in the env file.
You can create a key using the following command:
solana-keygen grind --starts-with ai:1
And copy the contents into your .env file for SOLANA_PRIVATE_KEY.
The OPENAI_API_KEY is the key for the OpenAI API and you can find it in the OpenAI platform
The RPC url we just leave at devnet for now.
Create the agent script
Create a new file called agent.ts with the following content:
import { SolanaAgentKit, createSolanaTools } from "solana-agent-kit";
import { HumanMessage } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { MemorySaver } from "@langchain/langgraph";
import * as dotenv from "dotenv";
import bs58 from "bs58";
import * as readline from "readline";
dotenv.config();
async function initializeAgent() {
const llm = new ChatOpenAI({
modelName: "gpt-4-turbo-preview",
temperature: 0.7,
});
// Convert array string to actual array, then to Uint8Array, then to base58
const privateKeyArray = JSON.parse(process.env.SOLANA_PRIVATE_KEY!);
const privateKeyUint8 = new Uint8Array(privateKeyArray);
const privateKeyBase58 = bs58.encode(privateKeyUint8);
const solanaKit = new SolanaAgentKit(privateKeyBase58, process.env.RPC_URL!, {
OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
});
const tools = createSolanaTools(solanaKit);
const memory = new MemorySaver();
return createReactAgent({
llm,
tools,
checkpointSaver: memory,
});
}
async function runInteractiveChat() {
const agent = await initializeAgent();
const config = { configurable: { thread_id: "Solana Agent Kit!" } };
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Clear console and start chat with a small delay
setTimeout(() => {
console.clear(); // Clear any initialization messages
console.log("Chat with Solana Agent (type 'exit' to quit)");
console.log("--------------------------------------------");
askQuestion();
}, 100);
const askQuestion = () => {
rl.question("You: ", async (input) => {
if (input.toLowerCase() === "exit") {
rl.close();
return;
}
const stream = await agent.stream(
{
messages: [new HumanMessage(input)],
},
config,
);
process.stdout.write("Agent: ");
for await (const chunk of stream) {
if ("agent" in chunk) {
process.stdout.write(chunk.agent.messages[0].content);
} else if ("tools" in chunk) {
process.stdout.write(chunk.tools.messages[0].content);
}
}
console.log("\n--------------------------------------------");
askQuestion(); // Continue the conversation
});
};
}
runInteractiveChat().catch(console.error);
Run the agent
You can run this script using the following command:
npx tsx agent.ts
This will start a simple chat with the agent.
### Test basic functionality
You can now ask it to show you your solana balance and ask it to request some devnet sol:
Please show me my wallet address and request some devnet sol
If the devnet faucet is empty you can use the web faucet instead and paste in your solana address.
Create an NFT collection
Next ask the agent:
Please create me a NFT collection called trains with symbol TRN using this uri: https://scarlet-fancy-minnow-617.mypinata.cloud/ipfs/bafkreif43sp62yuy3sznrvqesk23tfnhpdck4npqowdwrhrzhsrgf5ao2e
Mint an NFT
After the collection is created, mint an NFT:
Please mint me an NFT into that collection using the name: Train1 and using this URI: https://scarlet-fancy-minnow-617.mypinata.cloud/ipfs/bafkreif43sp62yuy3sznrvqesk23tfnhpdck4npqowdwrhrzhsrgf5ao2e
This will mint you an NFT with the name Train1 and an image of a train.
You can also use any different metadata for your NFT which you can upload using pinata or any other storage provider. You should end up with something like this devnet train nft
Where to go from here?
You can now for example import the private key into your browser extension wallet to see the NFT.
You can ask the agent to show you all your NFTs. You will notice you will get an error for this action. This is because the default action to request assets uses the Helius Asset api to request assets so for that you would need to add a Helius API key to your .env file and pass it into the agent. pass it into the agent.