Skip to main content


Crypto WS API connector for ASYNC requests

Full coverage of all methods provided by the interface

Provides of connection management, keepalive and rate limits control


PyPI version DeepSource DeepSource sonarcloud Downloads


For ✔️Binance,


Features

Lightweight and efficient solution to utilize of all available methods provided through the:

Session management layer for:

  • Credentials
  • Connection
  • Keepalive
  • Error handling
  • Limits control
  • Methods construction
    • Generating session request id by default
    • Creating request on-the-fly from method name and params: {}
    • Generating signature if necessary
    • Response handling
  • logging

User interface layer

  • Start session instance
  • Getting session operational status
  • Send async request
  • Get response or raised exception
  • Stop session instance

Get started

Install use PIP

pip install crypto_ws_api

For upgrade to latest versions use:

pip install -U crypto_ws_api

After first install create environment by run

crypto_ws_api_init

in terminal window.

The config directory will be created. You get path to config:

ubuntu@ubuntu:~$ crypto_ws_api_init

Can't find config file! Creating it...

Before first run set account(s) API key into /home/ubuntu/.config/crypto_ws_api/ws_api.toml

Prepare exchange account

  • For test purpose log in at Binance Spot Test Network
  • Create API Key
  • After install and create environment specify api_key and api_secret to the config file

Start demo

  • Run in terminal window
crypto_ws_api_demo

Useful tips

crypto_ws_api/demo.py - complete and fully functional example

Get credentials and create user session

from crypto_ws_api.ws_session import get_credentials, UserWSSession

# Can be omitted if you have credentials
_exchange, _test_net, api_key, api_secret, ws_api_endpoint = get_credentials(account_name)

session = aiohttp.ClientSession()

user_session = UserWSSession(
    api_key,
    api_secret,
    session=session,
    endpoint=ws_api_endpoint
)

await user_session.start()
print(f"Operational status: {user_session.operational_status}")

Demo method's calling

await account_information(user_session)

Stop user session and close aiohttp session

await user_session.stop()
await session.close()
print(f"Operational status: {user_session.operational_status}")

Method call example

async def account_information(user_session: UserWSSession):
    # https://developers.binance.com/docs/binance-trading-api/websocket_api#account-information-user_data
    try:
        res = await user_session.handle_request(
            "account.status",
            api_key=True,
            signed=True
        )
        if res is None:
            print("Here handling state Out-of-Service")
    except asyncio.CancelledError:
        pass  # Task cancellation should not be logged as an error
    except Exception as _ex:
        print(f"Handling exception: {_ex}")
    else:
        print(f"Account information (USER_DATA) response: {res}")

Logging setup

For configure logging in multi-module project use next snippet for yours main():

import logging.handlers

logger = logging.getLogger(__name__)
formatter = logging.Formatter(fmt="[%(asctime)s: %(levelname)s] %(message)s")
#
sh = logging.StreamHandler()
sh.setFormatter(formatter)
sh.setLevel(logging.DEBUG)
#
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(sh)

Limits control 🔗

Upon reaching the limit threshold of each type, the session switches to the Out-of-Service state. Monitor the values of the variables user_session.operational_status and user_session.order_handling

If you send a request in this state, the answer will be None

In any case, you are protected from exceeding limits and blocking for this reason

Donate

BNB, BUSD, USDT (BEP20) 0x5b52c6ba862b11318616ee6cef64388618318b92

USDT (TRC20) TP1Y43dpY7rrRyTSLaSKDZmFirqvRcpopC

Metadata

Release files for crypto-ws-api 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for crypto-ws-api 1.0.1
File Size Uploaded
crypto-ws-api-1.0.1.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for crypto-ws-api 1.0.1
File Interpreter ABI Platform
crypto_ws_api-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / crypto-ws-api-1.0.1.tar.gz

Download URL crypto-ws-api-1.0.1.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e790e0a6115cad9ef97f8e26e87fccc95a89b952479edd6a2a3f7eed61b3d9a6
BLAKE2b-256 checksum
How to use checksums
2a85b9c766d477532ed27e147a9f6d40861a3739477ce9501ee36ae1532d96d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release files / crypto_ws_api-1.0.1-py3-none-any.whl

Download URL crypto_ws_api-1.0.1-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ada5c9325101b73303f2a65080fba1bfcdad327a8b93e2d4576492748e695863
BLAKE2b-256 checksum
How to use checksums
8b7f4aa0d99e4f45e7fbfd964bfb6fa8d04df87407531f91591e753fec30da55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release history Release notifications | RSS feed

2.1.14

2 release files

2.1.13

2 release files

2.1.12

2 release files

2.1.11

2 release files

2.1.9

2 release files

2.1.8

2 release files

2.1.7

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.0

2 release files

2.0.20

2 release files

2.0.19

2 release files

2.0.18

2 release files

2.0.17

2 release files

2.0.16

2 release files

2.0.15

2 release files

2.0.14

2 release files

2.0.13

2 release files

2.0.12

2 release files

2.0.11

2 release files

2.0.10

2 release files

2.0.9

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page