Skip to content

Commit d782f2f

Browse files
humanagentfabrizio guespe
andauthored
Update xmtp agent guide (#33)
* Add yarn integrity file and update chat agent guide * Add package-lock.json for consistent dependency management * Remove unused package lock and yarn integrity files * Add link to Cursor Rules for better agent guidance * Switch package manager from yarn to npm for consistency --------- Co-authored-by: fabrizio guespe <fabrizioguespe@Fabri-M3-2.local>
1 parent 3cf378a commit d782f2f

1 file changed

Lines changed: 100 additions & 80 deletions

File tree

docs/wallet-app/guides/chat-agents.mdx

Lines changed: 100 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Real Examples:
1919

2020
• AI Trading Companion: Message "buy $100 ETH when it hits $3,000" and your agent executes trades 24/7 while you sleep.
2121

22-
• Travel Planning Bot: "Book flight LAX to NYC under $300" and get instant booking with crypto payments, all in your group chat
22+
• Travel Planning Agent: "Book flight LAX to NYC under $300" and get instant booking with crypto payments, all in your group chat
2323

2424
• Coinbase Wallet & XMTP are combining AI, crypto, and mini apps with secure messaging – to unlock use-cases never before possible. Secure group chats & DMs are the new surface area for developers.
2525

@@ -28,7 +28,7 @@ Real Examples:
2828
This guide will walk you through creating, testing, and deploying your first XMTP messaging agent. By the end, you'll have a fully functional agent that can send and receive messages on the XMTP messaging network.
2929

3030
**Prerequisites**
31-
• Node.js (v16 or higher)
31+
• Node.js (v20 or higher)
3232
• Git
3333
• A code editor
3434
• Basic knowledge of JavaScript/TypeScript
@@ -37,109 +37,116 @@ This guide will walk you through creating, testing, and deploying your first XMT
3737

3838
- [Getting Started with XMTP (Video)](https://www.youtube.com/watch?v=djRLnWUvwIA)
3939
- [Building Agents on XMTP](https://github.com/ephemeraHQ/xmtp-agent-examples)
40+
- [Cursor Rules](https://github.com/ephemeraHQ/xmtp-agent-examples/blob/main/.cursor/rules/xmtp.md)
4041
- [XMTP Documentation](https://docs.xmtp.org/)
41-
- [Coinbase AgentKit](https://github.com/coinbase/agentkit)
42-
- [Coinbase Developer Platform](https://docs.cdp.coinbase.com/)
4342
- [Faucets](https://portal.cdp.coinbase.com/products/faucet)
44-
- [OnchainKit](https://onchainkit.xyz/)
4543

4644
**STEP 1: SET UP YOUR DEVELOPMENT ENVIRONMENT**
4745

48-
Clone the XMTP Bot Starter Template:
46+
Clone the XMTP Agent Examples Repository:
4947

50-
```javascript
51-
git clone https://github.com/xmtp/bot-starter
52-
cd bot-starter
48+
```bash
49+
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
50+
cd xmtp-agent-examples
5351
```
5452

55-
Alternative: create from scratch:
53+
**STEP 2: INSTALL DEPENDENCIES**
5654

57-
```javascript
58-
mkdir my-xmtp-bot
59-
cd my-xmtp-bot
60-
npm init -y
55+
Install all required packages:
56+
57+
```bash
58+
npm
6159
```
6260

63-
**STEP 2: INSTALL DEPENDENCIES**
61+
**STEP 3: GENERATE KEYS FOR YOUR AGENT**
6462

65-
Install the required XMTP SDK and other dependencies:
63+
Run the key generation script to create your agent's wallet:
6664

67-
```javascript
68-
npm install @xmtp/xmtp-js ethers dotenv
65+
```bash
66+
npm gen:keys
6967
```
7068

71-
For TypeScript support (recommended):
69+
This creates a .env file with:
7270

73-
```javascript
74-
npm install -D typescript @types/node ts-node
71+
```bash
72+
WALLET_KEY=0x... # Your agent's private key
73+
ENCRYPTION_KEY=... # Encryption key for local database
74+
XMTP_ENV=dev # Environment (local, dev, production)
7575
```
7676

77-
**STEP 3: GENERATE KEYS FOR YOUR BOT**
77+
**STEP 4: WRITE YOUR AGENT LOGIC**
7878

79-
Run the key generation script to create your bot's wallet:
79+
Create a basic agent that responds to messages:
8080

8181
```javascript
82-
npm run gen:keys
82+
// import the xmtp sdk
83+
import { Client, type XmtpEnv, type Signer } from "@xmtp/node-sdk";
84+
85+
// encryption key, must be consistent across runs
86+
const encryptionKey: Uint8Array = ...;
87+
const signer: Signer = ...;
88+
const env: XmtpEnv = "dev";
89+
90+
// create the client
91+
const client = await Client.create(signer, {encryptionKey, env });
92+
// sync the client to get the latest messages
93+
await client.conversations.sync();
94+
95+
// listen to all messages
96+
const stream = await client.conversations.streamAllMessages();
97+
for await (const message of stream) {
98+
// ignore messages from the agent
99+
if (message?.senderInboxId === client.inboxId ) continue;
100+
// get the conversation by id
101+
const conversation = await client.conversations.getConversationById(message.conversationId);
102+
// send a message from the agent
103+
await conversation.send("gm");
104+
}
83105
```
84106
85-
This creates a .env file with:
107+
Then run your agent:
86108
87-
```javascript
88-
XMTP_ENV=dev
89-
PRIVATE_KEY=0x... (Your bot's private key)
90-
PUBLIC_ADDRESS=0x... (Your bot's public address)
109+
```bash
110+
npm dev
91111
```
92112
93-
IMPORTANT:
94-
• Keep your PRIVATE_KEY secure and never commit it to version control
95-
• Start with XMTP_ENV=dev for testing
96-
• Switch to XMTP_ENV=production when ready to go live
97-
98-
**STEP 4: WRITE YOUR BOT LOGIC**
99-
100-
Create a basic bot that responds to messages:
101-
102-
```javascript
103-
// bot.js
104-
import { Client } from '@xmtp/xmtp-js'
105-
import { Wallet } from 'ethers'
106-
107-
const wallet = new Wallet(process.env.PRIVATE_KEY)
108-
const xmtp = await Client.create(wallet, { env: process.env.XMTP_ENV })
113+
### Getting the address of a user
109114
110-
// Listen for new conversations
111-
for await (const conversation of await xmtp.conversations.stream()) {
112-
console.log(`New conversation started with ${conversation.peerAddress}`)
115+
Each user has a unique inboxId for retrieving their associated addresses (identifiers). One inboxId can have multiple identifiers like passkeys or EVM wallet addresses.
113116
114-
// Listen for messages in this conversation
115-
for await (const message of await conversation.streamMessages()) {
116-
if (message.senderAddress === xmtp.address) continue // Skip own messages
117+
```tsx
118+
const inboxState = await client.preferences.inboxStateFromInboxIds([
119+
message.senderInboxId,
120+
]);
121+
const addressFromInboxId = inboxState[0].identifiers[0].identifier;
122+
```
117123
118-
console.log(`Received: ${message.content}`)
124+
You can also explore example implementations in the `/examples` folder, including:
119125
120-
// Simple echo bot response
121-
await conversation.send(`You said: ${message.content}`)
122-
}
123-
}
126+
- [xmtp-gm](https://github.com/ephemeraHQ/xmtp-agent-examples/tree/main/examples/xmtp-gm): A simple agent that replies to all text messages with "gm".
127+
- [xmtp-gpt](https://github.com/ephemeraHQ/xmtp-agent-examples/tree/main/examples/xmtp-gpt): An example using GPT API's to answer messages.
128+
- [xmtp-nft-gated-group](https://github.com/ephemeraHQ/xmtp-agent-examples/tree/main/examples/xmtp-nft-gated-group): Add members to a group based on an NFT
129+
- [xmtp-coinbase-agentkit](https://github.com/ephemeraHQ/xmtp-agent-examples/tree/main/examples/xmtp-coinbase-agentkit): Agent that uses a CDP for gassless USDC on base
130+
- [xmtp-transactions](https://github.com/ephemeraHQ/xmtp-agent-examples/tree/main/examples/xmtp-transactions): Use XMTP content types to send transactions
131+
- [xmtp-smart-wallet](https://github.com/ephemeraHQ/xmtp-agent-examples/tree/main/examples/xmtp-smart-wallet): Agent that uses a smart wallet to send messages
124132
125-
```
126133
127-
**STEP 5: TEST YOUR BOT**
134+
**STEP 5: TEST YOUR AGENT**
128135
129136
**Development Testing**
130137
131-
1\. Start your bot:
138+
1\. Start your agent:
132139
133140
```javascript
134-
npm start
141+
npm dev
135142
```
136143
137144
2\. Test on [xmtp.chat:](https://xmtp.chat/conversations)
138145
• Go to https://xmtp.chat
139146
• Connect your personal wallet
140147
• Switch to Dev environment in settings
141-
• Start a new conversation with your bot's public address (from .env)
142-
• Send a test message and verify the bot responds
148+
• Start a new conversation with your agent's public address (from .env)
149+
• Send a test message and verify the agent responds
143150
144151
**Production Testing**
145152
@@ -152,36 +159,37 @@ XMTP_ENV=production
152159
2\. Test on Coinbase Wallet:
153160
• Open Coinbase Wallet mobile app
154161
• Go to messaging
155-
• Start conversation with your bot's address
162+
• Start conversation with your agent's address
156163
• Verify functionality
157164
158-
**STEP 6: GET A BASENAME FOR YOUR BOT**
165+
**STEP 6: GET A BASENAME FOR YOUR AGENT**
159166
160-
Give your bot a human-readable name:
167+
Give your agent a human-readable name:
161168
162-
**1\. Import bot wallet to Coinbase Wallet extension:**
169+
**1\. Import agent wallet to Coinbase Wallet extension:**
163170
• Install Coinbase Wallet browser extension
164-
• Import using your bot's private key
171+
• Import using your agent's private key
165172
166173
**2\. Purchase a basename:**
167174
• Visit https://base.org/names
168-
• Connect your bot's wallet
169-
• Search and purchase your desired basename (e.g., mybot.base.eth)
175+
• Connect your agent's wallet
176+
• Search and purchase your desired basename (e.g., myagent.base.eth)
170177
• Set as primary name
171178
172179
**3\. Verify setup:**
173-
• Your bot can now be reached via the basename instead of the long address
174-
• Users can message mybot.base.eth instead of 0x123...
180+
• Your agent can now be reached via the basename instead of the long address
181+
• Users can message myagent.base.eth instead of 0x123...
175182
176-
**STEP 7: DEPLOY YOUR BOT**
183+
**STEP 7: DEPLOY YOUR AGENT**
177184
178185
**Option 1: Railway (Recommended)**
179186
180187
• Visit https://railway.app
181188
• Connect your GitHub repository
182189
• Add environment variables in Railway dashboard:
183190
\- XMTP_ENV=production
184-
\- PRIVATE_KEY=your_bot_private_key
191+
\- WALLET_KEY=your_agent_private_key
192+
\- ENCRYPTION_KEY=your_agent_encryption_key
185193
• Deploy and monitor logs
186194
187195
**Option 2: Other Platforms**
@@ -193,22 +201,34 @@ Heroku, Vercel, or any Node.js hosting platform:
193201
**STEP 8: MONITOR AND MAINTAIN**
194202
195203
**Best Practices**
196-
1\. Logging: Add comprehensive logging to track bot activity
204+
1\. Logging: Add comprehensive logging to track agent activity
197205
2\. Error Handling: Implement try-catch blocks for network issues
198-
3\. Rate Limiting: Respect XMTP rate limits in your bot logic
206+
3\. Rate Limiting: Respect XMTP rate limits in your agent logic
199207
4\. Security: Never expose private keys; use environment variables
200208
201209
**Monitoring**
202-
Add to your bot for basic monitoring:
210+
211+
Add to your agent for basic monitoring:
203212
204213
```javascript
205-
- console.log(\`Bot started. Address: ${xmtp.address}\`)
206-
- console.log(\`Environment: ${process.env.XMTP_ENV}\`)
207-
- console.log(\`Listening for messages...\`)
214+
const inboxState = await client.preferences.inboxState();
215+
const address = inboxState.identifiers[0].identifier;
216+
const inboxId = client.inboxId;
217+
const installationId = client.installationId;
218+
const conversations = await client.conversations.list();
219+
220+
console.log(`
221+
✓ XMTP Client:
222+
• InboxId: ${inboxId}
223+
• Address: ${address}
224+
• Conversations: ${conversations.length}
225+
• Installations: ${inboxState.installations.length}
226+
• InstallationId: ${installationId}
227+
• Network: ${process.env.XMTP_ENV}`);
208228
```
209229
210230
## Getting featured
211231
212-
Fill out the form [here](https://app.deform.cc/form/52b71db4-bfa2-4ef5-a954-76c66250bdd2/?page_number=0) to submit your agent for review. If approved, your bot will be featured in Coinbase Wallet. You will hear back from us within 5 business days.
232+
Fill out the form [here](https://app.deform.cc/form/52b71db4-bfa2-4ef5-a954-76c66250bdd2/?page_number=0) to submit your agent for review. If approved, your agent will be featured in Coinbase Wallet. You will hear back from us within 5 business days.
213233
214234
Need help or have feature requests? Visit [https://community.xmtp.org/](https://community.xmtp.org/)

0 commit comments

Comments
 (0)