binance icon indicating copy to clipboard operation
binance copied to clipboard

Cannot connect/subscribe to SpotUserDataStream on Testnet

Open ozum opened this issue 2 years ago • 2 comments

Hi,

When I try to connect/subscribe to SpotUserDataStream, I get an error message stating Invalid API key, IP, or permissions for action. (code: 2015).

const wsClient = new WebsocketClient({
  beautify: true,
  wsUrl: "wss://testnet.binance.vision",
  api_key: API_KEY,
  api_secret: API_SECRET,
});

await wsClient.subscribeSpotUserDataStream();

Workaround

I tracked the error,

In the websocket-client.js file the subscribeSpotUserDataStream() method calls the getSpotRestClient() method, which tries to connect to real network instead of testnet.

When I added the baseUrl option for the spot client to use testnet, the problem was solved. However:

  1. baseUrl is not a defined option in the WSClientConfigurableOptions
  2. This is undocumented.
const wsClient = new WebsocketClient({
  beautify: true,
  baseUrl: "https://testnet.binance.vision",
  wsUrl: "wss://testnet.binance.vision",
  api_key: API_KEY,
  api_secret: API_SECRET,
});

await wsClient.subscribeSpotUserDataStream();

ozum avatar Mar 08 '24 08:03 ozum

@tiagosiebler, in #422, we pushed an undo commit for the baseUrl attribute in the Web Socket client.

Did you reimplement baseUrl, or is this issue closed automatically by accident?

ozum avatar May 24 '24 05:05 ozum

Not yet, closed automatically by accident so I've reopened it, thanks

tiagosiebler avatar May 24 '24 10:05 tiagosiebler

+1

// passing ws client options =
{
beautify: true,
api_key: '---',
api_secret: '---',
wsUrl: 'wss://stream.testnet.binance.vision:9443'
}
 
// getting this error =
[
'Failed to connect to spot user data',
{
category: 'binance-ws',
error: {
code: -2015,
message: 'Invalid API-key, IP, or permissions for action.',
body: [Object],
headers: [Object [AxiosHeaders]],
requestUrl: 'https://api.binance.com/api/v3/userDataStream',
requestBody: undefined,
requestOptions: [Object]
}
}
]

Kos-M avatar Dec 07 '24 20:12 Kos-M

Actually i found a way to make it work in testnet , in case anyone else is trying..

Here it is:

    //
    const websocketOptions = {
      beautify: true,
      api_key: apiKey,
      api_secret: apiSecret,
      restOptions: {
        baseUrl: 'https://testnet.binance.vision',
      },
      wsUrl:  'wss://stream.testnet.binance.vision:9443' ,
    };
    this.binanceWs = new WebsocketClient(websocketOptions);
    this.binanceWs.subscribeSpotUserDataStream();
   //

Kos-M avatar Dec 08 '24 00:12 Kos-M

Will be supported in the next major release via #509

tiagosiebler avatar Feb 20 '25 11:02 tiagosiebler