OKX API Overview
OKX provides a comprehensive REST API and WebSocket API for trading automation. With it you can fetch market data, manage orders, check account balances, and stream real-time data — all programmatically.
Getting Your API Keys
Log in to OKX → Profile → API → Create V5 API key. Set permissions: Read + Trade (never Withdraw). Note down: API Key, Secret Key, and Passphrase (OKX requires all three for authenticated requests).
REST API Basics
- Base URL:
https://www.okx.com - Market data (no auth):
GET /api/v5/market/tickers?instType=SPOT - Account balance (auth required):
GET /api/v5/account/balance - Place order:
POST /api/v5/trade/order
Authentication
Every private request needs four headers: OK-ACCESS-KEY, OK-ACCESS-SIGN (HMAC-SHA256 signature), OK-ACCESS-TIMESTAMP, and OK-ACCESS-PASSPHRASE. Use the ccxt library to handle this automatically.
WebSocket API
Connect to wss://ws.okx.com:8443/ws/v5/public for public data. Subscribe to ticker, order book, or trades channels. Private channel at /ws/v5/private requires login with your credentials.
Using ccxt with OKX
import ccxt
exchange = ccxt.okx({
'apiKey': 'YOUR_KEY',
'secret': 'YOUR_SECRET',
'password': 'YOUR_PASSPHRASE',
})
balance = exchange.fetch_balance()
order = exchange.create_market_buy_order('BTC/USDT', 0.001)