跳到正文

价格 WebSocket API

Oracle server 提供只读 WebSocket 端点,用于接收实时 source tick、聚合价格更新和 finalized source tick bucket。

生产 WebSocket URL:

text
wss://api.coinpost.ai/oracle/ws/prices

上游端点路径:

text
GET /ws/prices

鉴权方式

WebSocket upgrade 鉴权遵循 Oracle server 规则。当 auth.enabledcp_auth.enabled 为 true 时,通过 Bearer token 发送 API key:

text
Authorization: Bearer <api_key>

浏览器客户端如果不能设置自定义 WebSocket 请求头,可以在 WebSocket URL 上传 token:

text
wss://api.coinpost.ai/oracle/ws/prices?token=<api_key>
wss://api.coinpost.ai/oracle/ws/prices?access_token=<api_key>

Query token 鉴权只在价格 WebSocket 端点上接受。

REST 接口的鉴权方式以 REST operation 页面为准。

Client Messages

Client message 使用 JSON。未知字段会被忽略。

Subscribe

json
{
  "type": "subscribe",
  "id": "btc-live",
  "symbols": ["BTC"],
  "channels": ["aggregate", "sources", "ticks"],
  "sources": ["bitbank", "binance_japan", "coinbase"],
  "include_snapshot": true
}

字段:

  • id:可选 subscription ID。Server 会在响应里用 subscription_id 原样返回。
  • symbols:必填,canonical symbol 列表。
  • channels:可选列表。默认值是 ["aggregate", "sources"]
  • sources:可选 source filter。省略或传空列表表示所有 source。
  • include_snapshot:可选 boolean。为 true 时,server 会立即发送最新状态。

Channels:

  • aggregate:接收 snapshot aggregate data 和 aggregate_update message。
  • sources:接收 snapshot source data 和 source_update message。
  • ticks:接收 finalized tick message。

Unsubscribe

json
{
  "type": "unsubscribe",
  "id": "btc-live"
}

如果省略 id,会移除 default subscription。

Ping

json
{
  "type": "ping",
  "ts": "2026-06-10T06:00:00Z"
}

响应:

json
{
  "type": "pong",
  "ts": "2026-06-10T06:00:00Z"
}

Server Messages

Snapshot

include_snapshot 为 true 时,在 subscribe 后发送。sources 是 source name 到最新 source tick 的 map。

json
{
  "type": "snapshot",
  "subscription_id": "btc-live",
  "symbol": "BTC",
  "asset_id": "d388491f-2ee0-337f-bad1-fd8f4444e076",
  "tick_interval": "1m0s",
  "aggregate": {
    "source": "aggregated",
    "selected_source": "bitbank",
    "selected_pair": "btc_jpy",
    "price_usd": "61363.4557105392479143",
    "price_jpy": "9839303",
    "tick_start": "2026-06-10T15:01:00+09:00",
    "tick_interval": "1m0s",
    "method": "source_vote",
    "vote_band_pct": "0.003",
    "vote_sources": ["bitbank", "bitflyer", "coinbase"],
    "rejected_sources": ["binance_japan"],
    "updated_at": "2026-06-10T15:01:24.347954002+09:00"
  },
  "sources": {
    "bitbank": {
      "symbol": "BTC",
      "asset_id": "d388491f-2ee0-337f-bad1-fd8f4444e076",
      "source": "bitbank",
      "pair": "btc_jpy",
      "tick_start": "2026-06-10T15:01:00+09:00",
      "tick_interval": "1m0s",
      "native_price": "9839303",
      "native_currency": "JPY",
      "price_jpy": "9839303",
      "price_usd": "61363.4557105392479143",
      "open": "9839303",
      "high": "9839303",
      "low": "9839303",
      "close": "9839303",
      "bid": "9837946",
      "ask": "9837947",
      "source_timestamp": "2026-06-10T15:01:10.046+09:00",
      "received_at": "2026-06-10T15:01:11.420015171+09:00",
      "transport": "websocket",
      "sample_count": 12
    }
  }
}

Source Update

channels 包含 sources 时发送。这是公开接口中延迟最低的交易所 source tick 事件。

json
{
  "type": "source_update",
  "subscription_id": "btc-live",
  "symbol": "BTC",
  "asset_id": "d388491f-2ee0-337f-bad1-fd8f4444e076",
  "source": "bitbank",
  "pair": "btc_jpy",
  "tick_start": "2026-06-10T15:01:00+09:00",
  "tick_interval": "1m0s",
  "native_price": "9839303",
  "native_currency": "JPY",
  "price_jpy": "9839303",
  "price_usd": "61363.4557105392479143",
  "open": "9839303",
  "high": "9839303",
  "low": "9839303",
  "close": "9839303",
  "bid": "9837946",
  "ask": "9837947",
  "source_timestamp": "2026-06-10T15:01:10.046+09:00",
  "received_at": "2026-06-10T15:01:11.420015171+09:00",
  "transport": "websocket",
  "sample_count": 12,
  "updated_at": "2026-06-10T15:01:11.421000000+09:00"
}

字段说明:

  • native_price:source quote currency 下的最新观测价格。
  • price_usd / price_jpy:可转换时的标准化价格。
  • openhighlowclose:当前 tick_start 的 source tick bucket 值。
  • bid / ask:source ticker 提供时的 best bid 和 best ask。
  • sample_count:折叠进当前 bucket 的 source message 数量。
  • source_timestamp:交易所或上游 source 报告的时间。
  • received_at:worker 本地收到数据的时间。

Bid 和 ask 可用性:

SourceBid/ask
bitbankYes
binance_japanYes
bitflyerYes
coinbaseYes
gmo_coinYes
coincheckNo, current collector uses trades
coingeckoNo, current collector uses simple price

这些是 ticker 级别的 best bid 和 best ask,不是完整 order book。

Aggregate Update

channels 包含 aggregate 时发送。Worker 会在每次 source update 后重新计算当前 aggregate。

json
{
  "type": "aggregate_update",
  "subscription_id": "btc-live",
  "symbol": "BTC",
  "asset_id": "d388491f-2ee0-337f-bad1-fd8f4444e076",
  "source": "bitbank",
  "tick_start": "2026-06-10T15:01:00+09:00",
  "tick_interval": "1m0s",
  "aggregate": {
    "source": "aggregated",
    "selected_source": "bitbank",
    "selected_pair": "btc_jpy",
    "price_usd": "61363.4557105392479143",
    "price_jpy": "9839303",
    "tick_start": "2026-06-10T15:01:00+09:00",
    "tick_interval": "1m0s",
    "method": "source_vote",
    "vote_band_pct": "0.003",
    "vote_sources": ["bitbank", "bitflyer", "coinbase"],
    "rejected_sources": ["binance_japan"],
    "updated_at": "2026-06-10T15:01:24.347954002+09:00"
  },
  "updated_at": "2026-06-10T15:01:24.347954002+09:00"
}

Tick

channels 包含 ticks,并且一个 tick bucket finalized 后发送。

json
{
  "type": "tick",
  "subscription_id": "btc-live",
  "symbol": "BTC",
  "asset_id": "d388491f-2ee0-337f-bad1-fd8f4444e076",
  "tick_start": "2026-06-10T15:01:00+09:00",
  "tick_interval": "1m0s",
  "aggregate": {
    "source": "aggregated",
    "selected_source": "bitbank",
    "selected_pair": "btc_jpy",
    "price_usd": "61363.4557105392479143",
    "price_jpy": "9839303",
    "method": "source_vote",
    "vote_sources": ["bitbank", "bitflyer", "coinbase"],
    "rejected_sources": ["binance_japan"]
  },
  "sources": {
    "bitbank": {
      "pair": "btc_jpy",
      "native_price": "9839303",
      "native_currency": "JPY",
      "price_jpy": "9839303",
      "price_usd": "61363.4557105392479143",
      "open": "9839303",
      "high": "9839303",
      "low": "9839303",
      "close": "9839303",
      "bid": "9837946",
      "ask": "9837947",
      "transport": "websocket",
      "sample_count": 12
    }
  },
  "updated_at": "2026-06-10T15:02:02.000000000+09:00"
}

Client 示例

js
const ws = new WebSocket("wss://api.coinpost.ai/oracle/ws/prices");

ws.addEventListener("open", () => {
  ws.send(JSON.stringify({
    type: "subscribe",
    id: "btc-live",
    symbols: ["BTC"],
    channels: ["aggregate", "sources"],
    include_snapshot: true
  }));
});

ws.addEventListener("message", (event) => {
  const msg = JSON.parse(event.data);
  if (msg.type === "source_update") {
    console.log(msg.source, msg.bid, msg.ask, msg.native_price);
  }
});

运行说明

  • GET /prices/sources?symbol=BTC 会过滤 stale source tick。使用生产 REST Base URL 时,请求地址是 https://api.coinpost.ai/oracle/prices/sources?symbol=BTC
  • WebSocket source_update 会在实时更新到达时发送。
  • source_update 包含当前 bucket 的 OHLC 值,不是 finalized historical candle。
  • 这个端点不提供完整 order book depth。如果需要完整深度,请使用交易所自己的 order book stream。