Bybit JavaScript SDK example: ws-api-raw-events.ts

Bybit websocket API RAW events JavaScript example. Read the source code, review its SDK calls, and browse related exchange integration examples.

This example uses bybit-api, the JavaScript and TypeScript SDK for Bybit. Install it with npm install bybit-api.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/Bybit/Websocket/WS-API/ws-api-raw-events.ts

What this example covers

  • Bybit WebSocket API request/response JavaScript example.
  • Uses the Siebly Bybit SDK package bybit-api instead of hand-written WebSocket plumbing.
  • Source path: Bybit/Websocket/WS-API/ws-api-raw-events.ts.
  • Imports SDK symbols including DefaultLogger, WebsocketClient, WS_KEY_MAP.
  • Calls SDK methods such as on(), connectWSAPI().

How to use this example

  • Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
  • For WebSocket examples, keep reconnect, resubscribe, heartbeat, and event-handler behavior explicit in your service.
  • Open the repository source when you need the latest committed version: GitHub source file.
import { DefaultLogger, WebsocketClient, WS_KEY_MAP } from 'bybit-api'; const logger = {  ...DefaultLogger,  // For a more detailed view of the WebsocketClient, enable the `trace` level by uncommenting the below line:  // trace: (...params) => console.log('trace', ...params),}; const key = process.env.API_KEY;const secret = process.env.API_SECRET; const wsClient = new WebsocketClient(  {    key: key,    secret: secret,    // testnet: true, // Whether to use the testnet environment: https://testnet.bybit.com/app/user/api-management    // demoTrading: false, // note: As of Jan 2025, demo trading does NOT support the WS API  },  logger, // Optional: inject a custom logger); /** * General event handlers for monitoring the WebsocketClient */wsClient.on('update', (data) => {  console.log('raw message received ', JSON.stringify(data));});wsClient.on('open', (data) => {  console.log('ws connected', data.wsKey);});wsClient.on('reconnect', ({ wsKey }) => {  console.log('ws automatically reconnecting.... ', wsKey);});wsClient.on('reconnected', (data) => {  console.log('ws has reconnected ', data?.wsKey);});wsClient.on('authenticated', (data) => {  console.log('ws has authenticated ', data?.wsKey);}); async function main() {  /**   *   * This SDK's WebSocket API integration is event-driven at its core. You can treat the sentWSAPIRquest(...) method as   * a fire-and-forget method, to submit commands (create/amend/cancel order) via a WebSocket Connection.   *   * Replies to commands will show in the `response` event from the WebsocketClient's EventEmitter. Exceptions, however,   * will show in the `error` event from the WebsocketClient's EventEmitter.   *   * - Fire-and-forget a command.   * - Handle command results in the `response` event handler asynchronously as desired.   * - Handle any exceptions in a catch block.   *   * This is a more "raw" workflow in how WebSockets behave. For a more convenient & REST-like approach, using the   * promise-driven interface is recommended. See the `ws-api-raw-promises.ts` and `ws-api-client.ts` examples for a   * demonstration you can compare.   *   * Note: even without using promises, you should still tie on a .catch handler to each sendWSAPIRequest call, to prevent   * any unnecessary "unhandled promise rejection" exceptions.   *   */   // To make it easier to watch, wait a few seconds before sending the amend order  const AMEND_AFTER_SECONDS = 3;   // Then wait a few more before sending the cancel order  const CANCEL_AFTER_SECONDS = 5;   // Exceptions including rejected commands will show here (as well as the catch handler used below)  wsClient.on('exception', (data) => {    console.error('ws exception: ', data);  });   // Replies to commands will show here  wsClient.on('response', (data) => {    console.log('received reply to command: ', JSON.stringify(data, null, 2));  });   /**   *   * If you haven't connected yet, the WebsocketClient will automatically connect and authenticate you as soon as you send   * your first command. That connection will then be reused for every command you send, unless the connection drops - then   * it will automatically be replaced with a healthy connection.   *   * This "not connected yet" scenario can add an initial delay to your first command. If you want to prepare a connection   * in advance, you can ask the WebsocketClient to prepare it before you start submitting commands. This is optional.   *   * Repeated note: even without using promises, you should still tie on a .catch handler to each sendWSAPIRequest call, to prevent   * any unnecessary "unhandled promise rejection" exceptions.   *   */   // Optional, see above. Can be used to prepare a connection before sending commands  await wsClient.connectWSAPI();   console.log('Step 1: Create an order');   // Fire and forget the create.order command  // Even without using promises, you should still "catch" exceptions (although no need to await anything you send)  wsClient    .sendWSAPIRequest(WS_KEY_MAP.v5PrivateTrade, 'order.create', {      symbol: 'BTCUSDT',      side: 'Buy',      orderType: 'Limit',      price: '50000',      qty: '1',      category: 'linear',    })    .catch((e) => console.error('Step 1: Order submit exception: ', e));   console.log('Step 1: Create order sent...');   //  setTimeout(() => {    console.log('Step 2: Amend an order');     // Fire and forget the order.amend command    // For simplicity, the orderId is hardcoded here (and will probably not work)    wsClient      .sendWSAPIRequest(WS_KEY_MAP.v5PrivateTrade, 'order.amend', {        symbol: 'BTCUSDT',        category: 'linear',        orderId: '1234567',        price: '55000',      })      .catch((e) => console.error('Step 2: Amend order exception: ', e));     console.log('Step 2: Amend order sent...');  }, AMEND_AFTER_SECONDS * 1000);   //  setTimeout(() => {    console.log('Step 3: Cancel an order');     // Fire and forget the order.cancel command    // For simplicity, the orderId is hardcoded here (and will probably not work)    wsClient      .sendWSAPIRequest(WS_KEY_MAP.v5PrivateTrade, 'order.cancel', {        category: 'linear',        symbol: 'BTCUSDT',        orderId: '1234567',      })      .catch((e) => console.error('Step 3: Cancel order exception: ', e));     console.log('Step 3: Cancel order sent...');  }, CANCEL_AFTER_SECONDS * 1000);} // Start executing the example workflowmain(); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.