Binance Public API Connector Python
This is a lightweight library that works as a connector to Binance public API. It’s designed to be simple, clean, and easy to use with minimal dependencies.
Source Code: https://github.com/binance/binance-connector-python
Official API document:
Support channels:
Binance developer forum: https://dev.binance.vision/
Telegram Channel: https://t.me/binance_api_english
API key setup: https://www.binance.com/en-NG/support/faq/360002502072
Testnet API key setup: https://dev.binance.vision/t/99
Features
Supported APIs:
/api/*
/sapi/*
Spot Websocket Market Stream
Spot User Data Stream
Inclusion of test cases and examples
Customizable base URL, request timeout and HTTP proxy
Response metadata can be displayed
Quick Start
Installation
Install via package name
pip install binance-connector
Alternatively, install with git repository path
python -m pip install git+https://github.com/binance/binance-connector-python.git
Usage
RESTful APIs
from binance.spot import Spot
client = Spot()
print(client.time())
client = Spot(key='<api_key>', secret='<api_secret>')
# Get account information
print(client.account())
# Post a new order
params = {
'symbol': 'BTCUSDT',
'side': 'SELL',
'type': 'LIMIT',
'timeInForce': 'GTC',
'quantity': 0.002,
'price': 9500
}
response = client.new_order(**params)
print(response)
Please find examples folder to check for more endpoints.
Websocket
from binance.websocket.spot.websocket_client import SpotWebsocketClient as WebsocketClient
def message_handler(message):
print(message)
ws_client = WebsocketClient()
ws_client.start()
ws_client.mini_ticker(
symbol='bnbusdt',
id=1,
callback=message_handler,
)
# Combine selected streams
ws_client.instant_subscribe(
stream=['bnbusdt@bookTicker', 'ethusdt@bookTicker'],
callback=message_handler,
)
ws_client.stop()
More websocket examples are available in the examples folder
Contents
- Changelog
- Getting Started
- Spot APIs
- Blvt Endpoints
- Bswap Endpoints
- List All Swap Pools (MARKET_DATA)
- Get liquidity information of a pool (USER_DATA)
- Add Liquidity (TRADE)
- Remove Liquidity (TRADE)
- Get Liquidity Operation Record (USER_DATA)
- Request Quote (USER_DATA)
- Swap (TRADE)
- Get Swap History (USER_DATA)
- Get Pool Configure (USER_DATA)
- Add Liquidity Preview (USER_DATA)
- Remove Liquidity Preview (USER_DATA)
- Get Unclaimed Rewards Record (USER_DATA)
- Claim rewards (TRADE)
- Get Claimed History (USER_DATA)
- C2C Endpoints
- Convert Endpoints
- Data Stream Endpoints
- Create a ListenKey (USER_STREAM)
- Ping/Keep-alive a ListenKey (USER_STREAM)
- Close a ListenKey (USER_STREAM)
- Create a margin ListenKey (USER_STREAM)
- Renew a margin ListenKey (USER_STREAM)
- Close a margin ListenKey (USER_STREAM)
- Create an isolated margin ListenKey (USER_STREAM)
- Renew an isolated ListenKey (USER_STREAM)
- Close an isolated margin ListenKey (USER_STREAM)
- Fiat Endpoints
- Futures Endpoints
- New Future Account Transfer (USER_DATA)
- Get Future Account Transaction History List (USER_DATA)
- Borrow For Cross-Collateral (TRADE)
- Cross-Collateral Borrow History (USER_DATA)
- Repay For Cross-Collateral (TRADE)
- Cross-Collateral Repayment History (USER_DATA)
- Cross-Collateral Wallet (USER_DATA)
- Cross-Collateral Information (USER_DATA)
- Calculate Rate After Adjust Cross-Collateral LTV (USER_DATA)
- Get Max Amount for Adjust Cross-Collateral LTV (USER_DATA)
- Adjust Cross-Collateral LTV (TRADE)
- Adjust Cross-Collateral LTV History (USER_DATA)
- Cross-Collateral Liquidation History (USER_DATA)
- Check Collateral Repay Limit (USER_DATA)
- Get Collateral Repay Quote (USER_DATA)
- Repay with Collateral (USER_DATA)
- Collateral Repayment Result (USER_DATA)
- Cross-Collateral Interest History (USER_DATA)
- Card Gift Endpoints
- Crypto Loans Endpoints
- Margin Endpoints
- Margin Account Transfer (MARGIN)
- Margin Account Borrow (MARGIN)
- Margin Account Repay(MARGIN)
- Query Margin Asset (MARKET_DATA)
- Query Margin Pair (MARKET_DATA)
- Get All Margin Assets (MARKET_DATA)
- Get All Margin Pairs (MARKET_DATA)
- Query Margin PriceIndex (MARKET_DATA)
- Margin Account New Order (TRADE)
- Margin Account Cancel Order (TRADE)
- Get Transfer History (USER_DATA)
- Query Loan Record (USER_DATA)
- Query Repay Record (USER_DATA)
- Get Interest History (USER_DATA)
- Get Force Liquidation Record (USER_DATA)
- Query Cross Margin Account Details (USER_DATA)
- Query Margin Account’s Order (USER_DATA)
- Query Margin Account’s Open Order (USER_DATA)
- Margin Account Cancel all Open Orders on a Symbol (USER_DATA)
- Query Margin Account’s All Orders (USER_DATA)
- Query Margin Account’s Trade List (USER_DATA)
- Query Max Borrow (USER_DATA)
- Query Max Transfer-Out Amount (USER_DATA)
- Isolated Margin Account Transfer (MARGIN)
- Get Isolated Margin Transfer History (USER_DATA)
- Query Isolated Margin Account Info (USER_DATA)
- Query Isolated Margin Symbol (USER_DATA)
- Get All Isolated Margin Symbol(USER_DATA)
- Toggle BNB Burn On Spot Trade And Margin Interest (USER_DATA)
- Get BNB Burn Status (USER_DATA)
- Get Margin Interest Rate History (USER_DATA)
- Margin Account New OCO (TRADE)
- Margin Account Cancel OCO (TRADE)
- Query Margin Account’s OCO (USER_DATA)
- Query Margin Account’s all OCO (USER_DATA)
- Query Margin Account’s Open OCO (USER_DATA)
- Disable Isolated Margin Account (TRADE)
- Enable Isolated Margin Account (TRADE)
- Query Enabled Isolated Margin Account Limit (USER_DATA)
- Query Cross Margin Fee Data (USER_DATA)
- Query Isolated Margin Fee Data (USER_DATA)
- Query Isolated Margin Tier Data (USER_DATA)
- Query Current Margin Order Count Usage (TRADE)
- Market Endpoints
- Mining Endpoints
- Acquiring Algorithm (MARKET_DATA)
- Acquiring CoinName (MARKET_DATA)
- Request for Detail Miner List (USER_DATA)
- Request for Miner List (USER_DATA)
- Revenue List (USER_DATA)
- Extra Bonus List (USER_DATA)
- Statistic List (USER_DATA)
- Account List (USER_DATA)
- Hashrate Resale Request (USER_DATA)
- Cancel hashrate resale configuration(USER_DATA)
- Hashrate Resale List (USER_DATA)
- Hashrate Resale Detail (USER_DATA)
- Mining Account Earning (USER_DATA)
- NFT Endpoints
- Pay Endpoints
- Portfolio Margin Endpoints
- Rebate Endpoints
- Savings Endpoints
- Get Flexible Product List (USER_DATA)
- Get Left Daily Purchase Quota of Flexible Product (USER_DATA)
- POST Purchase Flexible Product (USER_DATA)
- Get Left Daily Redemption Quota of Flexible Product (USER_DATA)
- Redeem Flexible Product (USER_DATA)
- Get Flexible Product Position (USER_DATA)
- Get Fixed and Activity Project List(USER_DATA)
- Purchase Fixed/Activity Project (USER_DATA)
- Get Fixed/Activity Project Position (USER_DATA)
- Lending Account (USER_DATA)
- Get Purchase Record (USER_DATA)
- Get Redemption Record (USER_DATA)
- Get Interest History (USER_DATA)
- Change Fixed/Activity Position to Daily Position(USER_DATA)
- Staking Endpoints
- Sub Account Endpoints
- Create a Virtual Sub-account(For Master Account)
- Query Sub-account List(For Master Account)
- Query Sub-account Assets(For Master Account)
- Get Sub-account Deposit Address (For Master Account)
- Get Sub-account Deposit History (For Master Account)
- Get Sub-account’s Status on Margin/Futures(For Master Account)
- Enable Margin for Sub-account (For Master Account)
- Get Detail on Sub-account’s Margin Account (For Master Account)
- Get Summary of Sub-account’s Margin Account (For Master Account)
- Enable Futures for Sub-account (For Master Account)
- Futures Transfer for Sub-account(For Master Account)
- Margin Transfer for Sub-account(For Master Account)
- Transfer to Sub-account of Same Master(For Sub-account)
- Transfer to Master(For Sub-account)
- Sub-account Transfer History (For Sub-account)
- Query Sub-account Futures Asset Transfer History(For Master Account)
- Query Sub-account Futures Asset Transfer History(For Master Account)
- Query Sub-account Spot Assets Summary (For Master Account)
- Universal Transfer (For Master Account)
- Query Universal Transfer History (For Master Account)
- Get Detail on Sub-account’s Futures Account V2 (For Master Account)
- Get Summary of Sub-account’s Futures Account V2 (For Master Account)
- Get Futures Position-Risk of Sub-account V2 (For Master Account)
- Query Sub-account Spot Asset Transfer History (SAPI For Master Account)
- Enable Leverage Token for Sub-account(For Master Account)
- Deposit assets into the managed sub-account (For Investor Master Account)
- Query managed sub-account asset details (For Investor Master Account)
- Withdrawl assets from the managed sub-account (For Investor Master Account)
- Enable or Disable IP Restriction for a Sub-account API Key (For Master Account)
- Add IP List for a Sub-account API Key (For Master Account)
- Get IP Restriction for a Sub-account API Key (For Master Account)
- Delete IP List for a Sub-account API Key (For Master Account)
- Query Managed Sub-account Snapshot (For Investor Master Account)
- Account / Trade Endpoints
- Test New Order (TRADE)
- New Order (TRADE)
- Cancel Order (TRADE)
- Cancel all Open Orders on a Symbol (TRADE)
- Query Order (USER_DATA)
- Current Open Orders (USER_DATA)
- All Orders (USER_DATA)
- New OCO (TRADE)
- Cancel OCO (TRADE)
- Query OCO (USER_DATA)
- Query all OCO (USER_DATA)
- Query Open OCO (USER_DATA)
- Account Information (USER_DATA)
- Account Trade List (USER_DATA)
- Query Current Order Count Usage (TRADE)
- Wallet Endpoints
- System Status (System)
- All Coins’ Information (USER_DATA)
- Daily Account Snapshot (USER_DATA)
- Disable Fast Withdraw Switch (USER_DATA)
- Enable Fast Withdraw Switch (USER_DATA)
- Withdraw (USER_DATA)
- Deposit History(supporting network) (USER_DATA)
- Withdraw History (supporting network) (USER_DATA)
- Deposit Address (supporting network) (USER_DATA)
- Account Status (USER_DATA)
- Account API Trading Status (USER_DATA)
- DustLog (USER_DATA)
- User Universal Transfer
- Query User Universal Transfer History
- Dust Transfer (USER_DATA)
- Asset Dividend Record (USER_DATA)
- Asset Detail (USER_DATA)
- Trade Fee (USER_DATA)
- Funding Wallet (USER_DATA)
- API Key Permission (USER_DATA)
- Get Assets That Can Be Converted Into BNB (USER_DATA)
- Spot Websocket Streams