@mixrpay/agent-sdk 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +356 -19
- package/dist/index.cjs +314 -1
- package/dist/index.d.cts +232 -2
- package/dist/index.d.ts +232 -2
- package/dist/index.js +314 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @mixrpay/agent-sdk
|
|
2
2
|
|
|
3
|
-
Enable AI agents to make payments.
|
|
3
|
+
Enable AI agents to make payments to MixrPay-powered APIs. Session-based authentication with spending limits, automatic payment handling, and Agent Runtime for agentic workflows.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -8,16 +8,7 @@ Enable AI agents to make payments.
|
|
|
8
8
|
npm install @mixrpay/agent-sdk
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
# .env
|
|
15
|
-
MIXRPAY_SESSION_KEY=sk_live_...
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
Get session keys from [mixrpay.com/wallet/sessions](https://www.mixrpay.com/wallet/sessions).
|
|
19
|
-
|
|
20
|
-
## Usage
|
|
11
|
+
## Quick Start
|
|
21
12
|
|
|
22
13
|
```typescript
|
|
23
14
|
import { AgentWallet } from '@mixrpay/agent-sdk';
|
|
@@ -26,6 +17,7 @@ const wallet = new AgentWallet({
|
|
|
26
17
|
sessionKey: process.env.MIXRPAY_SESSION_KEY!
|
|
27
18
|
});
|
|
28
19
|
|
|
20
|
+
// Call any MixrPay merchant API - payments handled automatically
|
|
29
21
|
const response = await wallet.callMerchantApi({
|
|
30
22
|
url: 'https://api.merchant.com/generate',
|
|
31
23
|
merchantPublicKey: 'pk_live_...',
|
|
@@ -36,35 +28,380 @@ const response = await wallet.callMerchantApi({
|
|
|
36
28
|
const data = await response.json();
|
|
37
29
|
```
|
|
38
30
|
|
|
31
|
+
Get session keys from [mixrpay.com/wallet/sessions](https://www.mixrpay.com/wallet/sessions).
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
| Feature | Description |
|
|
38
|
+
|---------|-------------|
|
|
39
|
+
| **Auto-Pay Fetch** | Drop-in `fetch()` replacement that handles 402 payments |
|
|
40
|
+
| **Session Management** | Create, list, and revoke spending sessions with merchants |
|
|
41
|
+
| **Agent Runtime** | Full agentic loop with LLM + tools + bundled billing |
|
|
42
|
+
| **MCP Tools** | Access web scraping, search, and other tools |
|
|
43
|
+
| **Spending Controls** | Client-side limits and payment tracking |
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Core Methods
|
|
48
|
+
|
|
49
|
+
### Auto-Pay Fetch
|
|
50
|
+
|
|
51
|
+
Use `fetch()` as a drop-in replacement - payments are handled automatically when servers return 402:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
// Just like native fetch, but handles 402 payments automatically
|
|
55
|
+
const response = await wallet.fetch('https://api.example.com/ai/endpoint', {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers: { 'Content-Type': 'application/json' },
|
|
58
|
+
body: JSON.stringify({ prompt: 'Hello world' })
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Call Merchant APIs
|
|
65
|
+
|
|
66
|
+
For MixrPay-integrated merchants, use the session-based approach:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const response = await wallet.callMerchantApi({
|
|
70
|
+
url: 'https://api.merchant.com/generate',
|
|
71
|
+
merchantPublicKey: 'pk_live_abc123...',
|
|
72
|
+
method: 'POST',
|
|
73
|
+
body: { prompt: 'Analyze this data' },
|
|
74
|
+
priceUsd: 0.05,
|
|
75
|
+
feature: 'ai-analysis', // Optional: for tracking
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Session Management
|
|
82
|
+
|
|
83
|
+
Sessions allow spending limits with specific merchants.
|
|
84
|
+
|
|
85
|
+
### Create or Get Session
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const session = await wallet.getOrCreateSession({
|
|
89
|
+
merchantPublicKey: 'pk_live_abc123...',
|
|
90
|
+
spendingLimitUsd: 25.00,
|
|
91
|
+
durationDays: 7,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
console.log(`Session: ${session.id}`);
|
|
95
|
+
console.log(`Remaining: $${session.remainingLimitUsd}`);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### List All Sessions
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const sessions = await wallet.listSessions();
|
|
102
|
+
|
|
103
|
+
for (const session of sessions) {
|
|
104
|
+
console.log(`${session.merchantName}: $${session.remainingLimitUsd} remaining`);
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Session Statistics
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
const stats = await wallet.getSessionStats();
|
|
112
|
+
|
|
113
|
+
console.log(`Active sessions: ${stats.activeCount}`);
|
|
114
|
+
console.log(`Total authorized: $${stats.totalAuthorizedUsd}`);
|
|
115
|
+
console.log(`Total remaining: $${stats.totalRemainingUsd}`);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Revoke Session
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
await wallet.revokeSession('sess_abc123');
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Charge Session Directly
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
const result = await wallet.chargeSession('sess_abc123', 0.05, {
|
|
128
|
+
feature: 'image-generation',
|
|
129
|
+
idempotencyKey: 'unique-request-id',
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
console.log(`Charged: $${result.amountUsd}`);
|
|
133
|
+
console.log(`Remaining: $${result.remainingSessionBalanceUsd}`);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Agent Runtime
|
|
139
|
+
|
|
140
|
+
Run full agentic workflows with LLM reasoning and tool execution. Costs are bundled into a single charge.
|
|
141
|
+
|
|
142
|
+
### Basic Agent Run
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const result = await wallet.runAgent({
|
|
146
|
+
sessionId: 'sess_abc123',
|
|
147
|
+
messages: [
|
|
148
|
+
{ role: 'user', content: 'Find AI startups in San Francisco and summarize their products' }
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
console.log(result.response);
|
|
153
|
+
console.log(`Cost: $${result.cost.totalUsd.toFixed(4)}`);
|
|
154
|
+
console.log(`Tools used: ${result.toolsUsed.join(', ')}`);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### With Custom Configuration
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const result = await wallet.runAgent({
|
|
161
|
+
sessionId: 'sess_abc123',
|
|
162
|
+
messages: [
|
|
163
|
+
{ role: 'user', content: 'Research quantum computing breakthroughs in 2024' }
|
|
164
|
+
],
|
|
165
|
+
config: {
|
|
166
|
+
model: 'gpt-4o', // LLM model
|
|
167
|
+
maxIterations: 15, // Max reasoning loops
|
|
168
|
+
tools: ['platform/exa-search', 'platform/firecrawl-scrape'],
|
|
169
|
+
systemPrompt: 'You are a research analyst. Be thorough and cite sources.',
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### With Streaming
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
await wallet.runAgent({
|
|
178
|
+
sessionId: 'sess_abc123',
|
|
179
|
+
messages: [{ role: 'user', content: 'Explain machine learning' }],
|
|
180
|
+
stream: true,
|
|
181
|
+
onEvent: (event) => {
|
|
182
|
+
switch (event.type) {
|
|
183
|
+
case 'llm_chunk':
|
|
184
|
+
process.stdout.write(event.delta);
|
|
185
|
+
break;
|
|
186
|
+
case 'tool_call':
|
|
187
|
+
console.log(`\nCalling: ${event.tool}`);
|
|
188
|
+
break;
|
|
189
|
+
case 'tool_result':
|
|
190
|
+
console.log(`Tool ${event.tool}: ${event.success ? 'success' : 'failed'}`);
|
|
191
|
+
break;
|
|
192
|
+
case 'complete':
|
|
193
|
+
console.log(`\n\nTotal cost: $${event.totalCostUsd.toFixed(4)}`);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Get Run Status
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
const status = await wallet.getAgentRunStatus('run_abc123', 'sess_xyz789');
|
|
204
|
+
|
|
205
|
+
console.log(`Status: ${status.status}`);
|
|
206
|
+
if (status.status === 'completed') {
|
|
207
|
+
console.log(status.response);
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## MCP Tools
|
|
214
|
+
|
|
215
|
+
Access external tools like web scraping and search.
|
|
216
|
+
|
|
217
|
+
### List Available Tools
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
const tools = await wallet.listMCPTools();
|
|
221
|
+
|
|
222
|
+
for (const tool of tools) {
|
|
223
|
+
console.log(`${tool.name}: $${tool.priceUsd} - ${tool.description}`);
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Call Tool (Direct Pay)
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
const result = await wallet.callMCPTool('firecrawl/scrape', {
|
|
231
|
+
url: 'https://example.com',
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
console.log(result.data);
|
|
235
|
+
console.log(`Charged: $${result.chargedUsd}`);
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Call Tool (With Session)
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
// Create session with tool provider
|
|
242
|
+
const session = await wallet.getOrCreateSession({
|
|
243
|
+
merchantPublicKey: 'pk_live_firecrawl_...',
|
|
244
|
+
spendingLimitUsd: 50,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// Use session for multiple calls
|
|
248
|
+
const result = await wallet.callMCPToolWithSession(
|
|
249
|
+
session.id,
|
|
250
|
+
'firecrawl/scrape',
|
|
251
|
+
{ url: 'https://example.com' }
|
|
252
|
+
);
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## Wallet Information
|
|
258
|
+
|
|
259
|
+
### Get Balance
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
const balance = await wallet.getBalance();
|
|
263
|
+
console.log(`Balance: $${balance.toFixed(2)}`);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Get Spending Stats
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
const stats = await wallet.getSpendingStats();
|
|
270
|
+
|
|
271
|
+
console.log(`Total spent: $${stats.totalSpentUsd}`);
|
|
272
|
+
console.log(`Transactions: ${stats.txCount}`);
|
|
273
|
+
console.log(`Daily remaining: $${stats.remainingDailyUsd ?? 'unlimited'}`);
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Get Session Key Info
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
const info = await wallet.getSessionKeyInfo();
|
|
280
|
+
|
|
281
|
+
console.log(`Daily limit: $${info.limits.dailyUsd}`);
|
|
282
|
+
console.log(`Expires: ${info.expiresAt}`);
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### Payment History
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
const payments = wallet.getPaymentHistory();
|
|
289
|
+
|
|
290
|
+
for (const payment of payments) {
|
|
291
|
+
console.log(`$${payment.amountUsd} to ${payment.recipient} - ${payment.description}`);
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
39
297
|
## Error Handling
|
|
40
298
|
|
|
41
299
|
```typescript
|
|
42
|
-
import {
|
|
300
|
+
import {
|
|
301
|
+
AgentWallet,
|
|
302
|
+
InsufficientBalanceError,
|
|
303
|
+
SessionLimitExceededError,
|
|
304
|
+
SessionExpiredError,
|
|
305
|
+
SessionRevokedError,
|
|
306
|
+
SpendingLimitExceededError,
|
|
307
|
+
PaymentFailedError,
|
|
308
|
+
} from '@mixrpay/agent-sdk';
|
|
43
309
|
|
|
44
310
|
try {
|
|
45
|
-
|
|
311
|
+
await wallet.callMerchantApi({ ... });
|
|
46
312
|
} catch (error) {
|
|
47
313
|
if (error instanceof InsufficientBalanceError) {
|
|
48
|
-
console.log(`
|
|
314
|
+
console.log(`Need $${error.required}, have $${error.available}`);
|
|
315
|
+
} else if (error instanceof SessionLimitExceededError) {
|
|
316
|
+
console.log(`Session limit: $${error.limit}, tried: $${error.attempted}`);
|
|
317
|
+
} else if (error instanceof SessionExpiredError) {
|
|
318
|
+
console.log(`Session ${error.sessionId} expired`);
|
|
319
|
+
} else if (error instanceof SpendingLimitExceededError) {
|
|
320
|
+
console.log(`${error.limitType} limit exceeded`);
|
|
49
321
|
}
|
|
50
322
|
}
|
|
51
323
|
```
|
|
52
324
|
|
|
53
|
-
|
|
325
|
+
---
|
|
326
|
+
|
|
327
|
+
## Configuration Options
|
|
54
328
|
|
|
55
329
|
```typescript
|
|
56
330
|
const wallet = new AgentWallet({
|
|
331
|
+
// Required
|
|
57
332
|
sessionKey: 'sk_live_...',
|
|
58
|
-
|
|
59
|
-
|
|
333
|
+
|
|
334
|
+
// Optional
|
|
335
|
+
walletAddress: '0x...', // Override wallet address
|
|
336
|
+
maxPaymentUsd: 5.0, // Client-side safety limit
|
|
337
|
+
baseUrl: 'https://...', // Custom API URL
|
|
338
|
+
timeout: 60000, // Request timeout (ms)
|
|
339
|
+
logLevel: 'info', // 'debug' | 'info' | 'warn' | 'error' | 'none'
|
|
340
|
+
|
|
341
|
+
// Callbacks
|
|
342
|
+
onPayment: (payment) => {
|
|
343
|
+
console.log(`Paid $${payment.amountUsd} to ${payment.recipient}`);
|
|
344
|
+
},
|
|
60
345
|
});
|
|
346
|
+
```
|
|
61
347
|
|
|
62
|
-
|
|
348
|
+
---
|
|
349
|
+
|
|
350
|
+
## Diagnostics
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
const diagnostics = await wallet.runDiagnostics();
|
|
354
|
+
|
|
355
|
+
if (diagnostics.healthy) {
|
|
356
|
+
console.log('Wallet is ready to use');
|
|
357
|
+
} else {
|
|
358
|
+
console.log('Issues found:');
|
|
359
|
+
diagnostics.issues.forEach(issue => console.log(` - ${issue}`));
|
|
360
|
+
}
|
|
63
361
|
```
|
|
64
362
|
|
|
363
|
+
---
|
|
364
|
+
|
|
365
|
+
## Network Info
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
// Check if testnet
|
|
369
|
+
if (wallet.isTestnet()) {
|
|
370
|
+
console.log('Using Base Sepolia testnet');
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Get network details
|
|
374
|
+
const network = wallet.getNetwork();
|
|
375
|
+
console.log(`Chain: ${network.name} (${network.chainId})`);
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
---
|
|
379
|
+
|
|
380
|
+
## TypeScript Types
|
|
381
|
+
|
|
382
|
+
All types are exported for TypeScript users:
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
import type {
|
|
386
|
+
AgentWalletConfig,
|
|
387
|
+
PaymentEvent,
|
|
388
|
+
SpendingStats,
|
|
389
|
+
SessionAuthorization,
|
|
390
|
+
AgentMessage,
|
|
391
|
+
AgentRunConfig,
|
|
392
|
+
AgentRunOptions,
|
|
393
|
+
AgentRunResult,
|
|
394
|
+
AgentRunEvent,
|
|
395
|
+
} from '@mixrpay/agent-sdk';
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
---
|
|
399
|
+
|
|
65
400
|
## Documentation
|
|
66
401
|
|
|
67
|
-
[
|
|
402
|
+
- [Full Documentation](https://www.mixrpay.com/build)
|
|
403
|
+
- [API Reference](https://www.mixrpay.com/build/api)
|
|
404
|
+
- [Agent Runtime Guide](https://www.mixrpay.com/build/api#agent-runtime)
|
|
68
405
|
|
|
69
406
|
## License
|
|
70
407
|
|