agentkit-x402
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUsing AgentKit as an Agent
作为Agent使用AgentKit
This skill is for AI agents that interact with websites and APIs protected by x402 + AgentKit. It tells you how to prove you are backed by a real human so you can get free or discounted access instead of paying.
本技能适用于与受x402 + AgentKit保护的网站和API交互的AI Agent,教你如何证明自身有真实人类背书,从而获得免费或折扣访问权限,无需付款。
Critical Rule: Always Check for AgentKit Before Paying
关键规则:付款前务必检查是否存在AgentKit
When you receive a response, do not immediately proceed to payment. First inspect the response for an extension. If one is present, attempt the AgentKit authentication flow below. Only fall back to payment if:
402 Payment Requiredagentkit- There is no extension in the 402 response
agentkit - Your AgentKit authentication attempt fails and the server requires payment
- You have exhausted your free-trial uses
当你收到响应时,不要直接进入付款流程。首先检查响应中是否包含扩展。如果有,先尝试下方的AgentKit认证流程。仅在以下情况才 fallback 到付款流程:
402 Payment Requiredagentkit- 402响应中不存在扩展
agentkit - 你的AgentKit认证尝试失败,服务器要求付款
- 你已用完免费试用次数
Wallet Types
钱包类型
Your wallet determines how you sign the challenge. There are two types:
你的钱包类型决定了签名挑战的方式,共分为两类:
EOA (Externally Owned Account)
EOA(外部拥有账户)
A standard wallet where you directly hold the private key (e.g. a raw private key, a mnemonic-derived wallet).
- Signature type:
eip191 - How to sign: Use (EIP-191) to sign the SIWE message
personal_sign - Example:
wallet.signMessage(siweMessage)
你直接持有私钥的标准钱包(例如原始私钥、助记词派生的钱包)。
- 签名类型:
eip191 - 签名方式: 使用(EIP-191)签名SIWE消息
personal_sign - 示例:
wallet.signMessage(siweMessage)
Smart Contract Wallet (SCW)
智能合约钱包(SCW)
A wallet where the "account" is a smart contract and signing is done by an underlying owner key. The server verifies your signature on-chain via the contract's method (ERC-1271).
isValidSignatureExamples: Coinbase Smart Wallet, Safe, any ERC-4337 account.
- Signature type:
eip1271 - How to sign: Sign the SIWE message using the wallet's SDK or internal signer. The signature format depends on the wallet implementation — use whatever the wallet SDK provides.
- Example (Coinbase CDP):
account.signMessage({ message: siweMessage })
If you are unsure which type your wallet is: if you created it from a private key or mnemonic, it is an EOA. If you created it through a wallet SDK (Coinbase CDP, Safe SDK, etc.), it is likely an SCW.
这类钱包的“账户”是一个智能合约,签名由底层的所有者密钥完成。服务器会通过合约的方法(ERC-1271)链上验证你的签名。
isValidSignature示例:Coinbase Smart Wallet、Safe、任意ERC-4337账户。
- 签名类型:
eip1271 - 签名方式: 使用钱包的SDK或内部签名器对SIWE消息进行签名。签名格式取决于钱包的实现——直接使用钱包SDK返回的结果即可。
- 示例(Coinbase CDP):
account.signMessage({ message: siweMessage })
如果你不确定自己的钱包类型:如果你是通过私钥或助记词创建的钱包,就是EOA;如果你是通过钱包SDK(Coinbase CDP、Safe SDK等)创建的,大概率是SCW。
The AgentKit Flow
AgentKit使用流程
Step 1: Parse the 402 Response
步骤1:解析402响应
When you receive a , look for the extension in the response body. The 402 response contains data with extensions. The extension looks like:
402 Payment Requiredagentkitx402agentkitjson
{
"agentkit": {
"info": {
"domain": "api.example.com",
"uri": "https://api.example.com/data",
"version": "1",
"nonce": "abc123",
"issuedAt": "2025-01-01T00:00:00.000Z",
"statement": "Verify your agent is backed by a real human"
},
"supportedChains": [
{ "chainId": "eip155:8453", "type": "eip191" },
{ "chainId": "eip155:8453", "type": "eip1271" }
],
"schema": { ... }
}
}Key fields to extract:
- — the challenge data you must sign
info - — which chains and signature types the server accepts
supportedChains - (if present) — tells you the access policy:
mode,free, orfree-trialdiscount
当你收到响应时,在响应体中查找扩展。402响应包含带扩展的数据,扩展的格式如下:
402 Payment Requiredagentkitx402agentkitjson
{
"agentkit": {
"info": {
"domain": "api.example.com",
"uri": "https://api.example.com/data",
"version": "1",
"nonce": "abc123",
"issuedAt": "2025-01-01T00:00:00.000Z",
"statement": "Verify your agent is backed by a real human"
},
"supportedChains": [
{ "chainId": "eip155:8453", "type": "eip191" },
{ "chainId": "eip155:8453", "type": "eip1271" }
],
"schema": { ... }
}
}需要提取的关键字段:
- —— 你需要签名的挑战数据
info - —— 服务器接受的链和签名类型
supportedChains - (如果存在) —— 告知访问策略:
mode、free或free-trialdiscount
Step 2: Pick a Chain and Signature Type
步骤2:选择链和签名类型
Match your wallet to one of the entries:
supportedChains| Your wallet | Match | Use |
|---|---|---|
| EVM EOA | | |
| EVM Smart Contract | | |
Pick the entry that matches both your chain and wallet type.
将你的钱包与中的条目匹配:
supportedChains| 你的钱包类型 | 匹配 | 使用 |
|---|---|---|
| EVM EOA | | |
| EVM 智能合约钱包 | | |
选择同时匹配你的链和钱包类型的条目即可。
Step 3: Construct and Sign the SIWE/SIWS Message
步骤3:构造并签名SIWE/SIWS消息
Construct a SIWE (EIP-4361) message string from the challenge fields. The format is a plain text string with this exact structure:
info{domain} wants you to sign in with your Ethereum account:
{address}
{statement}
URI: {uri}
Version: {version}
Chain ID: {numericChainId}
Nonce: {nonce}
Issued At: {issuedAt}Where is extracted from the CAIP-2 chain ID (e.g. becomes ), and must be EIP-55 checksummed.
{numericChainId}eip155:84538453{address}If the challenge includes optional fields, append them in this order (only include lines for fields that are present):
Expiration Time: {expirationTime}
Not Before: {notBefore}
Request ID: {requestId}
Resources:
- {resources[0]}
- {resources[1]}Full example:
api.example.com wants you to sign in with your Ethereum account:
0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
Verify your agent is backed by a real human
URI: https://api.example.com/data
Version: 1
Chain ID: 8453
Nonce: abc123def
Issued At: 2025-01-01T00:00:00.000Z
Expiration Time: 2025-01-01T00:05:00.000Z
Request ID: req-456
Resources:
- https://api.example.com/tosImportant formatting rules:
- There must be a blank line before and after the line
{statement} - If there is no statement, there must be a single blank line between the address and
URI: - Each line ends with (LF, not CRLF)
\n - No trailing newline after the last line
Then sign the message string:
typescript
// EOA — use personal_sign (EIP-191)
const signature = await wallet.signMessage(messageToSign)
// SCW — use the wallet SDK's signMessage
const signature = await smartWallet.signMessage({ message: messageToSign })根据挑战的字段构造SIWE(EIP-4361)消息字符串,格式为纯文本,需严格遵循以下结构:
info{domain} wants you to sign in with your Ethereum account:
{address}
{statement}
URI: {uri}
Version: {version}
Chain ID: {numericChainId}
Nonce: {nonce}
Issued At: {issuedAt}其中是从CAIP-2链ID中提取的数值(例如对应),必须符合EIP-55校验和格式。
{numericChainId}eip155:84538453{address}如果挑战包含可选字段,请按以下顺序追加(仅包含存在的字段对应的行):
Expiration Time: {expirationTime}
Not Before: {notBefore}
Request ID: {requestId}
Resources:
- {resources[0]}
- {resources[1]}完整示例:
api.example.com wants you to sign in with your Ethereum account:
0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
Verify your agent is backed by a real human
URI: https://api.example.com/data
Version: 1
Chain ID: 8453
Nonce: abc123def
Issued At: 2025-01-01T00:00:00.000Z
Expiration Time: 2025-01-01T00:05:00.000Z
Request ID: req-456
Resources:
- https://api.example.com/tos重要格式规则:
- 行的前后必须各有一个空行
{statement} - 如果没有statement,地址和之间必须有一个空行
URI: - 每一行以结尾(LF,而非CRLF)
\n - 最后一行后不要加尾随换行符
然后对消息字符串进行签名:
typescript
// EOA — 使用personal_sign (EIP-191)
const signature = await wallet.signMessage(messageToSign)
// SCW — 使用钱包SDK的signMessage方法
const signature = await smartWallet.signMessage({ message: messageToSign })Step 4: Send the Request
步骤4:发送请求
Base64-encode a JSON object containing the challenge fields plus your , , and , and send it as the HTTP header:
infoaddresstypesignatureagentkittypescript
const header = btoa(JSON.stringify({
...info, // all fields from the challenge
address: walletAddress,
chainId: "eip155:8453", // from the supportedChains entry you picked
type: "eip191", // "eip191" for EOA, "eip1271" for SCW
signature: signature, // hex string from signing
}))
const response = await fetch("https://api.example.com/data", {
headers: {
"agentkit": header,
},
})If the server grants access (based on the mode), you will receive the resource directly — no payment needed.
If the server responds with another 402 or an error, fall back to the normal x402 payment flow.
将包含挑战字段、你的、和的JSON对象进行Base64编码,作为 HTTP头发送:
infoaddresstypesignatureagentkittypescript
const header = btoa(JSON.stringify({
...info, // 挑战的所有字段
address: walletAddress,
chainId: "eip155:8453", // 你选择的supportedChains条目中的chainId
type: "eip191", // EOA填"eip191",SCW填"eip1271"
signature: signature, // 签名得到的十六进制字符串
}))
const response = await fetch("https://api.example.com/data", {
headers: {
"agentkit": header,
},
})如果服务器批准访问(基于mode),你将直接获得请求的资源——无需付款。
如果服务器返回另一个402或错误,请 fallback 到常规的x402付款流程。
Access Modes
访问模式
The field in the AgentKit extension tells you what to expect:
mode| Mode | What it means |
|---|---|
| Human-backed agents always get free access |
| First N requests are free, then you must pay (N is per human, not per agent) |
| You get N% off the price (pay the discounted amount via x402) |
For mode: send both the header and the x402 payment header, but pay the discounted price. The server will reconcile the underpayment using your human-backed status.
discountagentkitAgentKit扩展中的字段会告知你可享受的访问策略:
mode| 模式 | 含义说明 |
|---|---|
| 有人类背书的Agent始终可获得免费访问权限 |
| 前N次请求免费,之后必须付款(N按人类用户计数,而非按Agent计数) |
| 你可享受N%的价格优惠(通过x402支付折扣后的金额) |
对于模式:同时发送头和x402付款头,但仅支付折扣后的金额即可。服务器会根据你的人类背书身份核销差额。
discountagentkitCommon Errors and How to Handle Them
常见错误及处理方案
"Agent is not registered in the AgentBook"
"Agent is not registered in the AgentBook"
Your wallet address is not registered. You need to register first using the AgentKit CLI:
bash
npx @worldcoin/agentkit-cli register <your-wallet-address>This opens a World ID verification flow that ties your wallet to an anonymous human identifier on-chain. Registration only needs to happen once per wallet.
你的钱包地址未注册,需要先通过AgentKit CLI完成注册:
bash
npx @worldcoin/agentkit-cli register <your-wallet-address>该命令会打开World ID验证流程,将你的钱包与链上的匿名人类标识符绑定。每个钱包仅需注册一次。
"Signature verification failed"
"Signature verification failed"
- Wrong signature type: Make sure matches your wallet. Use
typefor EOA,eip191for SCW.eip1271 - Wrong message format: The SIWE message must follow the exact format described in Step 3. Pay close attention to blank lines around the statement and field ordering.
- Wrong chain ID: The in the payload must be CAIP-2 format (
chainId), but the SIWE messageeip155:8453field must be the numeric chain ID (chainId).8453
- 签名类型错误: 确保与你的钱包类型匹配,EOA用
type,SCW用eip191。eip1271 - 消息格式错误: SIWE消息必须严格遵循步骤3中描述的格式,特别注意statement前后的空行和字段顺序。
- 链ID错误: payload中的必须是CAIP-2格式(
chainId),但SIWE消息中的eip155:8453字段必须是数值型链ID(chainId)。8453
"Invalid agentkit header: not valid base64"
"Invalid agentkit header: not valid base64"
Your header is not properly base64-encoded. Ensure you are encoding the full JSON string: .
btoa(JSON.stringify(payload))你的头没有正确进行Base64编码,确保你编码的是完整的JSON字符串:。
btoa(JSON.stringify(payload))"Message validation failed" / "issuedAt is too old"
"Message validation failed" / "issuedAt is too old"
The challenge has expired. Re-fetch the 402 response to get a fresh challenge (new nonce, new ) and sign again. Challenges expire after 5 minutes by default.
issuedAt挑战已过期,重新请求402响应获取新的挑战(新的nonce、新的)后重新签名即可。默认情况下挑战的有效期为5分钟。
issuedAt"Unsupported chain namespace"
"Unsupported chain namespace"
You are using a chain that the server does not support. Check in the 402 response and pick a chain/type pair that matches your wallet.
supportedChains你使用的链不在服务器支持范围内,检查402响应中的,选择与你的钱包匹配的链/类型对即可。
supportedChains