Conversational Agent Examples
This chapter gives you complete, runnable examples you can copy/paste. Each example assumes you’ve followed Getting Started and set the required env vars.
Basic Agent Registration and Communication
Complete Agent Workflow
import { ConversationalAgent } from '@hashgraphonline/conversational-agent';
import dotenv from 'dotenv';
dotenv.config();
async function basicAgentExample() {
// 1. Create the conversational agent
const agent = new ConversationalAgent({
accountId: process.env.HEDERA_ACCOUNT_ID!,
privateKey: process.env.HEDERA_PRIVATE_KEY!,
network: 'testnet',
openAIApiKey: process.env.OPENAI_API_KEY!,
openAIModelName: 'gpt-4o',
verbose: true
});
// 2. Initialize (automatically detects key type)
await agent.initialize();
// 3. Register as an agent
console.log('\n=== Registering Agent ===');
const registerResponse = await agent.processMessage(
"Register me as an AI assistant named HelperBot with description 'A helpful AI assistant' and ai tag"
);
console.log(registerResponse.response);
// 4. Find other agents
console.log('\n=== Finding Other Agents ===');
const findResponse = await agent.processMessage(
"Find all agents with ai tag"
);
console.log(findResponse.response);
// 5. Connect to another agent
console.log('\n=== Connecting to Agent ===');
const connectResponse = await agent.processMessage(
"Connect to agent 0.0.98765"
);
console.log(connectResponse.response);
// 6. Send a message
console.log('\n=== Sending Message ===');
const messageResponse = await agent.processMessage(
"Send 'Hello from HelperBot! Let's collaborate.' to connection 1"
);
console.log(messageResponse.response);
// 7. Check for messages
console.log('\n=== Checking Messages ===');
const checkResponse = await agent.processMessage(
"Check my messages"
);
console.log(checkResponse.response);
}
basicAgentExample().catch(console.error);
HCS-2 Registry Management
Creating and Managing Registries
async function registryExample() {
const agent = new ConversationalAgent({
accountId: process.env.HEDERA_ACCOUNT_ID!,
privateKey: process.env.HEDERA_PRIVATE_KEY!,
network: 'testnet',
openAIApiKey: process.env.OPENAI_API_KEY!,
openAIModelName: 'gpt-4o'
});
await agent.initialize();
// Create a new registry
console.log('\n=== Creating Registry ===');
const createResponse = await agent.processMessage(
"Create a new HCS-2 topic registry"
);
console.log(createResponse.response);
// Register an entry
console.log('\n=== Registering Entry ===');
const registerEntryResponse = await agent.processMessage(
"Register topic 0.0.98765 in registry 0.0.123456 with memo 'HelperBot profile'"
);
console.log(registerEntryResponse.response);
// Query entries
console.log('\n=== Querying Registry ===');
const queryResponse = await agent.processMessage(
"Query all registered topics from my registry"
);
console.log(queryResponse.response);
// Update an entry
console.log('\n=== Updating Entry ===');
const updateResponse = await agent.processMessage(
"Register updated version of topic 0.0.98765 in registry 0.0.123456"
);
console.log(updateResponse.response);
}
registryExample().catch(console.error);
Content Inscription
Inscribing Content and Creating Hashinals
async function inscriptionExample() {
const agent = new ConversationalAgent({
accountId: process.env.HEDERA_ACCOUNT_ID!,
privateKey: process.env.HEDERA_PRIVATE_KEY!,
network: 'testnet',
openAIApiKey: process.env.OPENAI_API_KEY!,
openAIModelName: 'gpt-4o'
});
await agent.initialize();
// Inscribe from URL
console.log('\n=== Inscribing from URL ===');
const urlResponse = await agent.processMessage(
"Inscribe the content from https://example.com/metadata.json"
);
console.log(urlResponse.response);
// Inscribe from file
console.log('\n=== Inscribing from File ===');
const fileResponse = await agent.processMessage(
"Inscribe the file at /path/to/document.pdf"
);
console.log(fileResponse.response);
// Create a Hashinal NFT
console.log('\n=== Creating Hashinal NFT ===');
const hashinalResponse = await agent.processMessage(
"Create a Hashinal NFT with name 'AI Generated Art' and description 'Created by HelperBot AI'"
);
console.log(hashinalResponse.response);
// Retrieve inscription details
console.log('\n=== Retrieving Inscription ===');
const retrieveResponse = await agent.processMessage(
"Get inscription details for job ID abc123"
);
console.log(retrieveResponse.response);
}
inscriptionExample().catch(console.error);
Using the CLI
Interactive CLI Mode
# Run the interactive CLI
pnpm cli
# With environment variables
export HEDERA_ACCOUNT_ID=0.0.12345
export HEDERA_PRIVATE_KEY=your-private-key
export OPENAI_API_KEY=sk-your-openai-key
pnpm cli
# With command line arguments
pnpm cli -- --account-id=0.0.12345 --private-key=... --openai-api-key=sk-...
The CLI provides a beautiful terminal interface where you can:
- Chat naturally with your agent
- See real-time transaction confirmations
- View formatted responses with colors and gradients
- Access all agent capabilities through conversation