Blockchain IQ 示範如何在 Base 上用 x402 按次支付加密貨幣數據
Paying for crypto data one request at a time with x402 on Base
Blockchain IQ 展示如何透過 Base 上的 x402,讓軟件智能體按次支付 USDC 以取得加密貨幣數據。未付款請求會收到列明價格、網絡和資產的 HTTP 402 回應,客戶端簽署付款後重試;流程毋須帳戶或 API key,付款方無須支付 gas。
I run Blockchain IQ, a crypto research company. We sell some of our data to software agents one request at a time using the x402 payment standard, and this post shows how a call works end to end. At the end is what our integration bounty produced.
What x402 does
x402 reuses the HTTP 402 "Payment Required" status. A request without payment gets a 402 response whose PAYMENT-REQUIRED header states the price, the network and the asset. The client signs a USDC payment for that amount, retries the request with the signed payment, and gets the data. There is no account and no API key. Settlement is gasless for the payer.
Our API settles in USDC on Base. You can see the price of every resource without a wallet: the catalogue at https://www.blockchainiq.co/api/x402/v1 is free, and there is a free sample response at https://www.blockchainiq.co/api/x402/v1/sample.
Try an unpaid request
curl -i https://www.blockchainiq.co/api/x402/v1/brief
You get HTTP/1.1 402 Payment Required, a PAYMENT-REQUIRED header and a short JSON error that includes the price ($0.01). A 404 or an error response is never charged.
Make a paid call in Node
This is the example from our /agents page. It needs a wallet holding a little USDC on Base. A wallet that has never paid us gets its first call to brief or event-risk free, so the first test costs nothing; the second call is paid.
// npm install @x402/fetch @x402/evm viem
import { privateKeyToAccount } from "viem/accounts";
import { wrapFetchWithPayment, x402Client, decodePaymentResponseHeader } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
const account = privateKeyToAccount(process.env.AGENT_PRIVATE_KEY);
const client = x402Client.fromConfig({
schemes: [{ network: "eip155:*", client: new ExactEvmScheme(account) }],
spendControls: { maxAmountPerPayment: 0.6 }, // dollars; refuse anything dearer
});
const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://www.blockchainiq.co/api/x402/v1/event-risk?days=7");
const { data } = await res.json();
console.log(data);
// Proof of settlement: payer, network and transaction hash.
console.log(decodePaymentResponseHeader(res.headers.get("payment-response")));
Two routes are the cheapest way to test:
-
briefcosts $0.01 and returns prices and recent moves for Bitcoin, Ethereum, Solana and XRP, scheduled events in the next 48 hours, and short risk flags. It takes no parameters. -
event-riskcosts $0.05 and returns US data releases, central bank decisions, Treasury auctions and dated crypto and regulatory milestones, seven days ahead by default, each with its official source.
Keep the spend control in the snippet. It makes the client refuse a payment above the limit you set.
Use it from an MCP client
We also publish an MCP server, blockchainiq-x402-mcp (0.1.2, MIT licence) on npm:
npx -y blockchainiq-x402-mcp
The catalogue tools work without a wallet; the paid tools are paid by your own wallet.
A free index of x402 sellers
Separately, we publish a free daily Conformance Index that sends an unpaid request to every paid x402 endpoint listed in public directories and grades the response (valid HTTP 402, x402 v2, valid payment terms, a schema): https://www.blockchainiq.co/x402/index
What the bounty produced
On 23 October we offered $525 for integrations that let agents buy this data through x402. Between 24 and 26 October builders sent 22 claims. We matched each settlement hash and paying wallet to our purchase log, read each repository and its dependencies, and ran it in a clean temporary folder with install scripts off and a throwaway wallet. Twenty claims passed. Four took the slots, one each for LangChain, ElizaOS, Cloudflare Agents and Coinbase AgentKit, and all four were paid on 27 October in USDC on Base. Every repository is listed, pinned to the commit we ran: https://www.blockchainiq.co/blog/x402-integrations-from-the-build-bounty
Our data is market data and analysis, not investment advice.
來源:DEV Community · dev.to