Skip to content

Examples

Igor Sazonov edited this page Jan 31, 2026 · 1 revision

Examples

This page provides practical examples of how to use the bybit-php library in your projects. For more detailed examples, please refer to the examples directory in the repository.

Get Market Data

This example demonstrates how to fetch market data, such as the server time, tickers, and k-line data.

use Tigusigalpa\ByBit\BybitClient;

$client = new BybitClient(
    apiKey: "your-api-key",
    apiSecret: "your-api-secret",
    testnet: true
);

// Get server time
$serverTime = $client->getServerTime();

// Get tickers
$tickers = $client->getTickers(["category" => "linear", "symbol" => "BTCUSDT"]);

// Get k-line data
$klines = $client->getKline([
    "category" => "linear",
    "symbol" => "BTCUSDT",
    "interval" => "60",
    "limit" => 200
]);

Place a Limit Order

This example shows how to place a limit order.

$order = $client->createOrder([
    "category" => "linear",
    "symbol" => "BTCUSDT",
    "side" => "Buy",
    "orderType" => "Limit",
    "qty" => "0.01",
    "price" => "30000",
    "timeInForce" => "GTC"
]);

Get Positions

This example demonstrates how to fetch your open positions.

$positions = $client->getPositions([
    "category" => "linear",
    "symbol" => "BTCUSDT"
]);

WebSocket Public Stream

This example shows how to subscribe to a public WebSocket stream for real-time ticker updates.

use Tigusigalpa\ByBit\BybitWebSocket;

$ws = new BybitWebSocket(testnet: true);

$ws->subscribeTicker("BTCUSDT");

$ws->onMessage(function ($data) {
    print_r($data);
});

$ws->listen();

Clone this wiki locally