carpenstar / bybitapi-sdk-core
Requires
- php: >=7.4
- dev-master
- 5.2.0.2
- 5.2.0.1
- 5.2.0.0
- 5.1.1.0
- 5.1.0.1
- 5.1.0.0
- 5.0.0.3
- 5.0.0.2
- 5.0.0.1
- 5.0.0.0
- 3.0.5
- 3.0.1
- 3.0.0
- dev-ext_getter_resp_class_name
- dev-rc
- dev-websockets-feature-1
- dev-v.5.2.0.0
- dev-v.5.1.1.0
- dev-v.5.1.0.1
- dev-v.5.1.0.0
- dev-v.5.0.0.3
- dev-v.5.0.0.2
- dev-v.5.0.0.1
- dev-v.5.0.0.0
- dev-v.3.0.5
- dev-v.3.0.1
- dev-v.3.0.0
This package is auto-updated.
Last update: 2025-04-16 09:58:29 UTC
README
Bybit SDK
NOTICE: This is an unofficial SDK, from an independent developer.
Any questions you are interested in regarding the settings, information about the bugs found, you can leave in Issues, by writing to mighty.vlad@gmail.com (ru, en) or in telegram: @novisad0189
And yes - the more stars, the more actively this project will develop :-)
Requirements
- PHP >= 7.4 Additional for websockets-package:
- posix - extension
- pcntl - extension
Installation
composer require carpenstar/bybitapi-sdk-spot:3.*
composer require carpenstar/bybitapi-sdk-derivatives:3.*
composer require carpenstar/bybitapi-sdk-websockets:3.*
Market Data - V5 (in developing...)
composer require carpenstar/bybitapi-sdk-v5-market:5.*
API key generation
https://testnet.bybit.com/app/user/api-management - testnet
https://www.bybit.com/app/user/api-management - production
Application instance:
use Carpenstar\ByBitAPI\BybitAPI; $sdk = new BybitAPI(); // Setting the host for the next call to the exchange API $sdk->setHost('https://api-testnet.bybit.com'); // Setting an API key that will be applied the next time you access the exchange API (optional, since the parameter is required when accessing private endpoints) $sdk->setApiKey('apiKey'); // Setting a secret key that will be applied the next time you access the exchange API (optional, because the parameter is required when accessing private endpoints) $sdk->setSecret('apiSecret'); // A wrapper function that allows you to set connection parameters with one call $sdk->setCredentials('https://api-testnet.bybit.com', 'apiKey', 'apiSecret') // The function is used to access endpoints that do not require authorization (see endpoint description) $sdk->publicEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>); // The function is used to access endpoints that require authorization (see endpoint description) $sdk->privateEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>)
REST - queries
All endpoints that can be called are divided into two types - public (not requiring authorization) and private (authorization is required for each request).
// The function is used to access endpoints that do not require authorization (see endpoint description) $sdk->publicEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>); // The function is used to access endpoints that require authorization (see endpoint description) $sdk->privateEndpoint(<'Endpoint class name'>, <'DTO containing request parameters'>)
Using the Derivatives/TickerInfo endpoint as an example, let’s make a request to the exchange API:
use Carpenstar\ByBitAPI\BybitAPI; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Request\TickerInfoRequest; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\TickerInfo; // Here we set only the host, because we don’t need authorization $sdk = (new BybitAPI())->setCredentials('https://api-testnet.bybit.com'); // Prepare the endpoint for the request: $endpoint = $sdk->publicEndpoint(TickerInfo::class, (new TickerInfoRequest())->setSymbol('BTCUSDT')); // Start execution of the request: $sdk->execute();
The execute() function always returns an object after completing a request, regardless of whether the request was successful implementing the interface Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface
namespace Carpenstar\ByBitAPI\Core\Interfaces; use Carpenstar\ByBitAPI\Core\Objects\AbstractResponse; interface IResponseInterface { public function getReturnCode(): int; // Request completion code. If successful, it will always be 0 public function getReturnMessage(): string; // Return message, usually 'OK' public function getExtendedInfo(): array; // Extended information public function getResult(): AbstractResponse; // Endpoint-specific DTO object containing the response from the exchange API }
To get the main body of the response, call the getResult function, which will return a DTO object containing information about the ticker.
In the case of the TickerInfo endpoint, this DTO will be an object that implements the Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces\TickerInfoResponse interface
namespace Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces; interface ITickerInfoResponseInterface { /** * @return ITickerInfoResponseItemInterface */ public function getTickerInfo(): ITickerInfoResponseItemInterface; }
Next, calling the getTickerInfo() function will allow us to get an object with information about the ticker (the following DTO implements the ITickerInfoResponseItemInterface interface):
namespace Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces; interface ITickerInfoResponseItemInterface { /** * Symbol name * @return string */ public function getSymbol(): string; /** * Best bid price * @return float */ public function getBidPrice(): float; /** * Best ask price * @return float */ public function getAskPrice(): float; /** * Last transaction price * @return float */ public function getLastPrice(): float; /** * Direction of price change * @return string */ public function getLastTickDirection(): string; /** * Price of 24 hours ago * @return float */ public function getPrevPrice24h(): float; /** * Percentage change of market price relative to 24h * @return float */ public function getPrice24hPcnt(): float; /** * The highest price in the last 24 hours * @return float */ public function getHighPrice24h(): float; /** * Lowest price in the last 24 hours * @return float */ public function getLowPrice24h(): float; /** * Hourly market price an hour ago * @return float */ public function getPrevPrice1h(): float; /** * Mark price * @return float */ public function getMarkPrice(): float; /** * Index price * @return float */ public function getIndexPrice(): float; /** * Open interest * @return float */ public function getOpenInterests(): float; /** * Turnover in the last 24 hours * @return float */ public function getTurnover24h(): float; /** * Trading volume in the last 24 hours * @return float */ public function getVolume24h(): float; /** * Funding rate * @return float */ public function getFundingRate(): float; /** * Next timestamp for funding to settle * @return \DateTime */ public function getNextFundingTime(): \DateTime; /** * Predicted delivery price. It has value when 30 min before delivery * @return float */ public function getPredictedDeliveryPrice(): float; /** * Basis rate for futures * @return float */ public function getBasisRate(): float; /** * Delivery fee rate * @return float */ public function getDeliveryFeeRate(): float; /** * Delivery timestamp * @return \DateTime */ public function getDeliveryTime(): \DateTime; /** * Open interest value * @return float */ public function getOpenInterestValue(): float; }
To summarize the above, as an example code this query might look like this:
use Carpenstar\ByBitAPI\BybitAPI; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Interfaces\ITickerInfoResponseItemInterface; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\Request\TickerInfoRequest; use Carpenstar\ByBitAPI\Derivatives\MarketData\TickerInfo\TickerInfo; $sdk = (new BybitAPI())->setCredentials('https://api-testnet.bybit.com'); $endpoint = $sdk->publicEndpoint(TickerInfo::class, (new TickerInfoRequest())->setSymbol('BTCUSDT')); $endpointResponse = $sdk->execute(); echo "Return code: {$endpointResponse->getReturnCode()}\n"; echo "Return message: {$endpointResponse->getReturnMessage()}\n"; /** @var ITickerInfoResponseItemInterface $tickerInfo */ $tickerInfo = $endpointResponse->getResult()->getTickerInfo(); echo "Symbol: {$tickerInfo->getSymbol()}\n"; echo "Bid Price: {$tickerInfo->getBidPrice()}\n"; echo "Ask Price: {$tickerInfo->getAskPrice()}\n"; echo "Last Price: {$tickerInfo->getLastPrice()}\n"; echo "Last Tick Direction: {$tickerInfo->getLastTickDirection()}\n"; echo "Prev Price 24 hours: {$tickerInfo->getPrevPrice24h()}\n"; echo "Prev Price 24 hours(%): {$tickerInfo->getPrice24hPcnt()}\n"; echo "High Price 24 hours: {$tickerInfo->getHighPrice24h()}\n"; echo "Low Price 24 hours: {$tickerInfo->getLowPrice24h()}\n"; echo "Prev price 1 hour: {$tickerInfo->getPrevPrice1h()}\n"; echo "Mark Price: {$tickerInfo->getMarkPrice()}\n"; echo "Index Price: {$tickerInfo->getIndexPrice()}\n"; echo "Open Interest: {$tickerInfo->getOpenInterests()}\n"; echo "Open Interest Value: {$tickerInfo->getOpenInterestValue()}\n"; echo "Turnover 24 hours: {$tickerInfo->getTurnover24h()}\n"; echo "Volume 24 hours: {$tickerInfo->getVolume24h()}\n"; echo "Funding Rate: {$tickerInfo->getFundingRate()}\n"; echo "Next Funding Time: {$tickerInfo->getNextFundingTime()->format("Y-m-d H:i:s")}\n"; echo "Predicted Delivery Price: {$tickerInfo->getPredictedDeliveryPrice()}\n"; echo "Basis Rate: {$tickerInfo->getBasisRate()}\n"; echo "Delivery Fee Rate: {$tickerInfo->getDeliveryFeeRate()}\n"; echo "Open Interests Value: {$tickerInfo->getOpenInterestValue()}\n"; /** * Return code: 0 * Return message: OK * Symbol: BTCUSDT * Bid Price: 59933.6 * Ask Price: 59935.7 * Last Price: 59938 * Last Tick Direction: ZeroMinusTick * Prev Price 24 hours: 58627.5 * Prev Price 24 hours(%): 0.022352 * High Price 24 hours: 63074.5 * Low Price 24 hours: 58267.4 * Prev price 1 hour: 59997 * Mark Price: 59938 * Index Price: 59957.26 * Open Interest: 208384.158 * Open Interest Value: 12490129662.2 * Turnover 24 hours: 2907929540.5417 * Volume 24 hours: 48504.964 * Funding Rate: 8.407E-5 * Next Funding Time: 2024-07-15 00:00:00 * Predicted Delivery Price: 0 * Basis Rate: 0 * Delivery Fee Rate: 0 * Open Interests Value: 12490129662.2 */
An example of calls to the API, as well as a description of the available request/response objects can be found on the page of each endpoint
List of available REST endpoints
ByBit API V3 - DERIVATIVES:
MARKET DATA | ||||
---|---|---|---|---|
Endpoint | Access type | View in directory | Official documentation | Language |
Funding Rate History | publicEndpoint | go | go | EN, RU |
Index Price Kline | publicEndpoint | go | go | EN, RU |
Instrument Info | publicEndpoint | go | go | EN, RU |
Kline | publicEndpoint | go | go | EN, RU |
Mark Price Kline | publicEndpoint | go | go | EN, RU |
Open Interest | publicEndpoint | go | go | EN, RU |
Order Book | publicEndpoint | go | go | EN, RU |
Public Trading History | publicEndpoint | go | go | EN, RU |
Risk Limit | publicEndpoint | go | go | EN, RU |
Ticker Info | publicEndpoint | go | go | EN, RU |
CONTRACT - ACCOUNT | ||||
Endpoint | Access type | View in directory | Official documentation | |
Get Trading Fee Rate | privateEndpoint | go | go | EN, RU |
Wallet Balance | privateEndpoint | go | go | EN, RU |
CONTRACT - ORDER | ||||
Endpoint | Access type | View in directory | Official documentation | |
Cancel All Order | privateEndpoint | go | go | EN, RU |
Cancel Order | privateEndpoint | go | go | EN, RU |
Get Open Orders | privateEndpoint | go | go | EN, RU |
Get Order List | privateEndpoint | go | go | EN, RU |
Place Order | privateEndpoint | go | go | EN, RU |
Replace Order | privateEndpoint | go | go | EN, RU |
CONTRACT - POSITION | ||||
Endpoint | Access type | View in directory | Official documentation | Language |
Get Closed PnL | privateEndpoint | go | go | EN, RU |
Get Execution List | privateEndpoint | go | go | EN, RU |
My Position | privateEndpoint | go | go | EN, RU |
Set Auto Add Margin | privateEndpoint | go | go | EN, RU |
Set Leverage | privateEndpoint | go | go | EN, RU |
Set Risk Limit | privateEndpoint | go | go | EN, RU |
Set Trading Stop | privateEndpoint | go | go | EN, RU |
Switch Cross Isolated Margin | privateEndpoint | go | go | EN, RU |
Switch Position Mode | privateEndpoint | go | go | EN, RU |
Switch TpSl Mode | privateEndpoint | go | go | EN, RU |
ByBit API V3 - SPOT:
MARKET DATA | ||||
---|---|---|---|---|
Endpoint | Access type | View in directory | Official documentation | Language |
Best Bid Ask Price | publicEndpoint | go | go | EN, RU |
Instrument Info | publicEndpoint | go | go | EN, RU |
Kline | publicEndpoint | go | go | EN, RU |
Last Traded Price | publicEndpoint | go | go | EN, RU |
Merged Order Book | publicEndpoint | go | go | EN, RU |
Public Trading Records | publicEndpoint | go | go | EN, RU |
Tickers | publicEndpoint | go | go | EN, RU |
Order Book | publicEndpoint | go | go | EN, RU |
TRADE | ||||
Endpoint | Access type | View in directory | Official documentation | |
Place Order | privateEndpoint | go | go | EN, RU |
Get Order | privateEndpoint | go | go | EN, RU |
Cancel Order | privateEndpoint | go | go | EN, RU |
Batch Cancel Order | privateEndpoint | go | go | EN, RU |
Batch Cancel Order By Id | privateEndpoint | go | go | EN, RU |
Open Orders | privateEndpoint | go | go | EN, RU |
Order History | privateEndpoint | go | go | EN, RU |
Trade History | privateEndpoint | go | go | EN, RU |
LEVERAGE TOKEN | ||||
Endpoint | Access type | View in directory | Official documentation | |
All Asset Info | publicEndpoint | go | go | EN |
Market Info | publicEndpoint | go | go | EN |
Purchase | publicEndpoint | go | go | EN |
Purchase Redeem History | publicEndpoint | go | go | EN |
Redeem | publicEndpoint | go | go | EN |
ACCOUNT | ||||
Endpoint | Access type | View in directory | Official documentation | |
Wallet Balance | privateEndpoint | go | go | EN |