Huobi API 教程

火币(HTX)交易API完整指南 2026

什么是Huobi API?

Huobi API(火币API/HTX API)允许开发者程序化访问火币全球站的交易功能,包括行情查询、账户管理、订单下单等。通过API,您可以构建自动交易机器人、量化策略或市场分析工具。

API类型

类型 用途 频率限制
REST API 行情查询、订单管理 200次/2秒
WebSocket 实时行情推送 无限制

如何申请Huobi API密钥

步骤1: 登录火币全球站
步骤2: 进入「API管理」页面
步骤3: 点击「创建API密钥」,设置标签和权限
步骤4: 完成邮箱/手机验证码验证
步骤5: 记录Access Key和Secret Key(只显示一次)
安全提示: 请立即保存您的Secret Key,它只会显示一次。建议绑定IP白名单增强安全性。

Python代码示例

import requests
import hashlib
import hmac
import time
import base64

HUOBI_API_URL = "https://api.huobi.pro"

def create_signature(access_key, secret_key, method, url, params=None):
    sorted_params = sorted(params.items()) if params else []
    params_str = '&'.join([f"{k}={v}" for k, v in sorted_params])
    pre_hash = f"{method}\n{url}\n{params_str}"
    signature = hmac.new(
        secret_key.encode('utf-8'),
        pre_hash.encode('utf-8'),
        hashlib.sha256
    ).digest()
    return base64.b64encode(signature).decode('utf-8')

def get_account_balance(access_key, secret_key):
    timestamp = time.strftime("%Y-%m-%dT%H:%M:%S")
    params = {
        "AccessKeyId": access_key,
        "SignatureMethod": "HmacSHA256",
        "SignatureVersion": "2",
        "Timestamp": timestamp,
        "account-id": "your-account-id"
    }
    url = "/v1/account/accounts"
    signature = create_signature(access_key, secret_key, "GET", url, params)
    params["Signature"] = signature
    response = requests.get(HUOBI_API_URL + url, params=params)
    return response.json()

# 使用示例
# balance = get_account_balance("your_access_key", "your_secret_key")
# print(balance)

WebSocket连接示例

import websocket
import json

ws_url = "wss://api.huobi.pro/ws"

def on_message(ws, message):
    data = json.loads(message)
    print(data)

def on_error(ws, error):
    print(f"Error: {error}")

def on_close(ws):
    print("Connection closed")

ws = websocket.WebSocketApp(
    ws_url,
    on_message=on_message,
    on_error=on_error,
    on_close=on_close
)

# 订阅行情数据
subscribe_msg = {
    "sub": "market.btcusdt.kline.1min",
    "id": "channel"
}
ws.send(json.dumps(subscribe_msg))
ws.run_forever()

API端点

端点 URL
REST API https://api.huobi.pro
WebSocket wss://api.huobi.pro/ws

常见API接口

接口 方法 说明
/market/tickers GET 获取所有交易对行情
/market/depth GET 获取市场深度
/v1/order/orders POST 创建订单
/v1/order/orders/{order-id} GET 查询订单详情

开始交易

通过API交易,享受专业级服务:注册OKX