How to use Websocket API for order.status endpoint?

I am trying to open a websocket connection to check status of my orders and eventually make some action if status has changed. I am having 404 error message and a 1006 code as if my url is incorrect which i don’t believe is not the case. Please see my snippet below

const wsTestURL2 = 'wss://testnet.binance.vision/ws-api/v3'

export async function getOrderStatusFromBinance(symbol: string, orderId: number): Promise<Order> {
  console.log("order id",orderId)
  return new Promise(async (resolve, reject) => {
    const listenKey = await getUserDataStream();
    const wsUserData = new WebSocket(`${wsTestURL2}/${listenKey}`);

    if (!testApiSecret) {
      throw new Error('No test API secret provided');
    }
    const requestId = uuidv4();
    wsUserData.on('open', () => {
      const timeStamp = Date.now();
      const queryString = `symbol=${symbol}&orderId=${orderId}&timestamp=${timeStamp}`;
      const signature = crypto.createHmac("sha256", testApiSecret).update(queryString).digest("hex");

      const params = {
        symbol: symbol,
        orderId: orderId,
        apiKey: testApiKey,
        signature: signature,
        timestamp: timeStamp,
      }
      const message = {
        id: requestId,
        method: 'order.status',
        params: params,
      }
      wsUserData.send(JSON.stringify(message));
    });

    wsUserData.on('message', (message: string) => {
      const data = JSON.parse(message) as { id: string; result: Order };
       console.log('order status', data);
      if (data.id === requestId) {
        resolve(data.result);
      }
    });

    wsUserData.on('error', (error) => {
      reject(error);
    });
  });
}
``` what do i do wrong here?  I made a change to the url as follow , ‘wss://testnet.binance.vision/ws’, i now get error message order status {
[1] error: {
[1] code: 2,
[1] msg: ‘Invalid request: unknown variant order.status, expected one of SUBSCRIBE, UNSUBSCRIBE, LIST_SUBSCRIPTIONS, SET_PROPERTY, GET_PROPERTY at line 1 column 68’
[1] }
[1] } however the documentation do not require this params in the request .I am using this documention as reference [Documention link](https://developers.binance.com/docs/binance-trading-api/websocket_api#query-order-user_data)
wss://testnet.binance.vision/ws

Is the URL for spot testnet websocket stream, not for websocket API.


In order to query order status via websocket API, you will need to send a json message to the server:


"method": "order.status"

Please check the document Binance API Documentation

1 Like

Hi @dino, i added the quote but it did not change anything. Using the correct url for the websocket api wss://testnet.binance.vision/ws-api/v3

export async function getOrderStatusFromBinance(symbol: string, orderId: number): Promise<Order> {
  console.log("order id",orderId)
  return new Promise(async (resolve, reject) => {
    const listenKey = await getUserDataStream();
    const wsUserData = new WebSocket(`${wsTestURL2}/${listenKey}`);

    if (!testApiSecret) {
      throw new Error('No test API secret provided');
    }
    const requestId = uuidv4();
    wsUserData.on('open', () => {
      const timeStamp = Date.now();
      const queryString = `symbol=${symbol}&orderId=${orderId}&timestamp=${timeStamp}`;
      const signature = crypto.createHmac("sha256", testApiSecret).update(queryString).digest("hex");

      const params = {
        symbol: symbol,
        orderId: orderId,
        apiKey: testApiKey,
        signature: signature,
        timestamp: timeStamp,
      }
      const message = {
        "id": requestId,
        "method": "order.status",
        "params": params,
      }
      console.log("message", message)
      wsUserData.send(JSON.stringify(message));
    });

    wsUserData.on('message', (message: string) => {
      const data = JSON.parse(message) as { id: string; result: Order };
       console.log('order status', data);
      if (data.id === requestId) {
        resolve(data.result);
      }
    });

    wsUserData.on('error', (error) => {
      reject(error);
    });

     // Timeout to reject the promise if no response is received within 10 seconds
     setTimeout(() => {
      wsUserData.close(); // Close the WebSocket connection
      reject(new Error('Request timed out'));
    }, 10000);
  });
} 

i can’t see what is wrong here really. based on documentation this is how the request should be formated.

{
  "id": "aa62318a-5a97-4f3b-bdc7-640bbe33b291",
  "method": "order.status",
  "params": {
    "symbol": "BTCUSDT",
    "orderId": 12569099453,
    "apiKey": "vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A",
    "signature": "2c3aab5a078ee4ea465ecd95523b77289f61476c2f238ec10c55ea6cb11a6f35",
    "timestamp": 1660801720951
  }
}

documentation link

Using the same url with method “allOrders” i am able to get the data i want to

export async function getAllOrdersFromBinance(symbol: string): Promise<Order[]> {
  return new Promise( async (resolve, reject) => {
    const listenKey = await getUserDataStream();
    const wsUserData = new WebSocket(`${wsTestURL}/${listenKey}`);


    if (!testApiSecret) {
      throw new Error('No test API secret provided');
    }
    const requestId = uuidv4();
    wsUserData.on('open', () => {
      const timeStamp = Date.now();
      const queryString = `symbol=${symbol}&timestamp=${timeStamp}`;
      const signature = crypto.createHmac("sha256", testApiSecret).update(queryString).digest("hex");

      const params = {
        symbol: symbol,
        apikey: testApiKey,
        signature: signature,
        timestamp: timeStamp,
      }
      const message = {
        id: requestId,
        method: 'allOrders',
        params: params,
        limit: 500,
      } 
      wsUserData.send(JSON.stringify(message));
    });

    wsUserData.on('message', (message: string) => {
      const data = JSON.parse(message) as BinanceResponse;
      console.log('Received order status from binance:', data);
      if (data.id === requestId) {
        resolve(data.result);
      }
    });

    wsUserData.on('error', (error) => {
      reject(error);
    });
  });
} 

why would it work for one and not the other ?

You must sign the apiKey parameter. All parameters in the request must be signed (except for the signature, of course).

The parameters must also be sorted by name in the signed query string:

const queryString = `apiKey=${testApiKey}&symbol=${symbol}&orderId=${orderId}&timestamp=${timeStamp}`;
1 Like

@ilammy @dino Hi both, thanks for assisting me finding the issue. I was wrongly formatting my url addind the listen key behing it, This only required to open the userdataStream. I assumed it was mandatory going forward.
it was this

    const wsUserData = new WebSocket(`${wsTestURL}/${listenKey}`);

corrected to

    const wsUserData = new WebSocket(`${wsTestURL}`);

Also @ilammy you were correct about the importance of signing the api key value and sorting the value by name.

Thanks for your help