Skip to content

Commit 5fc4913

Browse files
amends
1 parent 5c71b61 commit 5fc4913

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/content/docs/agents/x402.mdx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ sidebar:
1313

1414
## x402 Workers and Agents
1515

16-
You can create paywalled endpoints in your Workers and query any x402 server from your agent by wrapping the `fetch` with x402 and your wallet.
16+
You can create paywalled endpoints in your Workers and query any x402 server from your agent by wrapping the `fetch` with x402 and your account.
1717

1818
```ts
1919
import { Hono } from "hono";
2020
import { Agent, getAgentByName } from "agents";
2121
import { wrapFetchWithPayment } from "x402-fetch";
2222
import { paymentMiddleware } from "x402-hono";
2323

24-
// This allows us to create a wallet from just a private key
24+
// This allows us to derive an account from just the private key
2525
import { privateKeyToAccount } from "viem/accounts";
2626

2727
// The code below creates an Agent that can fetch the protected route and automatically pay.
28-
// The agent's wallet must not be empty! You can get test credits
28+
// The agent's account must not be empty! You can get test credits
2929
// for base-sepolia here: https://faucet.circle.com/
3030
export class PayAgent extends Agent<Env> {
3131
fetchWithPay!: ReturnType<typeof wrapFetchWithPayment>;
3232

3333
onStart() {
34-
// We instantiate a wallet from which the agent will pay
35-
const pk = process.env.CLIENT_TEST_PK as `0x${string}`;
36-
const agentAccount = privateKeyToAccount(pk);
37-
console.log("Agent will pay from this address:", agentAccount.address);
38-
this.fetchWithPay = wrapFetchWithPayment(fetch, agentAccount);
34+
// We derive the account from which the agent will pay
35+
const privateKey = process.env.CLIENT_TEST_PK as `0x${string}`;
36+
const account = privateKeyToAccount(privateKey);
37+
console.log("Agent will pay from this address:", account.address);
38+
this.fetchWithPay = wrapFetchWithPayment(fetch, account);
3939
}
4040

4141
async onRequest(req: Request) {
4242
const url = new URL(req.url);
4343
console.log("Trying to fetch paid API");
4444

45-
// We use the x402 fetch to access our paid endpoint
45+
// Use the x402 compatible fetch (fetchWithPay) to access the paid endpoint
4646
// Note: this could be any paid endpoint, on any server
4747
const paidUrl = new URL("/protected-route", url.origin).toString();
4848
return this.fetchWithPay(paidUrl, {});
@@ -55,7 +55,7 @@ const app = new Hono<{ Bindings: Env }>();
5555
// Only gate the `protected-route` endpoint, everything else we keep free.
5656
app.use(
5757
paymentMiddleware(
58-
process.env.SERVER_ADDRESS as `0x${string}`, // our server wallet address
58+
process.env.SERVER_ADDRESS as `0x${string}`, // our server's public address
5959
{
6060
"/protected-route": {
6161
price: "$0.10",
@@ -66,6 +66,7 @@ app.use(
6666
},
6767
},
6868
{ url: "https://x402.org/facilitator" }, // Payment facilitator URL
69+
// To learn more about facilitators https://x402.gitbook.io/x402/core-concepts/facilitator
6970
),
7071
);
7172

@@ -100,6 +101,7 @@ const X402_CONFIG: X402Config = {
100101
network: "base",
101102
recipient: env.MCP_ADDRESS,
102103
facilitator: { url: "https://x402.org/facilitator" }, // Payment facilitator URL
104+
// To learn more about facilitators https://x402.gitbook.io/x402/core-concepts/facilitator
103105
};
104106

105107
export class PaidMCP extends McpAgent<Env> {
@@ -155,13 +157,13 @@ export class MyAgent extends Agent {
155157
}
156158

157159
onPaymentRequired(paymentRequirements): Promise<boolean> {
158-
// Your HIL confirmation flow...
160+
// Your human-in-the-loop confirmation flow...
159161
}
160162

161-
async onToolCall() {
163+
async onToolCall(toolName: string, toolArgs: unknown) {
162164
// The first parameter becomes the confirmation callback.
163165
// We can set it to `null` if we want the agent to pay automatically.
164-
const res = await this.x402Client.callTool(this.onPaymentRequired, {
166+
return await this.x402Client.callTool(this.onPaymentRequired, {
165167
name: toolName,
166168
arguments: toolArgs,
167169
});

0 commit comments

Comments
 (0)