The signature algorithm:
signature = Base64(HMAC_SHA256(secret_key, timestamp + method + request_path + body))
Java Example:
```java
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public static String sign(String timestamp, String method,
String requestPath, String body, String secretKey) {
try {
String preHash = timestamp + method + requestPath + body;
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec spec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
mac.init(spec);
return Base64.getEncoder().encodeToString(mac.doFinal(preHash.getBytes()));
} catch (Exception e) {
throw new RuntimeException("Signature failed", e);
}
}
```
Python Example:
```python
import hmac, base64, hashlib
def sign(timestamp, method, request_path, body, secret_key):
pre_hash = timestamp + method + request_path + body
signature = hmac.new(
secret_key.encode(), pre_hash.encode(), hashlib.sha256
).digest()
return base64.b64encode(signature).decode()
```
Go Example:
```go
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
)
func sign(timestamp, method, requestPath, body, secretKey string) string {
preHash := timestamp + method + requestPath + body
mac := hmac.New(sha256.New, []byte(secretKey))
mac.Write([]byte(preHash))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
```
All authenticated REST requests MUST include:
| Header | Description |
|---|---|
| OK-ACCESS-KEY | API key |
| OK-ACCESS-SIGN | Base64-encoded HMAC SHA256 signature |
| OK-ACCESS-TIMESTAMP | ISO 8601 timestamp (e.g., 2023-01-01T00:00:00.000Z) |
| OK-ACCESS-PASSPHRASE | API passphrase |
| x-simulated-trading | Set to "1" for demo trading (optional) |
| Type | Description |
|---|---|
| SPOT | Spot trading |
| SWAP | Perpetual swap |
| FUTURES | Futures contracts |
| OPTION | Options trading |
wss://ws.okx.com:8443/ws/v5/public
Access market data without authentication: tickers, order books, trades, mark prices, funding rates, etc.
wss://ws.okx.com:8443/ws/v5/private
Requires authentication (login). Access account data, order updates, position changes, balance updates.
wss://ws.okx.com:8443/ws/v5/business
Requires authentication. Access business-specific channels (requires application).
{"op": "ping"}
{"op": "pong"}
{
"op": "subscribe",
"args": [
{
"channel": "tickers",
"instId": "BTC-USDT"
}
]
}
{
"op": "login",
"args": [
{
"apiKey": "YOUR_API_KEY",
"passphrase": "YOUR_PASSPHRASE",
"timestamp": "2023-01-01T00:00:00.000Z",
"sign": "BASE64_SIGNATURE"
}
]
}
WebSocket login signature: timestamp + "GET" + "/users/self/verify" (no body).
| Method | Path | Description |
|---|---|---|
| GET | /api/v5/account/balance | Get account balance |
| GET | /api/v5/account/positions | Get positions |
| GET | /api/v5/account/positions-history | Get positions history |
| GET | /api/v5/account/bills | Get bills (transaction history) |
| GET | /api/v5/account/config | Get account configuration |
| GET | /api/v5/account/instruments | Get instruments |
| POST | /api/v5/account/set-position-mode | Set position mode |
| GET | /api/v5/account/leverage-info | Get leverage |
| POST | /api/v5/account/set-leverage | Set leverage |
| Method | Path | Description |
|---|---|---|
| POST | /api/v5/trade/order | Place order |
| POST | /api/v5/trade/amend-order | Amend order |
| POST | /api/v5/trade/cancel-order | Cancel order |
| POST | /api/v5/trade/batch-orders | Place batch orders |
| POST | /api/v5/trade/batch-amend-orders | Amend batch orders |
| POST | /api/v5/trade/batch-cancel-orders | Cancel batch orders |
| POST | /api/v5/trade/close-position | Close position |
| GET | /api/v5/trade/order | Get order details |
| GET | /api/v5/trade/orders-pending | Get pending orders |
| GET | /api/v5/trade/orders-history | Get order history |
| GET | /api/v5/trade/fills | Get transaction details |
| GET | /api/v5/trade/fills-history | Get transaction history |
| Method | Path | Description |
|---|---|---|
| GET | /api/v5/market/tickers | Get all tickers |
| GET | /api/v5/market/ticker | Get single ticker |
| GET | /api/v5/market/books | Get order book |
| GET | /api/v5/market/candles | Get candlestick (K-line) |
| GET | /api/v5/market/history-candles | Get historical candles |
| GET | /api/v5/market/trades | Get recent trades |
| GET | /api/v5/market/history-trades | Get historical trades |
| GET | /api/v5/market/mark-price | Get mark price |
| GET | /api/v5/market/funding-rate | Get funding rate |
POST /api/v5/trade/order
Request Body:json { "instId": "BTC-USDT-SWAP", "tdMode": "cross", "side": "buy", "ordType": "limit", "sz": "1", "px": "20000" }
Response:json { "code": "0", "msg": "", "data": [ { "clOrdId": "", "ordId": "123456", "tag": "", "sCode": "0", "sMsg": "" } ] }
| Code | Meaning |
|---|---|
| 0 | Success |
| 50001 | Request timeout |
| 50002 | Service unavailable |
| 50004 | Rate limit exceeded |
| 50011 | API key does not exist |
| 50012 | API key has expired |
| 50013 | Invalid sign |
| 50014 | Invalid passphrase |
| 50015 | Invalid IP |
| 50016 | Permission denied |
| 50017 | Withdrawal address not whitelisted |
| 50100 | Unsupported operation |
| 51000 | Parameter error |
| 51001 | Instrument ID does not exist |
| 51006 | Order does not exist |
| 51400 | Order cancellation failed |
| 51500 | Insufficient balance |
clOrdId to prevent duplicate orders