Binance TR API Withdraw Endpoint – Invalid Signature Error
I’m getting an “Invalid signature” error when trying to use Binance TR’s /open/v1/withdraws
endpoint.
How can I solve this error ?
{
"code": 3702,
"msg": "Invalid signature.",
"timestamp": 1734095521602
}
Here’s my code:
import hmac
import hashlib
import time
import requests
from urllib.parse import urlencode
from typing import Optional, Dict
class BinanceTRClient:
def __init__(self, api_key: str, secret_key: str, base_url: str = 'https://www.binance.tr'):
self.api_key = api_key
self.secret_key = secret_key
self.base_url = base_url
def _generate_signature(self, params: Dict) -> str:
query_string = urlencode(params)
signature = hmac.new(self.secret_key.encode('utf-8'),
query_string.encode('utf-8'),
hashlib.sha256).hexdigest()
return signature
def withdraw_try(self, amount: float, bank_account: Dict[str, str],
client_id: Optional[str] = None, recv_window: int = 5000) -> Dict:
endpoint = "/open/v1/withdraws"
params = {
'asset': 'TRY',
'amount': str(amount),
'address': bank_account['iban'],
'accountHolder': bank_account['account_holder'],
'bankName': bank_account['bank_name'],
'timestamp': str(int(time.time() * 1000)),
'recvWindow': recv_window
}
print("Request Parameters:", params)
signature = self._generate_signature(params)
params['signature'] = signature
print("Generated Signature:", signature)
headers = {
'X-MBX-APIKEY': self.api_key,
'Content-Type': 'application/x-www-form-urlencoded'
}
print("Request Headers:", headers)
print("Full URL:", f"{self.base_url}{endpoint}")
try:
response = requests.post(
f"{self.base_url}{endpoint}",
headers=headers,
data=params
)
print(f"Full Response: {response.text}")
response_data = response.json()
if 'code' in response_data:
raise BinanceTRException(f"API Error: {response_data['msg']}")
return response_data
except requests.RequestException as e:
raise BinanceTRException(f"Connection error: {str(e)}")
class BinanceTRException(Exception):
pass
def main():
api_key = '' # Your API key here
secret_key = '' # Your secret key here
client = BinanceTRClient(api_key, secret_key)
try:
withdraw_request = client.withdraw_try(
amount=100.00,
bank_account={
'account_holder': '', # account holder
'iban': '', # Your IBAN here
'bank_name': '', #bank name
}
)
print("Withdrawal request successful:")
print(f"Transaction ID: {withdraw_request['data']['withdrawId']}")
print(f"Timestamp: {withdraw_request['timestamp']}")
except BinanceTRException as e:
print(f"Withdrawal error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
if __name__ == "__main__":
main()
New contributor
ALİ BAYIRLI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1