Binance SDK (Python)

Python version Documentation Code Style License: MIT

🔗 Fork Notice: This repository is a significantly modified fork of the official binance-connector-python. It has been refactored to use modern Python packaging (pyproject.toml), updated tooling (uv, ruff), and enhanced development workflows.

sidan-binance-py is a lightweight, modern Python library for connecting to the Binance public API. It’s designed to be simple, clean, and easy to use with minimal dependencies.

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 sidan-binance-py
    
  • Using uv (fastest)

    uv add sidan-binance-py
    
  • From source

    git clone https://github.com/sidan-lab/sidan-binance-py.git
    cd sidan-binance-py
    uv sync
    

Usage

RESTful APIs

import logging
from binance.spot import Spot
from binance.lib.utils import config_logging

config_logging(logging, logging.DEBUG)

client = Spot()
logging.info(client.time())

client = Spot(api_key='<api_key>', api_secret='<api_secret>')

# Get account information
logging.info(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)
logging.info(response)

Please find examples folder to check for more endpoints.

Websocket

import logging
from binance.websocket.spot.websocket_api import SpotWebsocketAPIClient

def on_close(_):
    logging.info("Do custom stuff when connection is closed")

def message_handler(message):
    print(message)

ws_client = SpotWebsocketAPIClient(on_message=message_handler, on_close=on_close)

ws_client.ticker(
    symbol='bnbusdt',
    type="FULL",
)

# Combine selected streams
ws_client.ticker(
    symbols=["BNBBUSD", "BTCUSDT"],
    type="MINI",
    windowSize="2h",
)

ws_client.stop()

More websocket examples are available in the examples folder

Contents

Indices and tables