-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (54 loc) · 2.13 KB
/
index.js
File metadata and controls
65 lines (54 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* x402 Client Integration Example (Node.js)
*
* This example shows how to call a paid AI agent using the x402 payment protocol.
* The client automatically handles HTTP 402 payment flows - you only pay when
* the request succeeds.
*/
import axios from "axios";
import { wrapAxiosWithPayment, x402Client, decodePaymentResponseHeader } from "@x402/axios";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";
import fs from "fs";
// Load your wallet (needs USDC on Base for payments)
const account = privateKeyToAccount(process.env.WALLET_KEY);
// Set up x402 client with EVM payment support
const client = new x402Client().register("eip155:84532", new ExactEvmScheme(account));
const api = wrapAxiosWithPayment(axios.create(), client);
// Agent endpoint - replace with any x402-enabled agent
const AGENT_URL = "https://agentokratia.com/api/v1/call/panche/snapshot-api";
async function callAgent() {
try {
console.log(`Calling agent: ${AGENT_URL}\n`);
// Make the request - payment is handled automatically
const response = await api.post(AGENT_URL, {
url: "https://example.com",
device: "iphone-15-pro",
delay: 2000
});
const data = response.data;
// Check for payment response header
const paymentResponse = response.headers["payment-response"];
if (paymentResponse) {
const decoded = decodePaymentResponseHeader(paymentResponse);
console.log("Payment details:", decoded);
}
console.log("Response received:");
console.log(" Success:", data.success);
console.log(" URL:", data.url);
console.log(" Device:", data.device);
// Save screenshot if the agent returned one
if (data.success && data.image) {
const base64Data = data.image.replace(/^data:image\/png;base64,/, "");
fs.writeFileSync("screenshot.png", base64Data, "base64");
console.log("\nScreenshot saved to screenshot.png");
}
} catch (error) {
if (error.response) {
console.error("Error response:", error.response.status, error.response.data);
} else {
console.error("Error:", error.message);
}
}
}
callAgent();