Skip to main content

Async Hyperliquid client using aiohttp

Project description

Async Hyperliquid

An asynchronous Python client for interacting with the Hyperliquid API using aiohttp.

Overview

This library provides an easy-to-use asynchronous interface for the Hyperliquid cryptocurrency exchange, supporting both mainnet and testnet environments. It handles API interactions, request signing, and data processing for both perpetual futures and spot trading.

Compared with the Hyperliquid Python SDK, Async Hyperliquid shows a significant performance improvement. The specific benchmark results are as follows.

benchmark

Features

  • Asynchronous API communication using aiohttp
  • Support for both mainnet and testnet environments
  • Message signing for authenticated endpoints
  • Trading operations for both perpetual futures and spot markets
  • Comprehensive type hints for better IDE integration

Installation

# Using pip
pip install async-hyperliquid

# Using Poetry
poetry add async-hyperliquid

# Using uv
uv add async-hyperliquid

Quick Start

import asyncio
import os
from async_hyperliquid.async_hyper import AsyncHyper

async def main():
    # Initialize the client
    address = os.getenv("HYPER_ADDRESS")
    api_key = os.getenv("HYPER_API_KEY")
    # Test on testnet
    client = AsyncHyper(address, api_key, is_mainnet=False)

    # Place a market order
    response = await client.place_order(
        coin="BTC",
        is_buy=True,
        sz=0.001,
        px=0,  # For market orders, price is ignored
        is_market=True
    )

    print(response)

    # Clean up
    await client.close()

if __name__ == "__main__":
    asyncio.run(main())

Or if you perfer context way:

import asyncio
import os
from async_hyperliquid.async_hyper import AsyncHyper

async def main():
    # Initialize the client
    address = os.getenv("HYPER_ADDRESS")
    api_key = os.getenv("HYPER_API_KEY")
    # Test on testnet
    async with AsyncHyper(address, api_key, is_mannet=False) as client:
        # place an market order open a BTC Long position
        resp = await client.place_order(coin="BTC", is_buy=True, sz=0.0001, px=0, is_market=True)
        print(resp)

if __name__ == "__main__":
    asyncio.run(main())

Place TP/SL orders

    coin = "BTC"
    is_buy = True
    sz = 0.001
    px = 105_000
    tp_px = px + 5_000
    sl_px = px - 5_000
    o1 = {
        "coin": coin,
        "is_buy": is_buy,
        "sz": sz,
        "px": px,
        "ro": False,
        "order_type": LimitOrder.ALO.value,
    }
    # Take profit
    tp_order_type = {
        "trigger": {"isMarket": False, "triggerPx": tp_px, "tpsl": "tp"}
    }
    o2 = {
        "coin": coin,
        "is_buy": not is_buy,
        "sz": sz,
        "px": px,
        "ro": True,
        "order_type": tp_order_type,
    }
    # Stop loss
    sl_order_type = {
        "trigger": {"isMarket": False, "triggerPx": sl_px, "tpsl": "sl"}
    }
    o3 = {
        "coin": coin,
        "is_buy": not is_buy,
        "sz": sz,
        "px": px,
        "ro": True,
        "order_type": sl_order_type,
    }

    # Place a market order to open position
    resp = await client.batch_place_orders([o1], is_market=True)
    print("\nBatch place market orders response: ", resp)
    assert resp["status"] == "ok"

    # Position TP/SL orders: position must be opened, otherwise it would failed
    orders = [o2, o3]
    resp = await client.batch_place_orders(orders, grouping="positionTpsl")
    print("Batch place orders with 'positionTpsl' response: ", resp)
    assert resp["status"] == "ok"

    # Close all positions
    resp = await client.close_all_positions()
    print("Close all positions response: ", resp)
    assert resp["status"] == "ok"

    # Normal TP/SL orders: main order and tp/sl must exists, each coin's normal
    # TP/SL orders can not batch with other coins', i.e. one coin one request.
    orders = [o1, o2, o3]
    resp = await client.batch_place_orders(orders, grouping="normalTpsl")
    print("Batch place orders with 'normalTpsl' response: ", resp)

    # Retrieve user opened orders
    orders = await client.get_user_open_orders(is_frontend=True)
    cancels = []
    for o in orders:
        coin = o["coin"]
        oid = o["oid"]
        cancels.append((coin, oid))
    resp = await client.batch_cancel_orders(cancels)
    print("Batch cancel orders response: ", resp)

For detailed usage, please check the test cases under test/ directory.

Environment Variables

Create a .env.local file with the following variables:

HYPER_ADDRESS=your_ethereum_address
HYPER_API_KEY=your_ethereum_private_key or api key generate hyperliquid website

Testing

Tests use pytest and pytest-asyncio. To run tests:

uv pip install -e .

# Run all tests
pytest

# Run with coverage
pytest --cov=async_hyperliquid

License

MIT

Acknowledgements

This library is a community-developed project and is not officially affiliated with Hyperliquid.

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

async_hyperliquid-0.4.8.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_hyperliquid-0.4.8-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file async_hyperliquid-0.4.8.tar.gz.

File metadata

  • Download URL: async_hyperliquid-0.4.8.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for async_hyperliquid-0.4.8.tar.gz
Algorithm Hash digest
SHA256 b0cdde82da429c427f65f6946119a9e500f1cc29e0848ae752b886ee96d4eaa1
MD5 a43d2bc970e540fe819de0dbe218b32c
BLAKE2b-256 be8e77c60097d451d17569415acb2ea6890d729c7ea3988073de434c45c855eb

See more details on using hashes here.

File details

Details for the file async_hyperliquid-0.4.8-py3-none-any.whl.

File metadata

  • Download URL: async_hyperliquid-0.4.8-py3-none-any.whl
  • Upload date:
  • Size: 31.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for async_hyperliquid-0.4.8-py3-none-any.whl
Algorithm Hash digest
SHA256 af5a665cb1a6f688e89a3631e779ee285ddbf09b244762e5e0ec47dec9849ddb
MD5 4b47fa245e74fe17c3d9ea79e3163771
BLAKE2b-256 c0fb125010b11a2f35fea40f7de31b0c1567f0fb9405b8f4ed3b87776a1b8577

See more details on using hashes here.

Supported by

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