x402-on-skale
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesex402 on SKALE
SKALE 上的 x402
x402 is HTTP 402 protocol for AI agent payments. Zero gas fees make SKALE ideal for micropayments.
x402是面向AI Agent支付的HTTP 402协议,零Gas费的特性让SKALE非常适合小额支付场景。
What is x402?
什么是x402?
- Extends HTTP with native payment capabilities using
402 Payment Required - Paywalled Resources: Protect APIs behind payments
- Autonomous Payments: Agents automatically pay for resources
- ERC-3009 Compatibility: Uses TransferWithAuthorization for gasless payments
- 基于扩展HTTP的原生支付能力
402 Payment Required - 付费墙资源:为API设置支付保护
- 自动支付:Agent可自动为所需资源付费
- 兼容ERC-3009:使用TransferWithAuthorization实现无Gas支付
Facilitators
服务商
Use a facilitator to handle payments - don't run your own:
| Service | URL | Notes |
|---|---|---|
| PayAI | | Recommended |
| Corbits | | Developer-focused |
| RelAI | | Marketplace |
| Kobaru | | Enterprise |
使用现有服务商处理支付,无需自行搭建:
| 服务 | URL | 备注 |
|---|---|---|
| PayAI | | 推荐使用 |
| Corbits | | 面向开发者 |
| RelAI | | 交易市场 |
| Kobaru | | 企业级 |
Payment Tokens on SKALE
SKALE上的支付代币
| Token | Address | Decimals |
|---|---|---|
| Bridged USDC | | 6 |
| Axios USD | | 6 |
| 代币 | 地址 | 精度 |
|---|---|---|
| Bridged USDC | | 6 |
| Axios USD | | 6 |
Network IDs
网络ID
Format:
eip155:CHAIN_ID| Chain | Chain ID | x402 Network ID |
|---|---|---|
| SKALE Base Sepolia | 324705682 | |
| SKALE Base | 1187947933 | |
格式:
eip155:CHAIN_ID| 链 | 链ID | x402网络ID |
|---|---|---|
| SKALE Base Sepolia | 324705682 | |
| SKALE Base | 1187947933 | |
Install
安装
bash
npm install @x402/core @x402/evm @x402/honobash
npm install @x402/core @x402/evm @x402/honoServer Setup
服务端设置
typescript
import { Hono } from "hono";
import { paymentMiddleware, x402ResourceServer } from "@x402/hono";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";
const client = new HTTPFacilitatorClient({ url: "https://facilitator.payai.network" });
const server = new x402ResourceServer(client);
server.register("eip155:*", new ExactEvmScheme());
const app = new Hono();
app.use(paymentMiddleware({
"GET /api/premium": {
accepts: [{
scheme: "exact",
network: "eip155:324705682",
payTo: "0xYourReceivingAddress",
price: {
amount: "10000", // 0.01 USDC (6 decimals)
asset: "0x2e08028E3C4c2356572E096d8EF835cD5C6030bD"
}
}]
}
}, server));
app.get("/api/premium", (c) => c.json({ data: "premium!" }));typescript
import { Hono } from "hono";
import { paymentMiddleware, x402ResourceServer } from "@x402/hono";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";
const client = new HTTPFacilitatorClient({ url: "https://facilitator.payai.network" });
const server = new x402ResourceServer(client);
server.register("eip155:*", new ExactEvmScheme());
const app = new Hono();
app.use(paymentMiddleware({
"GET /api/premium": {
accepts: [{
scheme: "exact",
network: "eip155:324705682",
payTo: "0xYourReceivingAddress",
price: {
amount: "10000", // 0.01 USDC (6 decimals)
asset: "0x2e08028E3C4c2356572E096d8EF835cD5C6030bD"
}
}]
}
}, server));
app.get("/api/premium", (c) => c.json({ data: "premium!" }));Multiple Endpoints
多端点配置
typescript
app.use(paymentMiddleware({
"GET /api/basic": {
accepts: [{
scheme: "exact",
network: "eip155:324705682",
payTo: "0xYourAddress",
price: { amount: "1000", asset: "0x..." } // 0.001 USDC
}]
},
"GET /api/premium": {
accepts: [{
scheme: "exact",
network: "eip155:324705682",
payTo: "0xYourAddress",
price: { amount: "10000", asset: "0x..." } // 0.01 USDC
}]
}
}, server));typescript
app.use(paymentMiddleware({
"GET /api/basic": {
accepts: [{
scheme: "exact",
network: "eip155:324705682",
payTo: "0xYourAddress",
price: { amount: "1000", asset: "0x..." } // 0.001 USDC
}]
},
"GET /api/premium": {
accepts: [{
scheme: "exact",
network: "eip155:324705682",
payTo: "0xYourAddress",
price: { amount: "10000", asset: "0x..." } // 0.01 USDC
}]
}
}, server));Agent Client
Agent 客户端
typescript
import { x402Client, x402HTTPClient } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const httpClient = new x402HTTPClient(
new x402Client().register("eip155:*", new ExactEvmScheme(account))
);
async function accessPaidResource(url: string) {
const response = await fetch(url);
if (response.status === 402) {
const responseBody = await response.json();
const paymentRequired = httpClient.getPaymentRequiredResponse(
(name) => response.headers.get(name),
responseBody
);
const paymentPayload = await httpClient.createPaymentPayload(paymentRequired);
const paymentHeaders = httpClient.encodePaymentSignatureHeader(paymentPayload);
const paidResponse = await fetch(url, { headers: paymentHeaders });
return paidResponse.json();
}
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
return response.json();
}typescript
import { x402Client, x402HTTPClient } from "@x402/core/client";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const httpClient = new x402HTTPClient(
new x402Client().register("eip155:*", new ExactEvmScheme(account))
);
async function accessPaidResource(url: string) {
const response = await fetch(url);
if (response.status === 402) {
const responseBody = await response.json();
const paymentRequired = httpClient.getPaymentRequiredResponse(
(name) => response.headers.get(name),
responseBody
);
const paymentPayload = await httpClient.createPaymentPayload(paymentRequired);
const paymentHeaders = httpClient.encodePaymentSignatureHeader(paymentPayload);
const paidResponse = await fetch(url, { headers: paymentHeaders });
return paidResponse.json();
}
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
return response.json();
}Price Calculation
价格计算
Amount is in token decimals (USDC = 6):
| Amount | USDC |
|---|---|
| 100 | $0.0001 |
| 1000 | $0.001 |
| 10000 | $0.01 |
| 1000000 | $1.00 |
金额按代币精度计算(USDC精度为6):
| 金额 | USDC |
|---|---|
| 100 | $0.0001 |
| 1000 | $0.001 |
| 10000 | $0.01 |
| 1000000 | $1.00 |
Use Cases
使用场景
- Autonomous Service Access: Agents pay for API calls, data feeds, AI services
- Monetize Agent Services: Protect AI endpoints with payments
- Multi-Agent Economies: Agent-to-agent payments for services
Reference: x402 Docs
- 自主服务访问:Agent为API调用、数据流、AI服务付费
- Agent服务商业化:为AI接口添加支付保护
- 多Agent经济:Agent之间为服务互相支付
参考:x402 文档