📦 Chart Splat (x402)
v1.0.0按次付费生成精美图表,使用 x402 微支付(Base 上的 USDC),无需 API key。当用户需要图表但没有 CHAR...
运行时依赖
安装命令
点击复制技能文档
Chart Splat (x402) 通过 x402 微支付按次付费生成精美图表——无需注册、无需 API key、无需订阅。 代理钱包在 Base 主网为每张图支付约 0.005 USDC。 这是标准 chart-splat 技能(用 API key 鉴权)的钱包版,按需选用。
前置条件
- EVM 钱包:Base 主网(eip155:8453)≥ 0.05 USDC
- 私钥导出为 X402_PRIVATE_KEY(0x 开头的 hex 字符串)
- Node.js 20+
买家不直接发起链上交易,Coinbase x402 facilitator 代付 gas 并结算。 协议细节见 references/x402-protocol.md。
支持图表 line 趋势 bar 类别对比 pie 整体占比 doughnut 带中心孔占比 radar 多变量对比 polarArea 径向类别对比 candlestick 金融 OHLC ohlc OHLC 柱状版
方法 1:打包脚本(推荐) node scripts/pay-and-fetch.js bar \ "Q1,Q2,Q3,Q4" \ "50,75,60,90" \ "Quarterly Revenue" \ chart.png
脚本完成完整 x402 流程:发请求 → 对 402 响应签 EIP-3009 → 带 PAYMENT-SIGNATURE 重试 → 保存 PNG → 打印 BaseScan 结算链接。
位置参数 pay-and-fetch.js [title] [output]
配置模式(多数据集 / OHLC) node scripts/pay-and-fetch.js --config chart-config.json -o chart.png config 文件即 Chart Splat 请求体,格式同 chartsplat.com/docs。 示例见 examples/sample-charts.json。
方法 2:编程调用(@x402/fetch) const { wrapFetchWithPayment, x402Client } = require('@x402/fetch'); const { registerExactEvmScheme } = require('@x402/evm/exact/client'); const { privateKeyToAccount } = require('viem/accounts'); const account = privateKeyToAccount(process.env.X402_PRIVATE_KEY); const client = new x402Client(); registerExactEvmScheme(client, { signer: account }); const fetchWithPayment = wrapFetchWithPayment(fetch, client); const res = await fetchWithPayment('https://api.chartsplat.com/chart', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ type: 'bar', data: { labels: ['A','B'], datasets: [{ data: [1,2] }] }, }), }); const { image } = await res.json();
须用 v2 @x402/* 包;旧 x402-fetch@1.x 不兼容。
环境变量 X402_PRIVATE_KEY (必填)付款钱包私钥 CHARTSPLAT_API_URL https://api.chartsplat.com 可改测试网地址
测试网(Base Sepolia) export CHARTSPLAT_API_URL=https://your-sepolia-server.example.com node scripts/pay-and-fetch.js bar "A,B,C" "1,2,3" -o chart.png 测试 USDC 从 Circle faucet 领取(选 Base Sepolia)。
输出
- PNG 保存至指定路径(默认 chart.png)
- 聊天平台返回文件路径:MEDIA: /path/to/chart.png
- 成功打印 BaseScan 结算链接供审计
常见错误 Set X402_PRIVATE_KEY in env → 导出私钥 Cannot find module '@x402/fetch' → npm install settlement failed → 钱包 USDC 不足,补至 ~0.01 402 循环 → 确认 @x402/fetch@^2.9.0 Signature rejected → 重试;检查系统时间
提示
- --config 支持完整请求体,示例见 examples/sample-charts.json
- pie/doughnut 可在 config 里传 colors 数组
- 默认 800×600,演示可加大 options.width/height
- 保存 BaseScan 链接便于记账