Binance Public API Connector Python

PyPI version Python version Documentation Code Style License: MIT

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.

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

Indices and tables