Integrating WebSockets

Important Note:

  • To track transactions on-chain (e.g., via Aptos Explorer or Aptos Scan), you must use your profile address, as all on-chain trades are executed using this address.

  • To obtain your profile address, call the /getProfileAddress endpoint with your wallet address as the userAddress parameter.


WebSocket URLs

Testnet URL:

wss://perpetuals-indexer-ws-develop.kanalabs.io/ws/

Mainnet URL:

wss://perpetuals-indexer-ws.kana.trade/ws/

Parameters to Subscribe:

To connect to a WebSocket and subscribe to the relevant topics, send a subscription message in the following format:

Example:

{
  "topic": "topic_name",
  "address": "user_address"
  "market_id": "market_id" (*only for getting orderbook)
}

You can replace topic_name with any of the supported topics (listed below), and user_address with your profile address.


Available Topics

1. Order History

Track your order history with the following subscription:

Response:

2. Deposit/Withdraw History

Subscribe to track deposit and withdraw events:

Response:

3. Trade History

Subscribe to get updates on your past trades:

Response:

4. Positions

Subscribe to track your current positions:

Response:

5. Open Orders

Track open orders in real-time:

Response:

6. Orderbook

Monitor the orderbook for a specific market:

Response:

7. Recent Trades

Subscribe to view recent trades for a given market:

Response:

8. Live Funding Rate

Subscribe to view live funding rate:

Response:

9. Live Order History

Subscribe to view live order history for a given user address (It will update only latest single data):

Response:

10. Live Trade History

Subscribe to view live trade history for a given user address (It will update only latest single data):

Response:


Example of Subscribing to WebSocket Endpoints

To connect to the WebSocket and subscribe to multiple topics, use the following example approach:

  1. Connect to the WebSocket URL (Choose either Testnet or Mainnet URLs depending on your environment).

  2. Send a subscription message for each topic you want to subscribe to (e.g., order history, positions, etc.).

Here is a JavaScript example using WebSocket:


Error Handling

If your WebSocket connection fails, check the following:

  • Ensure you're using the correct WebSocket URL (Testnet vs Mainnet).

  • Double-check your profile address to ensure it is valid and associated with your wallet.

If you encounter any errors in subscription requests, the server may send a response indicating the issue (e.g., invalid topic or missing parameters). Handle these responses appropriately in your code.

Last updated