-
Notifications
You must be signed in to change notification settings - Fork 0
REST API
Igor Sazonov edited this page Mar 15, 2026
·
1 revision
A complete guide to working with the OKX REST API through the PHP SDK.
The SDK covers all 335 endpoints of the OKX v5 API, organized into 16 categories.
All methods are accessed through factory methods on the client:
$client->account() // Account (53 methods)
$client->trade() // Trading (32 methods)
$client->market() // Market data (24 methods)
$client->asset() // Assets (26 methods)
$client->publicData() // Public data (24 methods)
// ... and more// Full balance
$balance = $client->account()->getBalance();
// Specific currency
$btcBalance = $client->account()->getBalance('BTC');// Get configuration
$config = $client->account()->getConfig();
// Set position mode
$client->account()->setPositionMode('long_short_mode');
// Set leverage
$client->account()->setLeverage(
lever: '10',
mgnMode: 'cross',
instId: 'BTC-USDT-SWAP'
);// All positions
$positions = $client->account()->getPositions();
// Positions for a specific instrument
$btcPositions = $client->account()->getPositions(
instType: 'SWAP',
instId: 'BTC-USDT-SWAP'
);
// Position history
$history = $client->account()->getPositionsHistory(
instType: 'SWAP',
limit: 100
);// Add margin
$client->account()->increaseDecreaseMargin(
instId: 'BTC-USDT-SWAP',
posSide: 'long',
type: 'add',
amt: '100'
);
// Reduce margin
$client->account()->increaseDecreaseMargin(
instId: 'BTC-USDT-SWAP',
posSide: 'long',
type: 'reduce',
amt: '50'
);$fees = $client->account()->getTradeFee(
instType: 'SPOT',
instId: 'BTC-USDT'
);// Market order
$order = $client->trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'market',
sz: '100',
tgtCcy: 'quote_ccy'
);
// Limit order
$order = $client->trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'limit',
sz: '0.01',
px: '50000'
);
// Order with TP/SL
$order = $client->trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cross',
side: 'buy',
ordType: 'limit',
sz: '0.1',
px: '50000',
tpTriggerPx: '55000',
tpOrdPx: '-1',
slTriggerPx: '48000',
slOrdPx: '-1'
);// Batch place
$orders = $client->trade()->batchOrders([
[
'instId' => 'BTC-USDT',
'tdMode' => 'cash',
'side' => 'buy',
'ordType' => 'limit',
'sz' => '0.01',
'px' => '50000'
],
[
'instId' => 'ETH-USDT',
'tdMode' => 'cash',
'side' => 'buy',
'ordType' => 'limit',
'sz' => '0.1',
'px' => '3000'
]
]);
// Batch cancel
$result = $client->trade()->cancelBatchOrders([
['instId' => 'BTC-USDT', 'ordId' => '123'],
['instId' => 'ETH-USDT', 'ordId' => '456']
]);// Get a specific order
$order = $client->trade()->getOrder(
instId: 'BTC-USDT',
ordId: '123456789'
);
// Open orders
$pending = $client->trade()->getOrdersPending(
instType: 'SPOT'
);
// Order history
$history = $client->trade()->getOrdersHistory(
instType: 'SPOT',
limit: 100
);
// Cancel an order
$result = $client->trade()->cancelOrder(
instId: 'BTC-USDT',
ordId: '123456789'
);
// Amend an order
$result = $client->trade()->amendOrder(
instId: 'BTC-USDT',
ordId: '123456789',
newSz: '0.02',
newPx: '51000'
);// Place an algo order
$algoOrder = $client->trade()->placeAlgoOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'trigger',
sz: '0.01',
triggerPx: '49000',
orderPx: '49000'
);
// Get pending algo orders
$algoOrders = $client->trade()->getOrdersAlgoPending(
algoOrdType: 'trigger'
);
// Cancel algo orders
$result = $client->trade()->cancelAlgos([
['algoId' => '123456']
]);// Recent fills
$fills = $client->trade()->getFills(
instType: 'SPOT',
limit: 100
);
// Fill history
$fillsHistory = $client->trade()->getFillsHistory(
instType: 'SPOT',
begin: '1609459200000',
end: '1640995200000'
);// Single instrument
$ticker = $client->market()->getTicker('BTC-USDT');
// All instruments of a type
$tickers = $client->market()->getTickers(
instType: 'SPOT'
);// Full order book
$orderBook = $client->market()->getBooks(
instId: 'BTC-USDT',
sz: '20'
);
// Top 5 levels
$books5 = $client->market()->getBooksLite('BTC-USDT');// Current candles
$candles = $client->market()->getCandles(
instId: 'BTC-USDT',
bar: '1H',
limit: 100
);
// Historical candles
$historyCandles = $client->market()->getHistoryCandles(
instId: 'BTC-USDT',
bar: '1D',
after: '1609459200000',
before: '1640995200000'
);// Recent trades
$trades = $client->market()->getTrades(
instId: 'BTC-USDT',
limit: 100
);
// Trade history
$historyTrades = $client->market()->getHistoryTrades(
instId: 'BTC-USDT',
limit: 100
);// Index tickers
$indexTickers = $client->market()->getIndexTickers(
quoteCcy: 'USDT'
);
// Index components
$components = $client->market()->getIndexComponents(
index: 'BTC-USDT'
);// All balances
$balances = $client->asset()->getBalances();
// Specific currency
$btcBalance = $client->asset()->getBalances('BTC');// Transfer between accounts
$transfer = $client->asset()->transfer(
ccy: 'USDT',
amt: '100',
from: '6', // Funding account
to: '18', // Trading account
type: '0'
);
// Transfer to sub-account
$subTransfer = $client->asset()->subaccountTransfer(
ccy: 'USDT',
amt: '100',
from: '6',
to: '6',
subAcct: 'sub-account-name'
);// Deposit address
$address = $client->asset()->getDepositAddress('USDT');
// Deposit history
$deposits = $client->asset()->getDepositHistory(
ccy: 'USDT',
limit: 100
);// Withdraw funds
$withdrawal = $client->asset()->withdrawal(
ccy: 'USDT',
amt: '100',
dest: '4',
toAddr: 'your-wallet-address',
fee: '1',
chain: 'USDT-TRC20'
);
// Withdrawal history
$withdrawals = $client->asset()->getWithdrawalHistory(
ccy: 'USDT',
limit: 100
);
// Cancel a withdrawal
$cancel = $client->asset()->cancelWithdrawal('withdrawal-id');// Get a conversion quote
$quote = $client->asset()->convertEstimateQuote(
baseCcy: 'BTC',
quoteCcy: 'USDT',
side: 'sell',
rfqSz: '0.1',
rfqSzCcy: 'BTC'
);
// Execute the conversion
$trade = $client->asset()->convertTrade(
quoteId: $quote[0]['quoteId'],
baseCcy: 'BTC',
quoteCcy: 'USDT',
side: 'sell',
sz: '0.1',
szCcy: 'BTC'
);// All instruments
$instruments = $client->publicData()->getInstruments(
instType: 'SPOT'
);
// Specific instrument
$instrument = $client->publicData()->getInstruments(
instType: 'SPOT',
instId: 'BTC-USDT'
);// Current funding rate
$fundingRate = $client->publicData()->getFundingRate('BTC-USDT-SWAP');
// Funding rate history
$history = $client->publicData()->getFundingRateHistory(
instId: 'BTC-USDT-SWAP',
limit: 100
);$openInterest = $client->publicData()->getOpenInterest(
instType: 'SWAP',
uly: 'BTC-USDT'
);$tiers = $client->publicData()->getPositionTiers(
instType: 'SWAP',
tdMode: 'cross',
uly: 'BTC-USDT'
);$time = $client->publicData()->getTime();
echo "Server time: " . $time[0]['ts'];// Savings balance
$balance = $client->finance()->getSavingsBalance();
// Purchase / Redeem
$result = $client->finance()->savingsPurchaseRedempt(
ccy: 'USDT',
amt: '100',
side: 'purchase'
);// Available staking offers
$offers = $client->finance()->getStakingDefiOffers(
productId: 'BTC-staking'
);
// Stake
$stake = $client->finance()->purchaseStakingDefi(
productId: 'BTC-staking',
investData: 'BTC',
amt: '0.1'
);| Category | Methods | Description |
|---|---|---|
account() |
53 | Account management |
trade() |
32 | Trading operations |
market() |
24 | Market data |
publicData() |
24 | Public data |
asset() |
26 | Asset management |
finance() |
33 | Financial products |
copyTrading() |
26 | Copy trading |
tradingBot() |
44 | Trading bots |
rfq() |
20 | Block trading (RFQ) |
sprd() |
13 | Spread trading |
rubik() |
15 | Trading statistics |
fiat() |
13 | Fiat operations |
users() |
8 | Sub-accounts |
support() |
2 | Announcements |
systemStatus() |
1 | System status |
affiliate() |
1 | Affiliate program |
-
SPOT— Spot trading -
MARGIN— Margin trading -
SWAP— Perpetual swaps -
FUTURES— Futures -
OPTION— Options
-
cash— Spot (cash) -
cross— Cross margin -
isolated— Isolated margin
-
buy— Buy -
sell— Sell
-
market— Market order -
limit— Limit order -
post_only— Post-only -
fok— Fill-or-Kill -
ioc— Immediate-or-Cancel
Many methods support cursor-based pagination:
// First page
$orders = $client->trade()->getOrdersHistory(
instType: 'SPOT',
limit: 100
);
// Next page
$nextOrders = $client->trade()->getOrdersHistory(
instType: 'SPOT',
after: $orders[count($orders) - 1]['ordId'],
limit: 100
);Back: ← Quick Start | Next: WebSocket API →