Skip to main content

Command Palette

Search for a command to run...

Restful Binance Requests in Python with UNICORN Binance REST API

Updated
2 min read
Restful Binance Requests in Python with UNICORN Binance REST API
O
I build systems that work — technically sound, security-first, and actually useful to the people who depend on them. Creator of the UNICORN Binance Suite — 6 open source Python libraries with 2.8M+ PyPI downloads and 388+ dependent public projects. Currently pioneering AI-driven open source maintenance: running a controlled AI agent that maintains production code and documenting what it means for engineering teams. Vienna, Austria 🇦🇹

In this article I will show you an easy way to get started with the Binance REST API using Python.

We use the unicorn-binance-rest-api package:


Installation

Install via pip:

pip install unicorn-binance-rest-api

Or via conda:

conda install -c conda-forge unicorn-binance-rest-api

Alternatively download the latest release or clone the repository to run the examples locally:

git clone git@github.com:oliver-zehentleitner/unicorn-binance-rest-api.git

How to use BinanceRestApiManager?

Import BinanceRestApiManager and create an instance:

from unicorn_binance_rest_api.manager import BinanceRestApiManager

ubra = BinanceRestApiManager(exchange="binance.com")

Configure logging — use DEBUG instead of INFO for a very verbose mode:

import logging
import os

logging.getLogger("unicorn_binance_rest_api")
logging.basicConfig(level=logging.INFO,
                    filename=os.path.basename(__file__) + '.log',
                    format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}",
                    style="{")

Now you can execute various methods via the ubra instance:

# Get market depth
depth = ubra.get_order_book(symbol='BNBBTC')
print(f"depth: {depth}")

# Get all symbol prices
prices = ubra.get_all_tickers()
print(f"prices: {prices}")

# Get used weight
used_weight = ubra.get_used_weight()
print(f"weight: {used_weight}")

# Retrieve 30-minute klines for the last month of 2021
klines_30m = ubra.get_historical_klines("BTCUSDT", "30m", "1 Dec, 2021", "1 Jan, 2022")
print(f"klines_30m:\r\n{klines_30m}")

For private data, provide API credentials when creating the instance:

api_key = "aaa"
api_secret = "bbb"

ubra = BinanceRestApiManager(api_key=api_key,
                             api_secret=api_secret,
                             exchange="binance.com")

Get private data:

# Get account info
account = ubra.get_account()
print(f"account: {account}")

# Get all orders
all_orders = ubra.get_all_orders(symbol='BTCUSDT', limit=10)
print(f"all_orders: {all_orders}")

# Get open orders
open_orders = ubra.get_open_orders(symbol='BTCUSDT')
print(f"open_orders: {open_orders}")

A full overview of all available methods can be found in the documentation.


I hope you found this informative and useful.

Follow me on Binance Square, GitHub, X, and LinkedIn, or join Telegram for updates on my latest publications. Constructive feedback is always appreciated.

Thank you for reading, and happy coding! ¯\_(ツ)_/¯

UNICORN Binance Suite

Part 12 of 16

Open source Python libraries for automated trading on Binance — WebSocket streams, REST API, local order books, trailing stop loss, and Kubernetes-scale depth caching. Engineering decisions, real-world findings, and lessons from production.

Up next

How to Connect to binance.com REST API using Python via a SOCKS5 Proxy

US servers unfortunately can no longer connect to binance.com (geoblocking).

More from this blog

T

Technopathy

28 posts

DevSecOps · Python · AI-driven Development · Real-world findings from production systems.