This example shows how to build XMTP agents using a helper pattern that separates XMTP logic from business logic.
xmtpAgent
.createAndStart(
{
walletKey: WALLET_KEY,
encryptionKey: ENCRYPTION_KEY,
env: XMTP_ENV,
},
(message: ProcessedMessage) => processMessage(message),
)
.catch(console.error);- Simple API: Just write a message processing function
- Automatic message filtering: Skips messages from the agent itself and non-text messages
- Stream retry mechanism: Automatically retries failed message streams with configurable retries (5 attempts with 5-second intervals)
- Address resolution: Converts inbox IDs to Ethereum addresses automatically
- Error handling: Graceful error handling for message processing and sending
- Node.js v20 or higher
- Yarn v4 or higher
Create a .env file:
WALLET_KEY= # the private key of the wallet
ENCRYPTION_KEY= # encryption key for the local database
XMTP_ENV=dev # local, dev, productionGenerate keys:
yarn gen:keys# Install dependencies
yarn install
# Generate keys (if needed)
yarn gen:keys
# Start the agent
yarn devThe helper encapsulates all XMTP complexity. You just write a function that processes messages:
function processMessage(message: ProcessedMessage): string {
console.log(`Received: ${message.content}`);
return "gm"; // Your response
}
// Start the agent
xmtpAgent.createAndStart(config, processMessage);That's it! The helper handles client initialization, message streaming, filtering, sending responses, and stream recovery if the connection fails.