如何从V3 coinbase API获取交易?

问题描述 投票:0回答:1

我正在尝试从 Coinbase 的 API 获取我的账户的交易数据(例如:“用 USDC 购买了 X_AMOUNT 的 BTC”)。我尝试过

/api/v3/brokerage/orders/historical/fills
/api/v3/brokerage/orders/historical/batch
,但尽管我可以看到帐户中最近的交易,但两者都返回空数据。我见过的所有与此相关的帖子都使用已弃用的 V2 API。有没有办法用当前的 API 来做到这一点?

coinbase-api
1个回答
0
投票

设置您的环境

确保您已准备好 API 密钥、秘密和密码。 如果您还没有安装 requests 库,请安装:

pip install requests

API 调用示例:

import requests
import time
import hmac
import hashlib
import base64

# Replace these values with your actual API key, secret, and passphrase
api_key = 'your_api_key'
api_secret = 'your_api_secret'
api_passphrase = 'your_api_passphrase'

# Get the current timestamp in seconds
timestamp = str(int(time.time()))
method = 'GET'
request_path = '/api/v3/brokerage/orders/historical/fills'

# Create the prehash string by concatenating the timestamp, method, and request path
message = timestamp + method + request_path
hmac_key = base64.b64decode(api_secret)
signature = hmac.new(hmac_key, message.encode('utf-8'), hashlib.sha256).digest()
signature_b64 = base64.b64encode(signature).decode('utf-8')

headers = {
    'CB-ACCESS-KEY': api_key,
    'CB-ACCESS-SIGN': signature_b64,
    'CB-ACCESS-TIMESTAMP': timestamp,
    'CB-ACCESS-PASSPHRASE': api_passphrase,
    'Content-Type': 'application/json'
}

url = 'https://api.coinbase.com' + request_path

response = requests.get(url, headers=headers)
data = response.json()

print(data)

假设您的帐户中有交易,响应可能如下所示:

{
  "fills": [
    {
      "order_id": "12345",
      "trade_id": "67890",
      "product_id": "BTC-USDC",
      "price": "30000.00",
      "size": "0.01",
      "fee": "0.0001",
      "side": "buy",
      "created_at": "2023-07-01T10:00:00Z",
      "liquidity": "T",
      "settled": true
    },
    {
      "order_id": "23456",
      "trade_id": "78901",
      "product_id": "ETH-USDC",
      "price": "2000.00",
      "size": "0.5",
      "fee": "0.01",
      "side": "sell",
      "created_at": "2023-07-02T11:00:00Z",
      "liquidity": "M",
      "settled": true
    }
  ],
  "pagination": {
    "after": "cursor_after",
    "before": "cursor_before",
    "limit": 100
  }
}

注:

正确的API密钥权限:确保API密钥具有访问交易数据的正确权限。

日期范围和过滤器:确保您正在查询的交易落在请求中应用的日期范围和其他过滤器(如果有)内。

验证交易存在:确认您的账户中在指定时间内确实有交易。

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.