Skip to content

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.

Laravel Quick Start

1. Basic configuration

After installation, add the following to your .env file:

OKX_API_KEY=your-api-key
OKX_SECRET_KEY=your-secret-key
OKX_PASSPHRASE=your-passphrase

2. Using the Facade

use Tigusigalpa\OKX\Facades\OKX;

// Get account balance
$balance = OKX::account()->getBalance();

// Get balance for a specific currency
$btcBalance = OKX::account()->getBalance('BTC');

3. Placing an order

// 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
);

4. Fetching market data

// 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
);

5. Managing orders

// 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
);

Standalone Quick Start

1. Creating a client

use Tigusigalpa\OKX\Client;

$client = new Client(
    apiKey: 'your-api-key',
    secretKey: 'your-secret-key',
    passphrase: 'your-passphrase',
    isDemo: false
);

2. Fetching data

// 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'
);

3. Trading

// 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']
);

WebSocket Quick Start

1. Connecting to public channels

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();

2. Connecting to private channels

// 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();

Common Scenarios

Check balance before trading

$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";
}

Get the current price

function getCurrentPrice(string $symbol): string
{
    $ticker = OKX::market()->getTicker($symbol);
    return $ticker[0]['last'];
}

$btcPrice = getCurrentPrice('BTC-USDT');
echo "BTC: $" . $btcPrice;

Place an order with Stop Loss and Take Profit

$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
);

Batch order placement

$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'
    ]
]);

Fetch historical candlestick data

$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";
}

Error Handling

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()}";
}

Demo Mode

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
);

Next Steps


Back: ← Installation | Next: REST API →

Clone this wiki locally