binance-futures-connector-python
repository·main·Indexed 22 days ago
https://github.com/binance/binance-futures-connector-pythonA lightweight Python connector for the Binance Futures public API, supporting USDT-M and COIN-M futures via RESTful APIs and WebSockets. It features HMAC and RSA authentication, proxy support, and WebSocket stream clients. Note: This repository is deprecated; users are advised to use binance-connector-python.
What's inside binance-futures-connector-python
- Unlike standard Python PEP8 conventions, this connector requires that optional parameters in method calls match the exact casing and naming used in the Binance API documentation. Using lowercase or underscores where the API expects CamelCase will result in the parameter being unrecognized.
Authenticate with HMAC or RSA
mainThe connector supports two authentication methods:
- HMAC Authentication: Pass
keyandsecretto the client constructor. - RSA Authentication: Pass the
key(public key string),private_key(content of your.pemfile), and an optionalprivate_key_passphrase(if the key is encrypted).
# HMAC Authentication client = Client(api_key, api_secret) print(client.account()) # RSA Authentication key = "" with open("/Users/john/private_key.pem", "r") as f: # Location of private key file private_key = f.read() private_key_passphrase = "" # Optional: only used for encrypted RSA key client = Client(key=key, private_key=private_key, private_key_passphrase=private_key_passphrase) print(client.account())- HMAC Authentication: Pass
Use WebSocket Stream Client
mainEstablish WebSocket connections for market or user data streams. You must provide an
on_messagecallback function to handle incoming messages.Key Features:
- Request ID: You can optionally pass an
idto each request; otherwise, the library generates a random UUID. - Combined Streams: Set
is_combined=Trueto append/stream/to thebaseURLinstead of the default/ws/. - Proxy Support: Pass a
proxiesdictionary (e.g.,{'http': 'http://user:pass@host:port'}) during initialization. - Heartbeat: The library automatically handles pong responses to server pings.
import time from binance.websocket.um_futures.websocket_client import UMFuturesWebsocketClient def message_handler(_, message): print(message) # Initialize with proxy proxies = {'http': 'http://1.2.3.4:8080'} my_client = UMFuturesWebsocketClient(on_message=message_handler, proxies=proxies) # Subscribe with a custom request ID my_client.agg_trade(symbol="bnbusdt", id="my_request_id") time.sleep(5) my_client.stop()- Request ID: You can optionally pass an
Use RESTful APIs for COIN-M Futures
mainUse the
CMFuturesclass to interact with COIN-M Delivery APIs. You can perform public actions like getting server time or private actions like accessing account information and placing orders by providing your API credentials.Common methods include:
time(): Get server time.account(): Get account information.new_order(**params): Post a new order.
from binance.cm_futures import CMFutures # Public API usage cm_futures_client = CMFutures() print(cm_futures_client.time()) # Private API usage cm_futures_client = CMFutures(key='<api_key>', secret='<api_secret>') # Get account information print(cm_futures_client.account()) # Post a new order params = { 'symbol': 'BTCUSDT', 'side': 'SELL', 'type': 'LIMIT', 'timeInForce': 'GTC', 'quantity': 0.002, 'price': 59808 } response = cm_futures_client.new_order(**params) print(response)Install binance-futures-connector
mainInstall the library using pip to connect to Binance Futures public APIs (USDT-M and COIN-M).
Note: This repository is deprecated. Please use the new modular connector repository: binance-connector-python
pip install binance-futures-connectorConfigure Client parameters: timeout, proxy, and base_url
mainWhen initializing a client, you can customize several connection parameters:
base_url: The API endpoint. Defaults tofapi.binance.comfor USDT-M anddapi.binance.comfor COIN-M. It is recommended to provide this explicitly.timeout: Number of seconds to wait for a server response. Defaults toNone(no timeout).proxies: A dictionary for proxy settings (e.g.,{'https': 'http://1.2.3.4:8080'}).show_limit_usage: Set toTrueto include weight usage in response metadata.
Handle ClientError and ServerError
mainThe library raises two types of errors:
binance.error.ClientError: Thrown for4XXHTTP status codes (client-side issues). Properties includestatus_code,error_code,error_message, andheader.binance.error.ServerError: Thrown for5XXHTTP status codes (server-side issues).