Bitfinex API 教程

Bitfinex交易API完整指南 2026

什么是Bitfinex API?

Bitfinex API提供强大的程序化交易能力,支持REST API和WebSocket连接,可访问高级交易功能,包括杠杆交易、合约交易和融资市场。

API类型

类型 URL 用途
REST API v2 api.bitfinex.com 订单管理、账户操作
WebSocket v2 ws.bitfinex.com 实时行情推送

如何申请API密钥

步骤1: 登录Bitfinex
步骤2: 进入「平台设置」→「API」
步骤3: 点击「生成新API密钥」
步骤4: 设置权限(读取/交易/提款)
步骤5: 完成2FA验证,获取API Key和Secret
安全提示: 强烈建议只开启必要权限,启用IP白名单限制API访问。

Python代码示例

import requests
import json
import time
import hmac
import hashlib
import base64

BASE_URL = "https://api.bitfinex.com"

def generate_signature(secret, payload):
    return base64.b64encode(
        hmac.new(secret.encode(), payload.encode(), hashlib.sha384).digest()
    ).decode()

def fetch_balance(api_key, api_secret):
    endpoint = "/v2/auth/r/wallets"
    nonce = str(int(time.time() * 1000000))
    body = {}
    
    signature_payload = f"POST{endpoint}{nonce}{json.dumps(body)}"
    signature = generate_signature(api_secret, signature_payload)
    
    headers = {
        "bfx-apikey": api_key,
        "bfx-signature": signature,
        "bfx-nonce": nonce,
        "Content-Type": "application/json"
    }
    
    response = requests.post(BASE_URL + endpoint, headers=headers, json=body)
    return response.json()

# 使用示例
# balance = fetch_balance("your_api_key", "your_api_secret")
# print(balance)

WebSocket实时行情

import websocket
import json

ws_url = "wss://ws.bitfinex.com/ws/2"

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

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

# 订阅BTC/USDticker
subscribe_msg = {
    "event": "subscribe",
    "channel": "ticker",
    "pair": "BTC/USD"
}
ws.send(json.dumps(subscribe_msg))
ws.run_forever()

主要API端点

端点 方法 说明
/v2/auth/r/wallets POST 获取钱包余额
/v2/auth/r/orders POST 获取活跃订单
/v2/auth/w/order/new POST 提交新订单
/v2/auth/r/orders/{symbol}/hist POST 查询历史订单

开始交易

专业级加密货币交易:注册OKX