Python SDK for OKX
raw JSON → 0.4.1 verified Fri May 01 auth: no python
Python SDK for OKX exchange API. Version 0.4.1 supports REST and WebSocket APIs, including spot, futures, and options trading. Active development.
pip install python-okx Common errors
error AttributeError: module 'okx' has no attribute 'OkxClient' ↓
cause Importing from wrong submodule or old version.
fix
Ensure you use
from okx import OkxClient and have installed version 0.4.x. error okx.exceptions.OkxAPIException: (50001) OKX API key does not exist ↓
cause API key, secret, or passphrase is incorrect or missing.
fix
Check that environment variables OKX_API_KEY, OKX_SECRET_KEY, OKX_PASSPHRASE are set correctly and the key is active on OKX.
Warnings
breaking In v0.4.x, the constructor parameter 'api_secret_key' replaces the old 'apiSecretKey'. Also, 'flag' parameter default changed from '0' (simulated) to '' (empty), requiring explicit flag for live trading. ↓
fix Use `api_secret_key=...` and set `flag='1'` for live trading, `flag='0'` for demo.
gotcha The SDK does NOT handle authentication for WebSocket streams automatically; you must manually authenticate via login endpoint before subscribing to private channels. ↓
fix Call `ws.login(api_key, passphrase, secret_key, timestamp, sign)` on the WebSocket object before subscribing to private channels.
deprecated The old import path `from okx.client import OkxClient` is deprecated in v0.4.0. It still works but will be removed in future. ↓
fix Use `from okx import OkxClient`.
Imports
- OkxClient wrong
from okx.client import OkxClientcorrectfrom okx import OkxClient - SpotAPI wrong
from okx.SpotAPI import SpotAPIcorrectfrom okx.spot import SpotAPI
Quickstart
import os
from okx import OkxClient
api_key = os.environ.get('OKX_API_KEY', '')
secret_key = os.environ.get('OKX_SECRET_KEY', '')
passphrase = os.environ.get('OKX_PASSPHRASE', '')
client = OkxClient(api_key=api_key, api_secret_key=secret_key, passphrase=passphrase, use_server_time=False, flag='1')
print(client.get_ticker('BTC-USDT'))