[Python] Bitunix Futures Trading Place Order Signature Error in Python ( 1007 )

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to integrate with the Bitunix API (https://openapidoc.bitunix.com/doc/common/introduction.html) using Python and Request. I implemented authentication and signature generation as per the documentation, but when I try to place an order, the API always responds with:

    Below is just Bitunix class

    import base64
    import hashlib
    import secrets
    import time
    import re
    import requests
    import json
    from app.core.config import settings

    class Bitunix:
    def __init__(self):

    self.api_key = settings.BITUNIX_API_KEY
    self.api_secret = settings.BITUNIX_SECRET_KEY
    self.base_url = "https://fapi.bitunix.com/api/v1/futures"

    def sha256_hex(self, input_string):
    return hashlib.sha256(input_string.encode('utf-8')).hexdigest()

    def sign_request(self, nonce: str, timestamp: str, method: str, data: dict):
    query_params = ""
    body = ""
    if data:
    if method.lower() == "get":
    data = {k: v for k, v in data.items() if v is not None}
    query_params = '&'.join([f"{k}={v}" for k, v in sorted(data.items())])
    query_params = re.sub(r'[^a-zA-Z0-9]', '', query_params).replace(' ', '')
    if method.lower() == "post":
    body = json.dumps(data).replace(" ", "")

    digest_input = nonce + timestamp + self.api_key + query_params + body
    digest = self.sha256_hex(digest_input)

    sign_input = digest + self.api_secret
    sign = self.sha256_hex(sign_input)
    return sign

    def create_long_order(self, entry_price: str, tp_price: str, sl_price: str):
    print(f"create long order: {entry_price} {tp_price} {sl_price}")
    tp_order_price = get_up_price(float(tp_price), 0.0001)
    sl_order_price = get_down_price(float(sl_price), 0.0001)
    data = {
    "symbol":"BTCUSDT",
    "side": "BUY",
    "price": entry_price,
    "qty": "0.001",
    "tradeSide": "OPEN",
    "orderType": "LIMIT",
    "reduceOnly": False,
    "effect": "GTC",
    "tpPrice": tp_price,
    "tpStopType": "MARK",
    "tpOrderType": "LIMIT",
    "tpOrderPrice": tp_order_price,
    "slPrice": sl_price,
    "slStopType": "MARK",
    "slOrderType": "LIMIT",
    "slOrderPrice": sl_order_price
    }
    method = "post"
    timestamp = str(int(time.time() * 1000))
    random_bytes = secrets.token_bytes(32)
    nonce = base64.b64encode(random_bytes).decode('utf-8')

    sign = self.sign_request(nonce, timestamp, method, data)
    url = f"{self.base_url}/trade/place_order"
    headers = {
    "api-key": self.api_key,
    "sign": sign,
    "nonce": nonce,
    "timestamp": timestamp,
    "language": "en-US",
    "Content-Type": "application/json"
    }
    response = requests.request(method, url, headers=headers, data=data, timeout=10)
    response_data = response.json()
    return response_data


    Below is code to call bitunix class:

    @router.get("/webhook/bitunix")
    async def test_hook(session: SessionDep):
    ticker = bitunix.get_ticker("BTCUSDT")
    entry_price = ticker.get('markPrice')
    tp_price = str(float(entry_price) + 138)
    sl_price = str(float(entry_price) - 138)
    response_data = bitunix.create_long_order(entry_price, tp_price, sl_price)
    return {
    "status": "success",
    "message": "Signal received",
    "mark_price": entry_price,
    "tp_price": tp_price,
    "sl_price": sl_price
    }


    When I Run this code, I got below error:

    {'code': 10007, 'data': None, 'msg': 'Signature Error'}

    Could you please help me to solve this problem ?

    Continue reading...

Compartilhe esta Página