Bybit Client API

From sgcWebSockets 2022.10.0 Bybit Crypto Exchange is supported. The following APIs supported

  • WebSocket API: connect to a websocket server and provides real-time market data updates, account changes and more.
  • REST API: send HTTP requests to get market data, place orders, account data...

The following product APIs are implemented:

  • Spot (V3)
  • USDT Perpetual (Futures V2)
  • Inverse Perpetual (Futures V2)
  • Inverse Futures (Futures V2)

Properties 

You can configure the following properties in the Bybit property.

  • ApiKey: you can request a new api key in your Bybit account, just copy the value to this property. If the APIKey is set, the client will connect to the websocket private server. If it's empty, will connect to the WebSocket public server.
  • ApiSecret: it's the secret value of the api.
  • SignatureExpires: number of seconds after the signature expires (by default 10 seconds).
  • TestNet: if enabled, will connect to the Bybit TestNet Demo account (disabled by default).

Connection 

When the client successfully connects to Bybit servers, the event OnConnect is fired. After the event OnConnect is fired, then you can start to send and receive messages to/from Bybit servers. If you are connecting to the private websocket channel, you must wait till OnBybitAuthentication event is fired and check if the success parameter is true, before subscribe to any channel.

The client supports several APIs, so use the property BybitClient to set which API you want to use:

  • bybSpot
  • bybInversePerpetual
  • bybUSDTPerpetual
  • bybInverseFutures

Find below an example of connecting to WebSocket Spot Private API.

oClient := TsgcWebSocketClient.Create(nil);
oBybit := TsgcWSAPI_Bybit.Create(nil);
oBybit.Client := oClient;
oBybit.Bybit.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oBybit.Bybit.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oBybit.BybitClient := bybSpot;
oClient.Active := True;

procedure OnConnect(Connection: TsgcWSConnection);
begin
  DoLog('#Bybit Connected');
end; 

After a successfull connection to the Spot WebSocket Server, you can start to subscribe to WebSocket channels, just access the SPOT property and then call any of the subscribe/unsubscribe methods available.

Find below the list of the Bybit APIs objects:

  • SPOT
  • INVERSE_PERPETUAL
  • USDT_PERPETUAL
  • INVERSE_FUTURES

Subscribe to WebSocket Channels 

Find below an example of subscribing to the Private Spot Websocket Channels after a successful authentication.

oClient := TsgcWebSocketClient.Create(nil);
oBybit := TsgcWSAPI_Bybit.Create(nil);
oBybit.Client := oClient;
oBybit.Bybit.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oBybit.Bybit.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oBybit.BybitClient := bybSpot;
oClient.Active := True;

procedure OnBybitAuthentication(Sender: TObject; aSuccess: Boolean; const aError, aRawMessage: string)
begin
  if aSuccess then
  begin
    oClient.SubscribeOrder;
    oClient.SubscribeStopOrder;
  end;
end; 

Placing Orders 

Find below an example of Placing a Market Order for USDT Perpetual.


oClient := TsgcWebSocketClient.Create(nil);
oBybit := TsgcWSAPI_Bybit.Create(nil);
oBybit.Client := oClient;
oBybit.Bybit.ApiKey := 'alsdjk23kandfnasbdfdkjhsdf';
oBybit.Bybit.ApiSecret := 'aldskjfk3jkadknfajndsjfj23j';
oBybit.BybitClient := bybUSDTPerpetual;

oBybit.USDT_PERPETUAL.REST_API.PlaceMarketOrder('BTCUSDT', bbosBuy, 1); 

Compiled Windows Bybit Demo

Download a compiled Bybit Client API Demo for Windows that shows the main features of the sgcWebSockets Bybit API Client.
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Indy Setup Rad Studio Delphi and CBuilder
sgcWebSockets 2022.9

Related Posts