-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Complete method reference for the OKX PHP SDK.
The main class for interacting with the OKX API.
public function __construct(
string $apiKey,
string $secretKey,
string $passphrase,
bool $isDemo = false,
string $baseUrl = 'https://www.okx.com',
?HttpClient $httpClient = null,
?LoggerInterface $logger = null
)Parameters:
-
$apiKey— OKX API key -
$secretKey— Secret key -
$passphrase— Passphrase -
$isDemo— Enable demo trading mode -
$baseUrl— Base API URL -
$httpClient— Optional HTTP client (Guzzle) -
$logger— Optional PSR-3 logger
$client->account() // Account API
$client->trade() // Trade API
$client->market() // Market API
$client->publicData() // Public Data API
$client->asset() // Asset API
$client->finance() // Finance API
$client->copyTrading() // Copy Trading API
$client->tradingBot() // Trading Bot API
$client->rfq() // RFQ API
$client->sprd() // Spread Trading API
$client->rubik() // Trading Statistics API
$client->fiat() // Fiat API
$client->users() // Users API
$client->support() // Support API
$client->systemStatus() // System Status API
$client->affiliate() // Affiliate APIGet account balance.
public function getBalance(?string $ccy = null): arrayParameters:
-
$ccy— Currency (optional)
Returns: Array with balance data
Example:
$balance = $client->account()->getBalance();
$btcBalance = $client->account()->getBalance('BTC');Get open positions.
public function getPositions(
?string $instType = null,
?string $instId = null,
?string $posId = null
): arrayParameters:
-
$instType— Instrument type (SPOT, SWAP, FUTURES, OPTION) -
$instId— Instrument ID -
$posId— Position ID
Set leverage.
public function setLeverage(
string $lever,
?string $mgnMode = null,
?string $instId = null,
?string $ccy = null,
?string $posSide = null
): arrayParameters:
-
$lever— Leverage value (e.g.,"10") -
$mgnMode— Margin mode (cross,isolated) -
$instId— Instrument ID -
$ccy— Currency -
$posSide— Position side (long,short)
Place an order.
public function placeOrder(
string $instId,
string $tdMode,
string $side,
string $ordType,
string $sz,
?string $px = null,
?string $ccy = null,
?string $clOrdId = null,
?string $tag = null,
?string $posSide = null,
?bool $reduceOnly = null,
?string $tgtCcy = null,
?bool $banAmend = null,
?string $tpTriggerPx = null,
?string $tpOrdPx = null,
?string $slTriggerPx = null,
?string $slOrdPx = null,
?string $tpTriggerPxType = null,
?string $slTriggerPxType = null,
?string $quickMgnType = null,
?string $stpId = null,
?string $stpMode = null,
?array $attachAlgoOrds = null
): arrayRequired parameters:
-
$instId— Instrument ID (e.g.,"BTC-USDT") -
$tdMode— Trade mode (cash,cross,isolated) -
$side— Side (buy,sell) -
$ordType— Order type (market,limit,post_only,fok,ioc) -
$sz— Order size
Optional parameters:
-
$px— Price (required for limit orders) -
$tgtCcy— Target currency (base_ccy,quote_ccy) -
$tpTriggerPx— Take Profit trigger price -
$slTriggerPx— Stop Loss trigger price
Example:
// Market order
$order = $client->trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'market',
sz: '100',
tgtCcy: 'quote_ccy'
);
// Limit order with TP/SL
$order = $client->trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cross',
side: 'buy',
ordType: 'limit',
sz: '0.1',
px: '50000',
tpTriggerPx: '55000',
slTriggerPx: '48000'
);Cancel an order.
public function cancelOrder(
string $instId,
?string $ordId = null,
?string $clOrdId = null
): arrayParameters:
-
$instId— Instrument ID -
$ordId— Order ID (eitherordIdorclOrdIdis required) -
$clOrdId— Client Order ID
Get open orders.
public function getOrdersPending(
?string $instType = null,
?string $uly = null,
?string $instId = null,
?string $ordType = null,
?string $state = null,
?string $after = null,
?string $before = null,
?int $limit = null,
?string $instFamily = null
): arrayGet trade fills.
public function getFills(
?string $instType = null,
?string $uly = null,
?string $instId = null,
?string $ordId = null,
?string $after = null,
?string $before = null,
?string $begin = null,
?string $end = null,
?int $limit = null,
?string $instFamily = null
): arrayGet ticker data.
public function getTicker(string $instId): arrayParameters:
-
$instId— Instrument ID
Returns:
[
[
'instId' => 'BTC-USDT',
'last' => '50000',
'lastSz' => '0.1',
'askPx' => '50001',
'bidPx' => '49999',
'open24h' => '49000',
'high24h' => '51000',
'low24h' => '48000',
'vol24h' => '1000',
'volCcy24h' => '50000000'
]
]Get the order book.
public function getBooks(
string $instId,
?string $sz = null
): arrayParameters:
-
$instId— Instrument ID -
$sz— Order book depth (1–400)
Get candlestick data.
public function getCandles(
string $instId,
?string $bar = null,
?string $after = null,
?string $before = null,
?int $limit = null
): arrayParameters:
-
$instId— Instrument ID -
$bar— Interval (1m,3m,5m,15m,30m,1H,2H,4H,6H,12H,1D,1W,1M) -
$after— Timestamp (after) -
$before— Timestamp (before) -
$limit— Number of candles (max 300)
Returns:
[
[timestamp, open, high, low, close, volume, volumeCcy],
...
]Get recent trades.
public function getTrades(
string $instId,
?int $limit = null
): arrayTransfer funds between accounts.
public function transfer(
string $ccy,
string $amt,
string $from,
string $to,
?string $type = null,
?string $subAcct = null,
?string $instId = null,
?string $toInstId = null,
?bool $loanTrans = null,
?string $clientId = null,
?bool $omitPosRisk = null
): arrayParameters:
-
$ccy— Currency -
$amt— Amount -
$from— Source account (6: Funding,18: Trading) -
$to— Destination account (6: Funding,18: Trading)
Withdraw funds.
public function withdrawal(
string $ccy,
string $amt,
string $dest,
string $toAddr,
string $fee,
?string $chain = null,
?string $areaCode = null,
?string $clientId = null
): arrayParameters:
-
$ccy— Currency -
$amt— Amount -
$dest— Destination type (3: internal transfer,4: on-chain) -
$toAddr— Recipient address -
$fee— Transaction fee -
$chain— Network (e.g.,"USDT-TRC20")
public function __construct(
string $apiKey,
string $secretKey,
string $passphrase,
bool $isDemo = false,
?LoggerInterface $logger = null
)Connect to public channels.
public function connectPublic(): voidConnect to private channels.
public function connectPrivate(): voidConnect to business channels.
public function connectBusiness(): voidSubscribe to a channel.
public function subscribe(
string $channel,
array $args,
callable $callback
): voidParameters:
-
$channel— Channel name -
$args— Channel arguments -
$callback— Callback function to handle incoming data
Example:
$ws->subscribe('tickers', ['instId' => 'BTC-USDT'], function ($data) {
echo "Price: " . $data['data'][0]['last'];
});Unsubscribe from a channel.
public function unsubscribe(string $channel, array $args): voidStart listening for messages.
public function run(): voidStop the client.
public function stop(): voidBase exception class.
Properties:
-
$okxCode— OKX error code -
$rawResponse— Raw response from the API
Authentication errors (50111, 50113).
Rate limit exceeded (50011).
Invalid request parameters (51000–51099).
Insufficient funds (54000–54099).
-
SPOT— Spot trading -
MARGIN— Margin trading -
SWAP— Perpetual swaps -
FUTURES— Futures -
OPTION— Options
-
cash— Spot (cash) -
cross— Cross margin -
isolated— Isolated margin
-
market— Market order -
limit— Limit order -
post_only— Post-only -
fok— Fill-or-Kill -
ioc— Immediate-or-Cancel -
optimal_limit_ioc— Optimal limit IOC
-
buy— Buy -
sell— Sell
-
long— Long position -
short— Short position -
net— Net (one-way mode)
-
1m,3m,5m,15m,30m— Minutes -
1H,2H,4H,6H,12H— Hours -
1D— Day -
1W— Week -
1M— Month
getAccountLevel()getBalance(?string $ccy)getBills(...)getConfig()getPositions(...)setLeverage(...)setPositionMode(string $posMode)increaseDecreaseMargin(...)- And more...
placeOrder(...)batchOrders(array $orders)cancelOrder(...)cancelBatchOrders(...)amendOrder(...)getOrder(...)getOrdersPending(...)getOrdersHistory(...)getFills(...)placeAlgoOrder(...)- And more...
getTicker(string $instId)getTickers(...)getBooks(...)getCandles(...)getTrades(...)getHistoryCandles(...)- And more...
getBalances(?string $ccy)transfer(...)withdrawal(...)getDepositAddress(string $ccy)getDepositHistory(...)getWithdrawalHistory(...)- And more...
getInstruments(...)getTime()getFundingRate(string $instId)getOpenInterest(...)getPositionTiers(...)- And more...
All methods return arrays containing the data from the OKX API. The general response structure is:
[
[
// First item data
],
[
// Second item data
],
...
]For typed access, you can use DTOs:
use Tigusigalpa\OKX\DTO\Trade\PlaceOrderResponse;
$order = $client->trade()->placeOrder(...);
$orderDto = PlaceOrderResponse::fromArray($order[0]);
echo $orderDto->ordId;
echo $orderDto->sCode;