-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Igor Sazonov edited this page Mar 15, 2026
·
1 revision
Get up and running with the OKX PHP SDK in just a few minutes.
After installation, add the following to your .env file:
OKX_API_KEY=your-api-key
OKX_SECRET_KEY=your-secret-key
OKX_PASSPHRASE=your-passphraseuse Tigusigalpa\OKX\Facades\OKX;
// Get account balance
$balance = OKX::account()->getBalance();
// Get balance for a specific currency
$btcBalance = OKX::account()->getBalance('BTC');// Market order (spend USDT)
$order = OKX::trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'market',
sz: '100', // spend 100 USDT
tgtCcy: 'quote_ccy'
);
// Limit order
$order = OKX::trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'limit',
sz: '0.01', // amount in BTC
px: '50000' // limit price
);// Current price
$ticker = OKX::market()->getTicker('BTC-USDT');
echo "BTC price: " . $ticker[0]['last'];
// Recent trades
$trades = OKX::market()->getTrades('BTC-USDT', limit: 10);
// Candlestick data
$candles = OKX::market()->getCandles(
instId: 'BTC-USDT',
bar: '1H',
limit: 100
);// Get open orders
$orders = OKX::trade()->getOrdersPending(
instType: 'SPOT'
);
// Cancel an order
$result = OKX::trade()->cancelOrder(
instId: 'BTC-USDT',
ordId: '123456789'
);
// Get order history
$history = OKX::trade()->getOrdersHistory(
instType: 'SPOT',
limit: 50
);use Tigusigalpa\OKX\Client;
$client = new Client(
apiKey: 'your-api-key',
secretKey: 'your-secret-key',
passphrase: 'your-passphrase',
isDemo: false
);// Account balance
$balance = $client->account()->getBalance();
// Market data
$ticker = $client->market()->getTicker('BTC-USDT');
// Public data (no authentication required)
$instruments = $client->publicData()->getInstruments(
instType: 'SPOT'
);// Place an order
$order = $client->trade()->placeOrder(
instId: 'ETH-USDT',
tdMode: 'cash',
side: 'sell',
ordType: 'limit',
sz: '0.1',
px: '3000'
);
// Check order status
$orderInfo = $client->trade()->getOrder(
instId: 'ETH-USDT',
ordId: $order[0]['ordId']
);use Tigusigalpa\OKX\WebsocketClient;
$ws = new WebsocketClient(
apiKey: 'your-api-key',
secretKey: 'your-secret-key',
passphrase: 'your-passphrase'
);
// Connect to the public endpoint
$ws->connectPublic();
// Subscribe to ticker updates
$ws->subscribe('tickers', ['instId' => 'BTC-USDT'], function ($data) {
$ticker = $data['data'][0];
echo "BTC-USDT: {$ticker['last']}\n";
});
// Start listening
$ws->run();// Connect to the private endpoint
$ws->connectPrivate();
// Subscribe to account updates
$ws->subscribe('account', ['ccy' => 'BTC'], function ($data) {
foreach ($data['data'] as $account) {
echo "Balance {$account['ccy']}: {$account['bal']}\n";
}
});
// Subscribe to order updates
$ws->subscribe('orders', ['instType' => 'SPOT'], function ($data) {
foreach ($data['data'] as $order) {
echo "Order {$order['ordId']}: {$order['state']}\n";
}
});
$ws->run();$balance = OKX::account()->getBalance('USDT');
$availableBalance = $balance[0]['details'][0]['availBal'];
if ((float)$availableBalance >= 100) {
$order = OKX::trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'market',
sz: '100',
tgtCcy: 'quote_ccy'
);
} else {
echo "Insufficient funds";
}function getCurrentPrice(string $symbol): string
{
$ticker = OKX::market()->getTicker($symbol);
return $ticker[0]['last'];
}
$btcPrice = getCurrentPrice('BTC-USDT');
echo "BTC: $" . $btcPrice;$order = OKX::trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cross',
side: 'buy',
ordType: 'limit',
sz: '0.1',
px: '50000',
tpTriggerPx: '55000', // Take Profit trigger
tpOrdPx: '-1', // Execute at market price
slTriggerPx: '48000', // Stop Loss trigger
slOrdPx: '-1' // Execute at market price
);$orders = OKX::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'
]
]);$candles = OKX::market()->getHistoryCandles(
instId: 'BTC-USDT',
bar: '1D', // daily candles
limit: 365 // one year
);
foreach ($candles as $candle) {
[$timestamp, $open, $high, $low, $close, $volume] = $candle;
echo date('Y-m-d', $timestamp / 1000) . ": $close\n";
}use Tigusigalpa\OKX\Exceptions\OKXException;
use Tigusigalpa\OKX\Exceptions\RateLimitException;
try {
$order = OKX::trade()->placeOrder(
instId: 'BTC-USDT',
tdMode: 'cash',
side: 'buy',
ordType: 'market',
sz: '100'
);
} catch (RateLimitException $e) {
// Rate limit hit — wait and retry
sleep(1);
} catch (OKXException $e) {
// Other OKX API errors
echo "Error [{$e->okxCode}]: {$e->getMessage()}";
}To test without any financial risk:
// In .env
OKX_DEMO=true
// Or in code
$client = new Client(
apiKey: 'demo-api-key',
secretKey: 'demo-secret-key',
passphrase: 'demo-passphrase',
isDemo: true
);- REST API — Full list of available methods
- WebSocket API — Working with real-time data
- Examples — Advanced usage patterns
- Error Handling — In-depth guide to exceptions
Back: ← Installation | Next: REST API →