Bitfinex API 设置步骤:从账号注册到交易操作详解

发布于 2025-02-07 11:44:22 · 阅读量: 178538

Bitfinex API 设置步骤

想玩转 Bitfinex API?这可不是随便点几下就能搞定的事儿,但别慌,跟着这篇指南走,包你顺利上车,爽快撸 API!

1. 账号准备 & API 权限设置

首先,得有个 Bitfinex 账号,没有的话先去注册,记得开 两步验证(2FA),不然 API 可不给你面子。

然后,登录后进入 API Key 管理界面

  • 依次点击 账户 → API 密钥 → 创建新的密钥
  • 这里要勾选 API 需要的权限,比如:
  • 读取余额(Account Info)
  • 交易权限(Orders & Trades)
  • 提币权限(要慎重,安全第一)
  • 设置好后,点击 生成 API 密钥

💡 注意
- API Key 只显示一次,别搞丢了,不然得重新申请。
- API Secret 也要保存好,这可是你访问 API 的通行证!

2. 安装 Bitfinex API 依赖

API 搞定了,接下来是技术活,得装上 Bitfinex API SDK,推荐用 Python,方便又强大:

bash pip install bitfinex

或者如果你更偏爱 requests,那就手动请求也行:

bash pip install requests

3. 连接 Bitfinex API

直接上代码,连通 Bitfinex API,检查 API Key 是否有效:

import requests import time import hmac import hashlib

API_KEY = '你的API Key' API_SECRET = '你的API Secret' BASE_URL = 'https://api.bitfinex.com'

def get_headers(endpoint, payload): payload_json = json.dumps(payload) payload_encoded = base64.b64encode(payload_json.encode()) signature = hmac.new(API_SECRET.encode(), payload_encoded, hashlib.sha384).hexdigest()

return {
    'bfx-apikey': API_KEY,
    'bfx-signature': signature,
    'bfx-nonce': str(int(time.time() * 1000))
}

def check_account_info(): endpoint = '/v2/auth/r/wallets' payload = {} headers = get_headers(endpoint, payload)

response = requests.post(BASE_URL + endpoint, headers=headers, json=payload)
return response.json()

print(check_account_info())

如果返回你的钱包数据,说明 API 正常运行,可以愉快开整了!

4. 下单操作

搞定了连接,接下来尝试挂单,买点 BTC 玩玩:

def place_order(symbol, amount, price, side): endpoint = '/v2/auth/w/order/submit' payload = { "type": "LIMIT", "symbol": symbol, "amount": str(amount), "price": str(price), "side": side }

headers = get_headers(endpoint, payload)
response = requests.post(BASE_URL + endpoint, headers=headers, json=payload)
return response.json()

示例:在 42000 USDT 价格买入 0.01 BTC

print(place_order('tBTCUSD', 0.01, 42000, 'buy'))

注意:Bitfinex 交易对一般是 tBTCUSD 这种格式,别写成 BTC/USDT,不然 API 可不认!

5. 查询订单

想看看自己挂的单?来查查订单列表:

def get_orders(): endpoint = '/v2/auth/r/orders' payload = {} headers = get_headers(endpoint, payload)

response = requests.post(BASE_URL + endpoint, headers=headers, json=payload)
return response.json()

print(get_orders())

6. 取消订单

后悔了?单子不想要了?来取消订单:

def cancel_order(order_id): endpoint = '/v2/auth/w/order/cancel' payload = {"id": order_id}

headers = get_headers(endpoint, payload)
response = requests.post(BASE_URL + endpoint, headers=headers, json=payload)
return response.json()

示例:取消订单 ID 为 123456 的订单

print(cancel_order(123456))

7. 余额查询

看看自己还有多少钱:

def get_balance(): endpoint = '/v2/auth/r/wallets' payload = {}

headers = get_headers(endpoint, payload)
response = requests.post(BASE_URL + endpoint, headers=headers, json=payload)
return response.json()

print(get_balance())

8. 退出 API 访问

Bitfinex API 没有专门的 Logout 机制,API Key 只要没被撤销就一直有效。如果要彻底退出 API 访问,直接去 Bitfinex 后台把 API Key 删除 就行。


Bitfinex API 的玩法可不少,比如 WebSocket 实时数据、杠杆交易、期货操作等,以上只是基础用法,更多骚操作可以自己摸索,或者直接翻 Bitfinex API 文档!

Gate.io Logo 加入 Gate.io,注册赢取最高$6666迎新任务奖励!