跳至主要內容
入門

REST API

深入了解 Bitcoin Core 的 REST 接口,無需認證即可查詢區塊鏈數據。

8 分鐘

什麼是 REST API?

Bitcoin Core 提供一個 REST(REpresentational State Transfer)接口, 允許無需認證即可查詢區塊鏈數據。REST API 是只讀的,適合公開數據查詢。

REST vs RPC

特性 REST RPC
認證 不需要 需要
操作類型 只讀 讀寫
輸出格式 JSON/HEX/BIN JSON
預設端口 8332 8332

配置

# bitcoin.conf

# 啟用 REST 接口
rest=1

# RPC 綁定地址(REST 共用)
rpcbind=127.0.0.1

# 允許的 IP(REST 共用)
rpcallowip=127.0.0.1

安全提示: REST 接口不需要認證,請勿暴露到公網。生產環境應使用反向代理。

端點列表

區塊查詢

# 獲取區塊(JSON 格式)
curl http://localhost:8332/rest/block/BLOCKHASH.json

# 獲取區塊(十六進制)
curl http://localhost:8332/rest/block/BLOCKHASH.hex

# 獲取區塊(二進制)
curl http://localhost:8332/rest/block/BLOCKHASH.bin

# 獲取區塊頭
curl http://localhost:8332/rest/headers/COUNT/BLOCKHASH.json

# 範例:獲取創世區塊
GENESIS=000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
curl http://localhost:8332/rest/block/$GENESIS.json | jq '.hash, .height'

區塊雜湊

# 根據高度獲取區塊雜湊
curl http://localhost:8332/rest/blockhashbyheight/HEIGHT.json

# 範例:獲取高度 100 的區塊
curl http://localhost:8332/rest/blockhashbyheight/100.json

# 輸出
{
  "blockhash": "000000007bc154e0fa7ea32218a72fe2c1bb9f86cf8c9ebf9a715ed27fdb229a"
}

交易查詢

# 獲取交易(需要 txindex=1)
curl http://localhost:8332/rest/tx/TXID.json

# 獲取交易(十六進制)
curl http://localhost:8332/rest/tx/TXID.hex

# 範例
TXID=4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
curl http://localhost:8332/rest/tx/$TXID.json | jq '.txid, .size'

UTXO 查詢

# 查詢 UTXO(POST 請求)
# 請求體格式:checkmempool/TXID-VOUT/TXID-VOUT/...

# 檢查特定 UTXO 是否存在
curl -X GET "http://localhost:8332/rest/getutxos/checkmempool/TXID-0.json"

# 批量查詢多個 UTXO
curl -X GET "http://localhost:8332/rest/getutxos/TXID1-0/TXID2-1.json"

# 輸出格式
{
  "chainHeight": 800000,
  "chaintipHash": "...",
  "bitmap": "1",  # 位圖表示哪些 UTXO 存在
  "utxos": [
    {
      "height": 700000,
      "value": 1.5,
      "scriptPubKey": {...}
    }
  ]
}

Mempool

# 獲取 mempool 資訊
curl http://localhost:8332/rest/mempool/info.json

# 輸出
{
  "loaded": true,
  "size": 5432,
  "bytes": 3456789,
  "usage": 12345678,
  "total_fee": 0.12345678,
  "maxmempool": 300000000,
  "mempoolminfee": 0.00001000,
  "minrelaytxfee": 0.00001000,
  "incrementalrelayfee": 0.00001000
}

# 獲取 mempool 內容
curl http://localhost:8332/rest/mempool/contents.json

區塊鏈資訊

# 獲取區塊鏈資訊
curl http://localhost:8332/rest/chaininfo.json

# 輸出
{
  "chain": "main",
  "blocks": 800000,
  "headers": 800000,
  "bestblockhash": "...",
  "difficulty": 53911173001054.59,
  "time": 1690000000,
  "mediantime": 1689999000,
  "verificationprogress": 0.9999999,
  "initialblockdownload": false,
  "chainwork": "...",
  "size_on_disk": 500000000000,
  "pruned": false
}

輸出格式

格式 擴展名 Content-Type
JSON .json application/json
十六進制 .hex text/plain
二進制 .bin application/octet-stream

實用範例

# 獲取最新區塊
BEST=$(curl -s http://localhost:8332/rest/chaininfo.json | jq -r '.bestblockhash')
curl -s "http://localhost:8332/rest/block/$BEST.json" | jq '.height, .tx | length'

# 獲取最近 5 個區塊頭
curl -s "http://localhost:8332/rest/headers/5/$BEST.json" | jq '.[].height'

# 檢查區塊鏈同步狀態
curl -s http://localhost:8332/rest/chaininfo.json | jq '{
  height: .blocks,
  synced: (.verificationprogress > 0.9999),
  pruned: .pruned
}'

# 獲取 mempool 統計
curl -s http://localhost:8332/rest/mempool/info.json | jq '{
  tx_count: .size,
  total_size_mb: (.bytes / 1048576),
  min_fee: .mempoolminfee
}'

Python 範例

import requests

class BitcoinREST:
    def __init__(self, host='localhost', port=8332):
        self.base_url = f'http://{host}:{port}/rest'

    def get_chaininfo(self):
        r = requests.get(f'{self.base_url}/chaininfo.json')
        return r.json()

    def get_block(self, blockhash, format='json'):
        r = requests.get(f'{self.base_url}/block/{blockhash}.{format}')
        return r.json() if format == 'json' else r.text

    def get_block_by_height(self, height):
        r = requests.get(f'{self.base_url}/blockhashbyheight/{height}.json')
        blockhash = r.json()['blockhash']
        return self.get_block(blockhash)

    def get_mempool_info(self):
        r = requests.get(f'{self.base_url}/mempool/info.json')
        return r.json()

# 使用
api = BitcoinREST()
info = api.get_chaininfo()
print(f"區塊高度: {info['blocks']}")

block = api.get_block_by_height(100)
print(f"區塊 100 有 {len(block['tx'])} 筆交易")

限制

  • 只讀接口,無法發送交易或修改錢包
  • 交易查詢需要啟用 txindex=1
  • 無法查詢錢包相關數據
  • 無速率限制,需自行實現

總結

  • 無需認證:適合公開數據查詢
  • 多種格式:JSON、HEX、BIN
  • 區塊鏈查詢:區塊、交易、UTXO、mempool
  • 只讀:無法執行寫入操作
已複製連結
已複製到剪貼簿