Pybit

5.15.0 · active · verified Thu Apr 16

Pybit is the official Python3 connector for Bybit's HTTP and WebSocket APIs, offering a lightweight and efficient way to interact with the Bybit exchange. The library is actively developed, ensuring new Bybit API changes arrive quickly, and is currently at version 5.15.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `pybit` HTTP client for Bybit's unified trading API, authenticate using environment variables, and perform basic authenticated (get wallet balance) and unauthenticated (get tickers) requests. Remember to replace placeholder API keys or set them as environment variables.

import os
from pybit.unified_trading import HTTP

# It's recommended to store API keys in environment variables
api_key = os.environ.get('BYBIT_API_KEY', 'YOUR_API_KEY')
api_secret = os.environ.get('BYBIT_API_SECRET', 'YOUR_API_SECRET')

# Initialize an authenticated HTTP session for the unified trading API
# Set testnet=True for testing on Bybit's testnet
session = HTTP(
    testnet=True,  # Change to False for mainnet
    api_key=api_key,
    api_secret=api_secret,
)

try:
    # Example: Get wallet balance (requires authentication)
    response = session.get_wallet_balance(accountType="UNIFIED")
    print("Authentication successful. Total Equity:", response["result"]["list"][0]["totalEquity"])

    # Example: Get market tickers (does not require authentication)
    tickers = session.get_tickers(category="linear", symbol="BTCUSDT")
    print("BTCUSDT Mark Price:", tickers["result"]["list"][0]["markPrice"])

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure your API keys are correct and match the testnet/mainnet setting.")

view raw JSON →