Hyperliquid Python SDK

0.23.0 · active · verified Thu Apr 16

The `hyperliquid-python-sdk` provides a comprehensive SDK for interacting with the Hyperliquid decentralized exchange API in Python. It allows programmatic access to market data, order placement, and account management functionalities. Currently at version 0.23.0, the library maintains a rapid release cadence with frequent minor updates, introducing new features, improvements, and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Info` client to fetch public market data like mid prices for assets on Hyperliquid Mainnet. It also includes commented-out code showing how to set up the `Exchange` client for authenticated operations, which requires a private key from an environment variable (`HL_WALLET_PRIVATE_KEY`). Ensure Python 3.9+ is used.

import os
from hyperliquid.info import Info

# Configuration
# Mainnet API URL for public data
MAINNET_API_URL = "https://api.hyperliquid.xyz"

try:
    # Initialize Info client for public market data
    info_client = Info(MAINNET_API_URL)

    print(f"Fetching market data from Hyperliquid Mainnet: {MAINNET_API_URL}")

    # Get all mid prices
    mids = info_client.all_mids()
    print("\nMid prices for all assets:")
    # Print only a few for brevity
    for i, (asset, price) in enumerate(mids.items()):
        if i >= 5: # Limit to first 5 assets
            break
        print(f"  {asset}: {price}")
    if len(mids) > 5:
        print(f"  ... ({len(mids) - 5} more assets)")

    # Example of getting an asset's ID
    eth_asset_id = info_client.asset_to_id("ETH")
    print(f"\nETH Asset ID: {eth_asset_id}")

    # To use the Exchange client for authenticated operations (e.g., placing orders),
    # you would need a private key set in an environment variable:
    # PRIVATE_KEY = os.environ.get("HL_WALLET_PRIVATE_KEY", "")
    # if PRIVATE_KEY:
    #     from hyperliquid.utils import Wallet
    #     from hyperliquid.exchange import Exchange
    #     wallet = Wallet(PRIVATE_KEY)
    #     exchange_client = Exchange(wallet, MAINNET_API_URL)
    #     print(f"\nExchange client initialized for address: {wallet.address}")
    # else:
    #     print("\nSet HL_WALLET_PRIVATE_KEY environment variable to use Exchange client.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →