API Reference

Guide to listen to real-time updates through a web socket connection to the API

The TegroDEX API supports real-time market and order updates through a WebSocket connection. A web socket connection to the API can be created using the /ws endpoint. This is demonstrated below in JavaScript but can be replicated in other languages as well using the appropriate libraries.

// Use the base url for the environment of choice
const BASE_URL = "wss://api.testnet.tegro.com"
// Using the default web socket library provided by JavaScript
const socket = new WebSocket(`${BASE_URL}/v2/ws`)

// Handling messages and callbacks from the web socket connection
socket.onmessage = handleMessage
socket.onopen = resolve

function handleMessage(response)
{
  const json = JSON.parse(res.data)
  /*
  The JSON object has the following structure
  {
  	"action": string,
    "data": Object
  }
  */
  
 	// Handling the message
}

To receive messages on these websocket connections, we first have to subscribe to a channel. The API sends messages through separate channels for every separate wallet address being used on the platform. The following code block demonstrates how to subscribe and unsubscribe from a channel.

const walletAddress = "" // Use a wallet address here

// Subscribing to a channel called `something`
this.socket.send(JSON.stringify({action: 'subscribe', channelId: walletAddress}))

// Unsubscribing from a channel called `something`
this.socket.send(JSON.stringify({action: 'unsubscribe', channelId: walletAddress}))

On subscribing to a wallet address channel, we recieve the following type of messages

ActionTrigger
order_placedAn order has been placed
order_submittedAn order has been submitted and accepted by the system
order_book_updatedOrder book for a market is updated
trade_createdOrder is matched
trade_updatedThe DB is updated with the blockchain transaction information
order_submitted_onchainAn order has been submitted to the blockchain