Python Wrapper for OKX v5 API with Async Support

·

Overview

python-okx-async is an unofficial Python wrapper for the OKX exchange v5 API, offering asynchronous support for REST requests and websocket streams. Built as an extension of the python-okx package, it enhances functionality with non-blocking operations, ideal for high-frequency trading and scalable applications.


Installation

Install the package via pip:

pip install python-okx-async

Getting Started

Step 1: Register an OKX Account

Step 2: Generate API Credentials

  1. Log in to your OKX account.
  2. Navigate to API keys in the user menu.
  3. Click + Create V5 API key and follow prompts to generate:

    • API Key
    • Passphrase
    • Secret Key

Step 3: Secure API Credentials

Store credentials in a protected .env file:

touch ~/.env
chmod 600 ~/.env

Add credentials to .env (without quotes):

OKX_API_KEY=your_key_here
OKX_API_PASSPHRASE=your_passphrase_here
OKX_API_SECRET=your_secret_here

👉 Best practices for API security include avoiding hardcoding credentials in source files or CLI arguments.


Example Usage

Initialize the AsyncTradeAPI Class

import os
from dotenv import load_dotenv
from okx_async.AsyncTrade import AsyncTradeAPI

load_dotenv()
tradeAPI = AsyncTradeAPI(
    os.getenv("OKX_API_KEY"),
    os.getenv("OKX_API_SECRET"),
    os.getenv("OKX_API_PASSPHRASE")
)

Fetch Order Book Data

Run the included example_order_book.py script to retrieve the XCH-USDT spot market order book (20 levels depth):

# Example snippet (full script in repository)
print(await tradeAPI.get_order_book("XCH-USDT", depth=20))

Key Features


FAQs

Q1: Is python-okx-async officially supported by OKX?

No. This is a community-maintained wrapper extending the official python-okx package.

Q2: How do I debug API requests?

Set debug=True during class initialization to log requests/responses.

Q3: Where can I find more examples?

Refer to the python-okx examples for synchronous use cases.

👉 Explore advanced trading strategies with async-capable endpoints.


Best Practices


Conclusion

This wrapper simplifies async interactions with OKX’s API, enabling scalable trading applications. For detailed documentation, consult the OKX API Reference.