3. Connecting your AI Agent to VeChain
Now you will learn how to connect them together by building actions that let your AI agent interact with VeChain blockchain.
3.1 Project Setup
Environment Configuration
Create a .env file in your project root. This file holds configuration that shouldn't be in your code (like API keys or network URLs).
Why separate environments?
Testnet is a practice blockchain where nothing is real. Mainnet is the real blockchain with real VET and real consequences. You can develop on mainnet or testnet because we are just fetching data.
The environment variables let you switch between them by changing one line instead of hunting through your code.
File Organization
Your project should look like this:
This organization separates concerns:
src/contains ElizaOS-specific codevechain/contains blockchain-specific code. (you will be adding all your VeChain-related tools here)If you switch blockchains later, you only change the
vechain/folder
3.2 Testing Your Agent
Build and Start:
What happens when you start:
ElizaOS reads your character file, initializes the runtime, calls your initCharacter() function which registers your actions, connects to OpenAI (or whatever AI provider you configured), and starts listening for messages.
You'll see console output showing initialization steps. If something fails (like a missing API key), it will show an error here.
Test Your Actions
Once running, test with these examples:
Test 1: Balance with address
Test 2: Balance without address
Test 3: Alias lookup
Test 4: Invalid address
You can see one of our developers test his AI Agent here.
Understanding Test Results
If the agent doesn't respond:
Check that the action is registered in
initCharacter()Add
console.log()statements in your validate function to see if it's being calledMake sure your keywords match what you're typing
If you get errors:
Read the error message carefully - it usually tells you what's wrong
Check the console for more detailed error logs
Verify your .env file has the right values
3.3 Adding Your Own Actions
Now that you understand the pattern, you can add any blockchain functionality you want. Here's the recipe:
1. Create the VeChain SDK function
First, figure out what blockchain data you need and write a function to fetch it. Use the VeChain SDK documentation as a reference.
For example, let’s say you want to get the latest deposit on Stargate. The simplest way will be to filter transactions sent `to` the Stargate smart contract.
To get the Stargate contract address, you go to VeChain Stats.
Just like we did at the start of this tutorial, we’ll create a new script in the ./vechain directory. We’ll use the `filterTransferLogs()` method:
Let’s unpack this. We’re using the vechain SDK to fetch data. For a more detailed step-by-step on how to use the SDK, check the Developer Fundamentals course.
We start by importing the ThorClient and initiating it within a export function:
We then move to setting up our filters for VET transfers. Note that:
we’re using the Stargate address as the `recipient`
we add a further filter to only look for transfers starting from block 20M. This limits the search, so we’re not looking for transfers from the genesis block.
Finally, we prepare the response to be easily readable by our agent:
2. Create the action
Now create the ElizaOS action that connects to your VeChain function:
3. Register it
Every new action you add to your agent needs to be included in the character initialization so it gets registered on launch.
To do this, add runtime.registerAction(yourAction); to the initCharacter function:
4. Test it
After creating your new action, rebuild your agent and test it with various inputs to make sure it works correctly.
To rebuild your agent you need to run this command:
Join our Telegram