
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socketโs new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
AioPyBit is a modern and convenient Python client for the ByBit cryptocurrency exchange API (v5). The module provides real-time access to market data and private account information via efficient WebSocket connections, and also supports HTTP requests with advanced error handling and auto-retry mechanisms.
websockets
>= 10.0aiohttp
>= 3.8.0asyncio
(built-in)pip install aiopybit
# The module is ready to use - no separate installation needed
ByBitWebSocketManager
: High-level manager for multiple WebSocket connectionsByBitWebSocketClient
: Low-level WebSocket client with connection managementByBitPublicStreamsMixin
: Methods for public market data streamsByBitPrivateStreamsMixin
: Methods for private account data streamsprotocols.py
import asyncio
from aiopybit import ByBitClient
client = ByBitClient(API_KEY, API_SECRET, MODE)
async def handle_ticker(data):
ticker = data.get('data', {})
symbol = ticker.get('symbol', 'N/A')
price = ticker.get('lastPrice', 'N/A')
print(f'๐ {symbol}: ${price}')
async def main():
await client.ws.subscribe_to_ticker(
category='spot', symbol='BTCUSDT', on_message=handle_ticker
)
try:
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
pass
await client.close()
if __name__ == '__main__':
asyncio.run(main())
The examples/
directory contains comprehensive usage examples:
For private streams, you need ByBit API credentials:
Environment | Description | WebSocket URLs |
---|---|---|
mainnet | Production environment | wss://stream.bybit.com/v5/ |
testnet | Testing environment | wss://stream-testnet.bybit.com/v5/ |
demo | Demo environment (limited features) | wss://stream-demo.bybit.com/v5/ |
Category | Description | Supported Streams |
---|---|---|
linear | USDT/USDC perpetual contracts | All public streams |
spot | Spot trading pairs | Tickers, orderbook, trades |
option | Options contracts | All public + greeks |
The client includes robust connection management features:
High-level WebSocket manager for multiple connections and subscriptions.
mode
: Environment ('mainnet', 'testnet', 'demo')api_key
: ByBit API key (required for private streams)api_secret
: ByBit API secret (required for private streams)ping_interval
: Ping interval in seconds (default: 20)ping_timeout
: Ping timeout in seconds (default: 10)auto_reconnect
: Enable auto-reconnection (default: True)get_websocket(channel_type)
: Get or create WebSocket for channelclose_all()
: Close all WebSocket connectionssubscribe_to_ticker(category, symbol, on_message)
subscribe_to_orderbook(category, symbol, on_message, depth)
subscribe_to_public_trades(category, symbol, on_message)
subscribe_to_kline(category, symbol, interval, on_message)
subscribe_to_liquidations(category, symbol, on_message)
subscribe_to_order(on_message)
subscribe_to_execution(on_message)
subscribe_to_position(on_message)
subscribe_to_wallet(on_message)
subscribe_to_greeks(on_message)
Low-level WebSocket client class with connection management.
url
: WebSocket URLapi_key
: ByBit API key (optional for public streams)api_secret
: ByBit API secret (optional for public streams)ping_interval
: Ping interval in seconds (default: 20)ping_timeout
: Ping timeout in seconds (default: 10)auto_reconnect
: Enable auto-reconnection (default: True)Enable logging to monitor connection status and debug issues:
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s'
)
# Set specific logger levels
logging.getLogger('aiopybit').setLevel(logging.INFO)
# For detailed debugging
logging.getLogger('aiopybit').setLevel(logging.DEBUG)
Log levels:
DEBUG
: Detailed connection and message informationINFO
: Connection status and important eventsWARNING
: Connection issues and recoverable errorsERROR
: Critical errors and failuresExample log output:
2024-01-15 10:30:45 [INFO] aiopybit: WebSocket connection for wss://stream.bybit.com/v5/public/linear established
2024-01-15 10:30:45 [INFO] aiopybit: โ
Subscribed to tickers.BTCUSDT
2024-01-15 10:30:46 [DEBUG] aiopybit: Sending ping for wss://stream.bybit.com/v5/public/linear
2024-01-15 10:31:05 [INFO] aiopybit: ๐ BTCUSDT: $45,123.45
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
This software is for educational and development purposes. Use at your own risk. The authors are not responsible for any financial losses incurred through the use of this software.
Happy Trading! ๐
FAQs
Asynchronous Python client for the ByBit API
We found that aiopybit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.ย It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socketโs new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.