Skip to main content

This is a lightweight Async library that works as a connector to Binance public API and WebSocket.

Project description

aio-binance-library

Async library for connecting to the Binance API on Python

Python 3.7 License: MIT Aiohttp: 3.8.1 Loguru: 0.5.3 Ujson: 0.5.3 Docstrings: Google

This is a lightweight library that works as a connector to Binance Futures public API

  • Supported APIs:
    • USDT-M Futures `/fapi/*``
    • Futures/Delivery Websocket Market Stream
    • Futures/Delivery User Data Stream
  • Inclusion of examples
  • Response metadata can be displayed

Installation

pip install aio-binance-library

Getting started

REST API

Usage examples:

import asyncio
from aio_binance.futures.usdt import Client 

async def main():
    client = Client()
    res = await client.get_public_time()
    print(res)

    client = Client(key='<api_key>', secret='<api_secret>')

    # Get account information
    res = await client.get_private_account_info()
    print(res)

    # Post a new order
    params = {
        'symbol': 'BTCUSDT',
        'side': 'SELL',
        'type_order': 'LIMIT',
        'time_in_force': 'GTC',
        'quantity': 0.002,
        'price': 59808
    }

    res = await client.create_private_order(**params)
    print(res)

asyncio.run(main())

Please find examples folder to check for more endpoints.

Notes

The methods you need, adheres to a hierarchy

<method>_<availability>_<method_name>

create_private_order()
or
get_public_time()

Methods:

create, get, delete, change, update

Availability:

private - methods where key_api and secret_api are required

public - you can get information without a key

Testnet

You can choose testnet

from aio_binance.futures.usdt import Client

client= Client(testnet=True)

Optional parameters

Parameters can be passed in different formats as in Binance api documents or PEP8 suggests lowercase with words separated by underscores

# Binance api
response = await client.get_private_open_order('BTCUSDT', orderListId=1)

# PEP8
response = await client.get_private_open_order('BTCUSDT', order_list_id=1)

Timeout

timeout is available to be assigned with the number of seconds you find most appropriate to wait for a server response.
Please remember the value as it won't be shown in error message no bytes have been received on the underlying socket for timeout seconds.
By default, timeout=5

from aio_binance.futures.usdt import Client

client= Client(timeout=1)

Response Metadata

The Binance API server provides weight usages in the headers of each response. You can display them by initializing the client with show_limit_usage=True:

from aio_binance.futures.usdt import Client

client = Client(show_limit_usage=True)
res = await client.time()
print(res)

returns:

{'data': {'serverTime': 1647990837551}, 'limit_usage': 40}

You can also display full response metadata to help in debugging:

client = Client(show_header=True)
res = await client.time()
print(res)

returns:

{'data': {'serverTime': 1587990847650}, 'header': {'Context-Type': 'application/json;charset=utf-8', ...}}

User agent

client = Client(agent='name_app')

You can pass the name of your application.

Websocket

This is an example of connecting to multiple streams

import asyncio

from aio_binance.futures.usdt import WsClient


async def callback_event(data: dict):
    print(data)


async def main():

    ws = WsClient()
    stream = [
        ws.stream_liquidation_order(),
        ws.stream_book_ticker(),
        ws.stream_ticker('BTCUSDT')
    ]
    res = await asyncio.gather(*stream)
    await ws.subscription_streams(res, callback_event)

asyncio.run(main())

More websocket examples are available in the examples folder

Note

Stream methods start with the word stream Example: stream_<name_method>

Subscribing to multiple streams: subscription_streams()

Heartbeat

Once connected, the websocket server sends a ping frame every 3 minutes and requires a response pong frame back within a 5 minutes period. This package handles the pong responses automatically.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aio-binance-library-2.0.5.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

aio_binance_library-2.0.5-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file aio-binance-library-2.0.5.tar.gz.

File metadata

  • Download URL: aio-binance-library-2.0.5.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for aio-binance-library-2.0.5.tar.gz
Algorithm Hash digest
SHA256 81a8bdd860f5473ab29b4cf7f4055428d903d3ae23cd24220cea411131c314c6
MD5 6f6993b38d20798e45bbf557eb3c03ab
BLAKE2b-256 4063ad12351aa314c7a62f0f8d3988db21c7a59f7e14068d093cac3afe8fd9f4

See more details on using hashes here.

Provenance

File details

Details for the file aio_binance_library-2.0.5-py3-none-any.whl.

File metadata

  • Download URL: aio_binance_library-2.0.5-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for aio_binance_library-2.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2fbdebb9ba799e937c30493a233422c1c85914889dcda01449fc63798c0dabd3
MD5 fad8d99fce5b6a8e0a02f8402d89dd1d
BLAKE2b-256 d20520c7d730964fb63593bd0772f4ab48cb8228a72f4e0b3653c64f2d168bc1

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page