1. Reading Data on VeChainThor
Before building the AI agent, you need to understand how to interact with VeChainThor blockchain.
1.1 Setting Up the VeChain SDK
VeChain SDK provides all the tools needed to interact with VeChainThor.
Install the SDK packages:
npm install @vechain/sdk-core npm install @vechain/sdk-network
npm install @vechain/sdk-core npm install @vechain/sdk-network
1.2 Connect To the Network
VeChain has two networks: mainnet (for real transactions) and testnet (for development and testing). For this tutorial, we'll connect to mainnet, since we will use AI Agents to fetch data.
import { ThorClient } from '@vechain/sdk-network'; // Connect to mainnet const thor = ThorClient.at("https://mainnet.vechain.org"); // For testnet, use: // const thorClient = ThorClient.fromUrl('https://testnet.vechain.org');
import { ThorClient } from '@vechain/sdk-network'; // Connect to mainnet const thor = ThorClient.at("https://mainnet.vechain.org"); // For testnet, use: // const thorClient = ThorClient.fromUrl('https://testnet.vechain.org');
1.3 Reading Account Information
An account can be classified as either an externally owned account or a smart contract.
Externally owned account - consists of private keys generated in wallets.
Smart contracts - they are deployed with specific program codes.
The difference between the two can be made by examining an account's hasCode flag. If it's true, it indicates that a smart contract is deployed at that address. For smart contracts, the bytecode can also be retrieved.
An account has three key attributes:
hasCode - indicates a smart contract at the address
balance - shows the VET balance
energy - shows the VTHO balance
This is how querying Account Information looks like:
import { ThorClient } from '@vechain/sdk-network'; const thor = ThorClient.at('https://testnet.vechain.org'); const account = await thor.accounts.getAccount( '0x01d6b50b31c18d7f81ede43935cadf79901b0ea0' ); console.log(account); console.log('VET Balance', BigInt(account.balance)/1000000000000000000n ); console.log('VTHO Balance', BigInt(account.energy)/1000000000000000000n );
import { ThorClient } from '@vechain/sdk-network'; const thor = ThorClient.at('https://testnet.vechain.org'); const account = await thor.accounts.getAccount( '0x01d6b50b31c18d7f81ede43935cadf79901b0ea0' ); console.log(account); console.log('VET Balance', BigInt(account.balance)/1000000000000000000n ); console.log('VTHO Balance', BigInt(account.energy)/1000000000000000000n );
And here’s an example response:
j { balance: '0x0', energy: '0x0', hasCode: true } VET Balance 1000n VTHO Balance 500n
j { balance: '0x0', energy: '0x0', hasCode: true } VET Balance 1000n VTHO Balance 500n
1.4 How To Read Transactions
Transactions are changes on the blockchain, which can be a transfer of VET or the execution of the contract’s function.
A transaction on VeChain consists of two parts:
Transaction Body
Transaction Receipt
Reading both will give you a full understanding of the transaction.
Transaction Body
Transaction body contains:
id - Unique transaction identifier.
chainTag - Identifier of the Blockchain.
blockRef - Block on which the call was made.
expiration - The transaction must be added within this many blocks, or it will fail.
origin - Sender of the transaction.
clauses - Instructions that need to be executed, including recipients, value, and data.
meta - Block metadata (BlockID, BlockNumber, and BlockTimestamp).
This is how you query transaction information:
import { ThorClient } from '@vechain/sdk-network'; const thor = ThorClient.at('https://mainnet.vechain.org'); const tx = await thor.transactions.getTransaction( '0x0326f6e9dc11e2754c3618cff79fd23e7f25305626df7856551518aefd534ed9' ); console.log(tx);
import { ThorClient } from '@vechain/sdk-network'; const thor = ThorClient.at('https://mainnet.vechain.org'); const tx = await thor.transactions.getTransaction( '0x0326f6e9dc11e2754c3618cff79fd23e7f25305626df7856551518aefd534ed9' ); console.log(tx);
Transaction Receipt
The transaction receipt shows the outcome and blockchain changes.
It contains:
reverted - Indicates if the transaction failed.
gasUsed - The amount of gas that was used to complete the transaction.
outputs - Events that have been triggered as a result of the transaction.
contractAddress - Address of a newly deployed contract (if there are any).
To request a transaction’s receipt, you can call the getTransactionReceipt() method instead. Just replace your `getTransaction()` call with this snippet:
const tx = await thor.transactions.getTransactionReceipt( '0x1228fa0f179d046c0245d2fe9f4cea5f93df0c74c25e7561f69325bc137b642e' ); console.log(tx);
const tx = await thor.transactions.getTransactionReceipt( '0x1228fa0f179d046c0245d2fe9f4cea5f93df0c74c25e7561f69325bc137b642e' ); console.log(tx);
Example of a Successful Transaction:
{ type: 81, gasUsed: 148354, gasPayer: '0x75e11b7e879529005585971bcdec60fc11262dd5', paid: '0x14d5f5b24594c430', reward: '0x3f5ded2f7f8430', reverted: false, meta: { blockID: '0x0151f1f6378b680cbd87c147aa305fd46778c5fc41258de8bf2632303ae897aa', blockNumber: 22147574, blockTimestamp: 1752005110, txID: '0x1228fa0f179d046c0245d2fe9f4cea5f93df0c74c25e7561f69325bc137b642e', txOrigin: '0x75e11b7e879529005585971bcdec60fc11262dd5' }, outputs: [ { contractAddress: null, events: [Array], transfers: [Array] } ] }
{ type: 81, gasUsed: 148354, gasPayer: '0x75e11b7e879529005585971bcdec60fc11262dd5', paid: '0x14d5f5b24594c430', reward: '0x3f5ded2f7f8430', reverted: false, meta: { blockID: '0x0151f1f6378b680cbd87c147aa305fd46778c5fc41258de8bf2632303ae897aa', blockNumber: 22147574, blockTimestamp: 1752005110, txID: '0x1228fa0f179d046c0245d2fe9f4cea5f93df0c74c25e7561f69325bc137b642e', txOrigin: '0x75e11b7e879529005585971bcdec60fc11262dd5' }, outputs: [ { contractAddress: null, events: [Array], transfers: [Array] } ] }
1.1 Setting Up the VeChain SDK
VeChain SDK provides all the tools needed to interact with VeChainThor.
Install the SDK packages:
npm install @vechain/sdk-core npm install @vechain/sdk-network
1.2 Connect To the Network
VeChain has two networks: mainnet (for real transactions) and testnet (for development and testing). For this tutorial, we'll connect to mainnet, since we will use AI Agents to fetch data.
import { ThorClient } from '@vechain/sdk-network'; // Connect to mainnet const thor = ThorClient.at("https://mainnet.vechain.org"); // For testnet, use: // const thorClient = ThorClient.fromUrl('https://testnet.vechain.org');
1.3 Reading Account Information
An account can be classified as either an externally owned account or a smart contract.
Externally owned account - consists of private keys generated in wallets.
Smart contracts - they are deployed with specific program codes.
The difference between the two can be made by examining an account's hasCode flag. If it's true, it indicates that a smart contract is deployed at that address. For smart contracts, the bytecode can also be retrieved.
An account has three key attributes:
hasCode - indicates a smart contract at the address
balance - shows the VET balance
energy - shows the VTHO balance
This is how querying Account Information looks like:
import { ThorClient } from '@vechain/sdk-network'; const thor = ThorClient.at('https://testnet.vechain.org'); const account = await thor.accounts.getAccount( '0x01d6b50b31c18d7f81ede43935cadf79901b0ea0' ); console.log(account); console.log('VET Balance', BigInt(account.balance)/1000000000000000000n ); console.log('VTHO Balance', BigInt(account.energy)/1000000000000000000n );
And here’s an example response:
j { balance: '0x0', energy: '0x0', hasCode: true } VET Balance 1000n VTHO Balance 500n
1.4 How To Read Transactions
Transactions are changes on the blockchain, which can be a transfer of VET or the execution of the contract’s function.
A transaction on VeChain consists of two parts:
Transaction Body
Transaction Receipt
Reading both will give you a full understanding of the transaction.
Transaction Body
Transaction body contains:
id - Unique transaction identifier.
chainTag - Identifier of the Blockchain.
blockRef - Block on which the call was made.
expiration - The transaction must be added within this many blocks, or it will fail.
origin - Sender of the transaction.
clauses - Instructions that need to be executed, including recipients, value, and data.
meta - Block metadata (BlockID, BlockNumber, and BlockTimestamp).
This is how you query transaction information:
import { ThorClient } from '@vechain/sdk-network'; const thor = ThorClient.at('https://mainnet.vechain.org'); const tx = await thor.transactions.getTransaction( '0x0326f6e9dc11e2754c3618cff79fd23e7f25305626df7856551518aefd534ed9' ); console.log(tx);
Transaction Receipt
The transaction receipt shows the outcome and blockchain changes.
It contains:
reverted - Indicates if the transaction failed.
gasUsed - The amount of gas that was used to complete the transaction.
outputs - Events that have been triggered as a result of the transaction.
contractAddress - Address of a newly deployed contract (if there are any).
To request a transaction’s receipt, you can call the getTransactionReceipt() method instead. Just replace your `getTransaction()` call with this snippet:
const tx = await thor.transactions.getTransactionReceipt( '0x1228fa0f179d046c0245d2fe9f4cea5f93df0c74c25e7561f69325bc137b642e' ); console.log(tx);
Example of a Successful Transaction:
{ type: 81, gasUsed: 148354, gasPayer: '0x75e11b7e879529005585971bcdec60fc11262dd5', paid: '0x14d5f5b24594c430', reward: '0x3f5ded2f7f8430', reverted: false, meta: { blockID: '0x0151f1f6378b680cbd87c147aa305fd46778c5fc41258de8bf2632303ae897aa', blockNumber: 22147574, blockTimestamp: 1752005110, txID: '0x1228fa0f179d046c0245d2fe9f4cea5f93df0c74c25e7561f69325bc137b642e', txOrigin: '0x75e11b7e879529005585971bcdec60fc11262dd5' }, outputs: [ { contractAddress: null, events: [Array], transfers: [Array] } ] }
Join our Telegram