Binance Connector
This is the lightweight, official Python library for connecting to Binance's public API. It provides interfaces for Spot, Futures, and other market data and trading endpoints. Currently at version 3.12.0, it receives active development and frequent updates to align with Binance API changes.
Common errors
-
ModuleNotFoundError: No module named 'binance.client'
cause You are likely attempting to use an import path belonging to the `python-binance` library, not `binance-connector`.fixAfter installing `binance-connector` (`pip install binance-connector`), update your client import to `from binance.spot import Spot as Client` for Spot trading or `from binance.futures import Futures as Client` for Futures. -
binance.exceptions.APIError: APIError(code=-1002): Invalid API-key, IP, or permissions for action.
cause The provided API key or secret is incorrect, your IP address is not whitelisted, or the API key lacks necessary permissions for the requested operation.fixDouble-check your `BINANCE_API_KEY` and `BINANCE_API_SECRET` environment variables. If applicable, ensure your IP address is whitelisted in Binance API settings, and verify that the API key has the required permissions enabled on Binance's website. -
binance.exceptions.APIError: APIError(code=-1003): Too many requests. Please try again later.
cause Your application has exceeded Binance's API rate limits for a given time period.fixReduce the frequency of your API calls. Implement delays or a backoff strategy between requests to stay within the limits. Review Binance's official API documentation for specific endpoint rate limits.
Warnings
- breaking Module structure change in 3.x: The client import path for `binance-connector` changed in version 3.x to differentiate between Spot and Futures clients.
- gotcha Confusion with `python-binance`: `binance-connector` is the *official* library provided by Binance, distinct from the popular community-maintained `python-binance`. Their APIs and import paths are incompatible.
- gotcha API Rate Limiting: Binance API has strict rate limits. Exceeding them frequently will result in `APIError(code=-1003)` and potentially temporary IP bans.
- gotcha API Key Permissions: Even with correct API keys, certain operations may fail with `APIError(code=-1002)` if the key lacks the necessary permissions (e.g., 'Enable Spot & Margin Trading', 'Enable Withdrawals').
Install
-
pip install binance-connector
Imports
- Spot
from binance.client import Client
from binance.spot import Spot as Client
- Futures
import binance.client
from binance.futures import Futures as Client
Quickstart
import os
from binance.spot import Spot as Client # For Spot API. Use 'from binance.futures import Futures as Client' for Futures.
api_key = os.environ.get('BINANCE_API_KEY', '')
api_secret = os.environ.get('BINANCE_API_SECRET', '')
# Initialize the client (API keys are optional for public endpoints like server time)
client = Client(api_key=api_key, api_secret=api_secret)
try:
# Get server time (public endpoint)
response = client.time()
print(f"Binance Server Time: {response['serverTime']}")
# Get exchange information (public endpoint)
exchange_info = client.exchange_info()
print(f"Number of symbols listed: {len(exchange_info['symbols'])}")
# Example of a signed endpoint (requires API key permissions)
# account_info = client.account()
# print(f"Account balance: {account_info['balances'][0]['free']}")
except Exception as e:
print(f"An error occurred: {e}")
print("Make sure your API keys (BINANCE_API_KEY, BINANCE_API_SECRET) are correctly set in your environment if required for the endpoint, and check network connectivity.")