Restful Binance Requests in Python with UNICORN Binance REST API

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:
- PyPI: https://pypi.org/project/unicorn-binance-rest-api
- Documentation: https://oliver-zehentleitner.github.io/unicorn-binance-rest-api
- GitHub: https://github.com/oliver-zehentleitner/unicorn-binance-rest-api
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 tutorial informative and enjoyable! Follow me on GitHub and LinkedIn to stay updated on my latest releases. Your constructive feedback is always appreciated!
Image source: pixabay.com





