批量支付#
概述#
批量支付是 OKX Onchain OS Payment 面向高频、低值场景设计的支付模式。与单次支付不同,批量支付不要求每笔交易即时上链——买家签名后,卖家立即获得确认并交付资源,链上结算由 OKX 在后台异步批量完成。
为什么需要批量支付#
在单次支付中,每一笔交易都需要经历 "验证 → 上链结算 → 交付" 的完整流程。这对于高价值交易是合理的,但在 AI Agent 高频调用的场景下,逐笔上链会带来两个问题:
- 延迟:即使使用异步结算,Facilitator 提交交易仍需额外耗时
- 吞吐:高频场景下逐笔上链受链上确认速度限制,大量并发请求会形成结算瓶颈
批量支付针对性地解决了这两个问题:settle 调用立即返回确认(解决延迟问题),多笔交易批量压缩为一笔链上交易(解决吞吐问题)。
前提条件#
注意
批量支付要求买家安装并使用 OKX Agentic Wallet。普通 EVM 钱包无法使用此模式。
这是因为批量支付依赖两项关键基础设施:
- Session Key:由 Agentic Wallet 生成的临时签名密钥,允许 Agent 在授权范围内自主签名,无需每次调用主私钥
- TEE(可信执行环境):一种硬件级安全隔离技术,OKX 在 TEE 内执行批量结算,确保签名数据无法被篡改或窃取——即使服务器被入侵,也无法伪造结算交易
适用场景#
- AI Agent 连续调用:Agent 在一次任务中频繁调用多个付费 API(如搜索 → 分析 → 生成)
- 流式计费:按 token、按字符、按请求次数的高频微支付
- 批量数据查询:短时间内大量查询链上数据、市场行情
- 工具链编排:Agent 自动编排多个工具调用,每次调用金额极小
卖家接入流程#
Node.js
Typescript
import { OKXFacilitatorClient } from "@okxweb3/x402-core";
import { x402ResourceServer } from "@okxweb3/x402-core/server";
import { AggrDeferredEvmScheme } from "@okxweb3/x402-evm/deferred/server";
const facilitator = new OKXFacilitatorClient({
apiKey: "xxx",
secretKey: "xxx",
passphrase: "xxx",
});
const server = new x402ResourceServer(facilitator);
server.register("eip155:196", new AggrDeferredEvmScheme());
const routes = {
"GET /api/data": {
accepts: [{
scheme: "aggr_deferred",
network: "eip155:196",
payTo: "0xSellerWallet",
price: "$0.001",
}],
description: "Data endpoint with deferred settlement",
mimeType: "application/json",
},
};
app.use(paymentMiddleware(routes, server));
Go
Go
package main
import (
"fmt"
"net/http"
"os"
"time"
x402http "github.com/okx/payments/go/http"
ginmw "github.com/okx/payments/go/http/gin"
deferred "github.com/okx/payments/go/mechanisms/evm/deferred/server"
ginfw "github.com/gin-gonic/gin"
)
func main() {
syncClient, err := x402http.NewOKXFacilitatorClient(&x402http.OKXFacilitatorConfig{
Auth: x402http.OKXAuthConfig{
APIKey: "OKX_API_KEY",
SecretKey: "OKX_SECRET_KEY",
Passphrase: "OKX_PASSPHRASE",
},
BaseURL: "https://beta.okex.org",
})
if err != nil {
fmt.Printf("Failed to create client: %v\n", err)
os.Exit(1)
}
routes := x402http.RoutesConfig{
"GET /api/data": {
Accepts: x402http.PaymentOptions{
{Scheme: "aggr_deferred", Price: "$0.01", Network: "eip155:196", PayTo: "SellerWallet"},
},
Description: "Premium data endpoint",
MimeType: "application/json",
},
}
schemes := []ginmw.SchemeConfig{
{Network: "eip155:196", Server: deferred.NewAggrDeferredEvmScheme()},
}
r := ginfw.Default()
apiGroup := r.Group("/")
apiGroup.Use(ginmw.X402Payment(ginmw.Config{
Routes: routes,
Facilitator: syncClient,
Schemes: schemes,
Timeout: 30 * time.Second,
}))
apiGroup.GET("/api/data", func(c *ginfw.Context) {
c.JSON(http.StatusOK, ginfw.H{
"data": "premium content",
"price": "$0.01",
})
})
r.Run(":3000")
}
Rust
Rust
use axum::{Router, Json, routing::get};
use serde_json::json;
use std::collections::HashMap;
use x402_axum::{payment_middleware, RoutePaymentConfig, AcceptConfig};
use x402_core::http::OkxHttpFacilitatorClient;
use x402_core::server::X402ResourceServer;
use x402_evm::AggrDeferredEvmScheme;
#[tokio::main]
async fn main() {
let facilitator = OkxHttpFacilitatorClient::new(
"https://beta.okex.org",
&std::env::var("OKX_API_KEY").unwrap(),
&std::env::var("OKX_SECRET_KEY").unwrap(),
&std::env::var("OKX_PASSPHRASE").unwrap(),
);
let server = X402ResourceServer::new(facilitator)
.register("eip155:196", AggrDeferredEvmScheme::new());
let routes = HashMap::from([(
"GET /api/data".to_string(),
RoutePaymentConfig {
accepts: vec![AcceptConfig {
scheme: "aggr_deferred".into(),
price: "$0.01".into(),
network: "eip155:196".into(),
pay_to: "0xSellerWallet".into(),
}],
description: "Premium data endpoint".into(),
mime_type: "application/json".into(),
sync_settle: None,
},
)]);
let app = Router::new()
.route("/api/data", get(|| async { Json(json!({"data": "premium content"})) }))
.layer(payment_middleware(routes, server));
let listener = tokio::net::TcpListener::bind("0.0.0.0:4021").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
买家接入流程#
买家安装 OKX Agentic Wallet 后,Agent 会自动完成 402 协商和签名支付。具体请参考 我是买家。