@payroute/x402-sdk 1.0.1 → 1.0.2
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 +78 -8
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +10 -9
- package/dist/index.mjs +10 -9
- package/package.json +18 -1
package/README.md
CHANGED
|
@@ -15,11 +15,9 @@ This SDK abstracts the complexity of **HTTP 402 Pay-Per-Hit** workflows, enablin
|
|
|
15
15
|
## Installation
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
npm install @payroute/x402-sdk
|
|
18
|
+
npm install @payroute/x402-sdk
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
_Note: `ethers` peer dependency (v6) is required._
|
|
22
|
-
|
|
23
21
|
## Quick Start
|
|
24
22
|
|
|
25
23
|
### 1. Initialize the Service
|
|
@@ -43,7 +41,7 @@ Use this method for direct peer-to-peer payments to a receiver.
|
|
|
43
41
|
|
|
44
42
|
```typescript
|
|
45
43
|
try {
|
|
46
|
-
const content = await service.getProxyEndpoint("
|
|
44
|
+
const content = await service.getProxyEndpoint("MantleDocs");
|
|
47
45
|
console.log("Accessed Content:", content);
|
|
48
46
|
} catch (error) {
|
|
49
47
|
console.error("Payment or Fetch Failed:", error);
|
|
@@ -56,7 +54,7 @@ Use this method when the gateway requires payment via an escrow smart contract.
|
|
|
56
54
|
|
|
57
55
|
```typescript
|
|
58
56
|
try {
|
|
59
|
-
const contentStart = await service.getProxyEndpointEscrow("
|
|
57
|
+
const contentStart = await service.getProxyEndpointEscrow("BitcoinOutlook");
|
|
60
58
|
console.log("Accessed Escrow Content:", contentStart);
|
|
61
59
|
} catch (error) {
|
|
62
60
|
console.error("Payment or Fetch Failed:", error);
|
|
@@ -71,7 +69,10 @@ Send messages to an AI agent that requires per-message payments.
|
|
|
71
69
|
|
|
72
70
|
```typescript
|
|
73
71
|
try {
|
|
74
|
-
const response = await service.generateAIResponse(
|
|
72
|
+
const response = await service.generateAIResponse(
|
|
73
|
+
"mantleAgent",
|
|
74
|
+
"How to build smart contract on Mantle Network?"
|
|
75
|
+
);
|
|
75
76
|
console.log("AI Response:", response);
|
|
76
77
|
} catch (error) {
|
|
77
78
|
console.error("Agent Interaction Failed:", error);
|
|
@@ -83,8 +84,8 @@ try {
|
|
|
83
84
|
```typescript
|
|
84
85
|
try {
|
|
85
86
|
const responseEscrow = await service.generateAIResponseEscrow(
|
|
86
|
-
"
|
|
87
|
-
"
|
|
87
|
+
"mantleAgent",
|
|
88
|
+
"How to build AVS on EigenLayer?"
|
|
88
89
|
);
|
|
89
90
|
console.log("AI Response Escrow:", responseEscrow);
|
|
90
91
|
} catch (error) {
|
|
@@ -105,6 +106,75 @@ const service = new PaymentService({
|
|
|
105
106
|
});
|
|
106
107
|
```
|
|
107
108
|
|
|
109
|
+
## Integration with AI Agents & LLMs
|
|
110
|
+
|
|
111
|
+
One of the most powerful use cases for `@payroute/x402-sdk` is enabling **Autonomous Economic Agents**. Because the SDK handles the entire payment lifecycle programmatically, LLMs can "pay" for their own resources without user intervention.
|
|
112
|
+
|
|
113
|
+
### Example: Autonomous Research Agent
|
|
114
|
+
|
|
115
|
+
Imagine an AI agent tasked with gathering premium market data. It can use this SDK to automatically pay for each data point it accesses using its own wallet.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { PaymentService } from "@payroute/x402-sdk";
|
|
119
|
+
import { openai } from "./my-llm-setup"; // Hypothetical LLM client
|
|
120
|
+
|
|
121
|
+
// 1. Give the Agent a Wallet
|
|
122
|
+
const agentWalletKey = process.env.AGENT_PRIVATE_KEY;
|
|
123
|
+
const payroute = new PaymentService({
|
|
124
|
+
privateKey: agentWalletKey,
|
|
125
|
+
network: "mantle",
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
async function autonomousResearchTask(topic: string) {
|
|
129
|
+
console.log(`Agent starting research on: ${topic}...`);
|
|
130
|
+
|
|
131
|
+
// 2. Agent decides it needs premium data (e.g., from 'HighValueData' endpoint)
|
|
132
|
+
// The SDK handles the 402 challenge, approves tokens, pays, and returns the data.
|
|
133
|
+
console.log("Accessing premium data source...");
|
|
134
|
+
|
|
135
|
+
// THIS SINGLE LINE handles the entire negotiation and payment
|
|
136
|
+
const premiumData = await payroute.getProxyEndpointEscrow("HighValueData");
|
|
137
|
+
|
|
138
|
+
// 3. Agent processes the purchased data
|
|
139
|
+
console.log("Data acquired. Analyzing...");
|
|
140
|
+
const analysis = await openai.chat.completions.create({
|
|
141
|
+
model: "gpt-4",
|
|
142
|
+
messages: [
|
|
143
|
+
{
|
|
144
|
+
role: "user",
|
|
145
|
+
content: `Analyze this data: ${JSON.stringify(premiumData)}`,
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return analysis.choices[0].message.content;
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
This pattern transforms **passive tools** into **economically active agents** capable of trading value for information or services on the open market.
|
|
155
|
+
|
|
156
|
+
### Example: Agent-to-Agent Consultation
|
|
157
|
+
|
|
158
|
+
Your agent can also pay to converse with other specialized AI agents (e.g., a "Legal Expert" or "Medical Advisor").
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
async function consultExpertAgent(problem: string) {
|
|
162
|
+
// Agent identifies it needs help from a specific expert agent slug
|
|
163
|
+
const expertAgentSlug = "legal-expert-v1";
|
|
164
|
+
|
|
165
|
+
console.log(`Consulting ${expertAgentSlug}...`);
|
|
166
|
+
|
|
167
|
+
// The SDK handles payment for the conversation turn
|
|
168
|
+
const expertAdvice = await payroute.generateAIResponseEscrow(
|
|
169
|
+
expertAgentSlug,
|
|
170
|
+
`I have a user asking about: ${problem}. What are the compliance risks?`
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
// Initial Agent integrates the paid advice into its final response
|
|
174
|
+
return expertAdvice.response;
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
108
178
|
## Architecture
|
|
109
179
|
|
|
110
180
|
The SDK implements the **Payroute x402 Protocol**:
|
package/dist/index.d.mts
CHANGED
|
@@ -13,6 +13,7 @@ interface PaymentServiceConfig {
|
|
|
13
13
|
privateKey: string;
|
|
14
14
|
network?: 'mantle' | 'mantleTestnet' | 'localhost';
|
|
15
15
|
rpcUrl?: string;
|
|
16
|
+
apiBaseUrl?: string;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* PaymentService class
|
|
@@ -21,6 +22,7 @@ interface PaymentServiceConfig {
|
|
|
21
22
|
declare class PaymentService {
|
|
22
23
|
private wallet;
|
|
23
24
|
private provider;
|
|
25
|
+
private apiBaseUrl;
|
|
24
26
|
constructor(config: PaymentServiceConfig);
|
|
25
27
|
/**
|
|
26
28
|
* Performs a payment on-chain and retries the original request with proof of payment.
|
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ interface PaymentServiceConfig {
|
|
|
13
13
|
privateKey: string;
|
|
14
14
|
network?: 'mantle' | 'mantleTestnet' | 'localhost';
|
|
15
15
|
rpcUrl?: string;
|
|
16
|
+
apiBaseUrl?: string;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* PaymentService class
|
|
@@ -21,6 +22,7 @@ interface PaymentServiceConfig {
|
|
|
21
22
|
declare class PaymentService {
|
|
22
23
|
private wallet;
|
|
23
24
|
private provider;
|
|
25
|
+
private apiBaseUrl;
|
|
24
26
|
constructor(config: PaymentServiceConfig);
|
|
25
27
|
/**
|
|
26
28
|
* Performs a payment on-chain and retries the original request with proof of payment.
|
package/dist/index.js
CHANGED
|
@@ -43,7 +43,6 @@ var NETWORKS = {
|
|
|
43
43
|
chainId: 31337
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
|
-
var BASE_ENDPOINT = "http://localhost:3000";
|
|
47
46
|
var ESCROW_ABI = [
|
|
48
47
|
"function createTx(bytes32 txId, address creator, uint256 amount)"
|
|
49
48
|
];
|
|
@@ -55,6 +54,7 @@ var ERC20_ABI = [
|
|
|
55
54
|
var PaymentService = class {
|
|
56
55
|
wallet;
|
|
57
56
|
provider;
|
|
57
|
+
apiBaseUrl;
|
|
58
58
|
constructor(config) {
|
|
59
59
|
const networkKey = config.network || "mantle";
|
|
60
60
|
const networkConfig = NETWORKS[networkKey];
|
|
@@ -68,6 +68,7 @@ var PaymentService = class {
|
|
|
68
68
|
} catch (error) {
|
|
69
69
|
throw new Error("Invalid private key provided.");
|
|
70
70
|
}
|
|
71
|
+
this.apiBaseUrl = config.apiBaseUrl || "https://x402-services.vercel.app";
|
|
71
72
|
}
|
|
72
73
|
/**
|
|
73
74
|
* Performs a payment on-chain and retries the original request with proof of payment.
|
|
@@ -124,7 +125,7 @@ var PaymentService = class {
|
|
|
124
125
|
*/
|
|
125
126
|
async generateAIResponse(agentSlug, message) {
|
|
126
127
|
try {
|
|
127
|
-
const initialResponse = await fetch(`${
|
|
128
|
+
const initialResponse = await fetch(`${this.apiBaseUrl}/agent/${agentSlug}/chat`, {
|
|
128
129
|
method: "POST",
|
|
129
130
|
headers: {
|
|
130
131
|
"Content-Type": "application/json"
|
|
@@ -164,7 +165,7 @@ var PaymentService = class {
|
|
|
164
165
|
"x-payment-tx": finalTxHash
|
|
165
166
|
};
|
|
166
167
|
console.log(`Retry with txHash: ${finalTxHash}`);
|
|
167
|
-
const retryResponse = await fetch(`${
|
|
168
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/agent/${agentSlug}/chat`, {
|
|
168
169
|
method: "POST",
|
|
169
170
|
headers,
|
|
170
171
|
body: JSON.stringify({
|
|
@@ -189,7 +190,7 @@ var PaymentService = class {
|
|
|
189
190
|
*/
|
|
190
191
|
async generateAIResponseEscrow(agentSlug, message) {
|
|
191
192
|
try {
|
|
192
|
-
const initialResponse = await fetch(`${
|
|
193
|
+
const initialResponse = await fetch(`${this.apiBaseUrl}/agent/escrow/${agentSlug}/chat`, {
|
|
193
194
|
method: "POST",
|
|
194
195
|
headers: {
|
|
195
196
|
"Content-Type": "application/json"
|
|
@@ -236,7 +237,7 @@ var PaymentService = class {
|
|
|
236
237
|
"x-payment-tx": finalTxHash
|
|
237
238
|
};
|
|
238
239
|
console.log(`Retry with txHash: ${finalTxHash}`);
|
|
239
|
-
const retryResponse = await fetch(`${
|
|
240
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/agent/escrow/${agentSlug}/chat`, {
|
|
240
241
|
method: "POST",
|
|
241
242
|
headers,
|
|
242
243
|
body: JSON.stringify({
|
|
@@ -263,7 +264,7 @@ var PaymentService = class {
|
|
|
263
264
|
try {
|
|
264
265
|
let initialResponse;
|
|
265
266
|
try {
|
|
266
|
-
initialResponse = await fetch(`${
|
|
267
|
+
initialResponse = await fetch(`${this.apiBaseUrl}/${gatewaySlug}`);
|
|
267
268
|
} catch (e) {
|
|
268
269
|
throw new Error(`Initial Endpoint Failed: ${e}`);
|
|
269
270
|
}
|
|
@@ -297,7 +298,7 @@ var PaymentService = class {
|
|
|
297
298
|
"x-payment-tx": finalTxHash
|
|
298
299
|
};
|
|
299
300
|
console.log("view header: ", headers);
|
|
300
|
-
const retryResponse = await fetch(`${
|
|
301
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/${gatewaySlug}`, {
|
|
301
302
|
method: "GET",
|
|
302
303
|
headers
|
|
303
304
|
});
|
|
@@ -321,7 +322,7 @@ var PaymentService = class {
|
|
|
321
322
|
try {
|
|
322
323
|
let initialResponse;
|
|
323
324
|
try {
|
|
324
|
-
initialResponse = await fetch(`${
|
|
325
|
+
initialResponse = await fetch(`${this.apiBaseUrl}/escrow/${gatewaySlug}`);
|
|
325
326
|
} catch (e) {
|
|
326
327
|
throw new Error(`Initial Endpoint Failed: ${e}`);
|
|
327
328
|
}
|
|
@@ -365,7 +366,7 @@ var PaymentService = class {
|
|
|
365
366
|
"x-payment-tx": finalTxHash
|
|
366
367
|
};
|
|
367
368
|
console.log("view header: ", headers);
|
|
368
|
-
const retryResponse = await fetch(`${
|
|
369
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/escrow/${gatewaySlug}`, {
|
|
369
370
|
method: "GET",
|
|
370
371
|
headers
|
|
371
372
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -17,7 +17,6 @@ var NETWORKS = {
|
|
|
17
17
|
chainId: 31337
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
var BASE_ENDPOINT = "http://localhost:3000";
|
|
21
20
|
var ESCROW_ABI = [
|
|
22
21
|
"function createTx(bytes32 txId, address creator, uint256 amount)"
|
|
23
22
|
];
|
|
@@ -29,6 +28,7 @@ var ERC20_ABI = [
|
|
|
29
28
|
var PaymentService = class {
|
|
30
29
|
wallet;
|
|
31
30
|
provider;
|
|
31
|
+
apiBaseUrl;
|
|
32
32
|
constructor(config) {
|
|
33
33
|
const networkKey = config.network || "mantle";
|
|
34
34
|
const networkConfig = NETWORKS[networkKey];
|
|
@@ -42,6 +42,7 @@ var PaymentService = class {
|
|
|
42
42
|
} catch (error) {
|
|
43
43
|
throw new Error("Invalid private key provided.");
|
|
44
44
|
}
|
|
45
|
+
this.apiBaseUrl = config.apiBaseUrl || "https://x402-services.vercel.app";
|
|
45
46
|
}
|
|
46
47
|
/**
|
|
47
48
|
* Performs a payment on-chain and retries the original request with proof of payment.
|
|
@@ -98,7 +99,7 @@ var PaymentService = class {
|
|
|
98
99
|
*/
|
|
99
100
|
async generateAIResponse(agentSlug, message) {
|
|
100
101
|
try {
|
|
101
|
-
const initialResponse = await fetch(`${
|
|
102
|
+
const initialResponse = await fetch(`${this.apiBaseUrl}/agent/${agentSlug}/chat`, {
|
|
102
103
|
method: "POST",
|
|
103
104
|
headers: {
|
|
104
105
|
"Content-Type": "application/json"
|
|
@@ -138,7 +139,7 @@ var PaymentService = class {
|
|
|
138
139
|
"x-payment-tx": finalTxHash
|
|
139
140
|
};
|
|
140
141
|
console.log(`Retry with txHash: ${finalTxHash}`);
|
|
141
|
-
const retryResponse = await fetch(`${
|
|
142
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/agent/${agentSlug}/chat`, {
|
|
142
143
|
method: "POST",
|
|
143
144
|
headers,
|
|
144
145
|
body: JSON.stringify({
|
|
@@ -163,7 +164,7 @@ var PaymentService = class {
|
|
|
163
164
|
*/
|
|
164
165
|
async generateAIResponseEscrow(agentSlug, message) {
|
|
165
166
|
try {
|
|
166
|
-
const initialResponse = await fetch(`${
|
|
167
|
+
const initialResponse = await fetch(`${this.apiBaseUrl}/agent/escrow/${agentSlug}/chat`, {
|
|
167
168
|
method: "POST",
|
|
168
169
|
headers: {
|
|
169
170
|
"Content-Type": "application/json"
|
|
@@ -210,7 +211,7 @@ var PaymentService = class {
|
|
|
210
211
|
"x-payment-tx": finalTxHash
|
|
211
212
|
};
|
|
212
213
|
console.log(`Retry with txHash: ${finalTxHash}`);
|
|
213
|
-
const retryResponse = await fetch(`${
|
|
214
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/agent/escrow/${agentSlug}/chat`, {
|
|
214
215
|
method: "POST",
|
|
215
216
|
headers,
|
|
216
217
|
body: JSON.stringify({
|
|
@@ -237,7 +238,7 @@ var PaymentService = class {
|
|
|
237
238
|
try {
|
|
238
239
|
let initialResponse;
|
|
239
240
|
try {
|
|
240
|
-
initialResponse = await fetch(`${
|
|
241
|
+
initialResponse = await fetch(`${this.apiBaseUrl}/${gatewaySlug}`);
|
|
241
242
|
} catch (e) {
|
|
242
243
|
throw new Error(`Initial Endpoint Failed: ${e}`);
|
|
243
244
|
}
|
|
@@ -271,7 +272,7 @@ var PaymentService = class {
|
|
|
271
272
|
"x-payment-tx": finalTxHash
|
|
272
273
|
};
|
|
273
274
|
console.log("view header: ", headers);
|
|
274
|
-
const retryResponse = await fetch(`${
|
|
275
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/${gatewaySlug}`, {
|
|
275
276
|
method: "GET",
|
|
276
277
|
headers
|
|
277
278
|
});
|
|
@@ -295,7 +296,7 @@ var PaymentService = class {
|
|
|
295
296
|
try {
|
|
296
297
|
let initialResponse;
|
|
297
298
|
try {
|
|
298
|
-
initialResponse = await fetch(`${
|
|
299
|
+
initialResponse = await fetch(`${this.apiBaseUrl}/escrow/${gatewaySlug}`);
|
|
299
300
|
} catch (e) {
|
|
300
301
|
throw new Error(`Initial Endpoint Failed: ${e}`);
|
|
301
302
|
}
|
|
@@ -339,7 +340,7 @@ var PaymentService = class {
|
|
|
339
340
|
"x-payment-tx": finalTxHash
|
|
340
341
|
};
|
|
341
342
|
console.log("view header: ", headers);
|
|
342
|
-
const retryResponse = await fetch(`${
|
|
343
|
+
const retryResponse = await fetch(`${this.apiBaseUrl}/escrow/${gatewaySlug}`, {
|
|
343
344
|
method: "GET",
|
|
344
345
|
headers
|
|
345
346
|
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payroute/x402-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "SDK for automation payment gateway with x402",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
8
25
|
"scripts": {
|
|
9
26
|
"build": "tsup"
|
|
10
27
|
},
|