Binance Connector

3.12.0 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize the `Spot` client and make basic calls to public endpoints like getting the server time and exchange information. For Futures API, replace `Spot` with `Futures`. For endpoints requiring authentication, ensure `BINANCE_API_KEY` and `BINANCE_API_SECRET` environment variables are set.

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.")

view raw JSON →