-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Description
Describe the bug
The Client object's API_URL
attribute is incorrectly set to the live Binance API endpoint (https://api.binance.com/api) when testnet=True
is passed during initialization. It should point to the Testnet endpoint (https://testnet.binance.vision/api). Fortunately, i found a workaround online. Its very likely this is already known but i could not find an official Binance solution.
To Reproduce
Code snippet to reproduce the behaviour:
import logging
from binance.client import Client
API_KEY = "YOUR_TESTNET_API_KEY"
API_SECRET = "YOUR_TESTNET_API_SECRET"
USE_TESTNET = True # This should be True
client = Client(API_KEY, API_SECRET, testnet=USE_TESTNET)
print(f"testnet flag passed to Client: {USE_TESTNET}")
print(f"client.testnet attribute: {client.testnet}")
print(f"client.API_URL: {client.API_URL}")
try:
if USE_TESTNET:
account_info = client.get_account()
print(f"Successfully fetched account info from: {client.API_URL}")
print(f"Account Info (Testnet): {account_info.get('makerCommission')}, Balances: {len(account_info.get('balances', []))}")
except Exception as e:
print(f"Error during API call: {e}")
Expected behavior
When testnet=True
is passed to the Client constructor, client.API_URL
should be https://testnet.binance.vision/api.
Environment (please complete the following information):
- Python version: - [3.11.7]
- Virtual Env: - [venv]
- OS: - [Windows 11]
- python-binance version - [1.0.29]
Logs
Please set logging to debug and paste any logs here, or upload debug.log
file to the issue.
(Note: The "override" messages in the log are from the local workaround).
2025-05-23 07:15:52 - binance_api_manager - DEBUG - Client object created: Initial API_URL=https://api.binance.com/api, testnet_flag_passed=True
2025-05-23 07:15:52 - binance_api_manager - WARNING - Initial API_URL (https://api.binance.com/api) is not the expected Testnet URL (https://testnet.binance.vision/api) despite testnet=True. Manually overriding API_URL.
2025-05-23 07:15:52 - binance_api_manager - DEBUG - Effective API_URL after potential override: https://testnet.binance.vision/api
2025-05-23 07:15:53 - binance_api_manager - INFO - Account info retrieved: makerCommission=0, balances count=409
2025-05-23 07:15:53 - binance_api_manager - DEBUG - Effective API endpoint reported by client after get_account: https://testnet.binance.vision/api
Additional context
I am using what I believe to be the latest version of python-binance. I understand recent updates (like PR #1583) were made concerning Testnet URLs, but this behavior is still observed.
My current workaround is to manually set client.API_URL
= 'https://testnet.binance.vision/api' after client initialization if testnet=True
and the client.API_URL
is incorrect. This workaround allows me to connect to the Testnet successfully.