How to User Data Streams for Binance

I’m studying on binance-spot-api-docs/ at master · binance/binance-spot-api-docs · GitHub.
I tried this code:

export const postUserDataStream = () => {
  return``, {
    headers: { "X-MBX-APIKEY": apiKey },

and I get { code: -2014, msg: 'API-key format invalid.' }.
What’s the problem? How can I resolve?

The above code need to add null as second parameter for method.

Try the following code, I have confirmed it’s working:

const axios = require('axios');

const API_KEY = 'your_api_key';

const getListenKey = async () => {
  try {
    const response = await``, null, {
      headers: {
        'X-MBX-APIKEY': API_KEY,

    const { listenKey } =;
    console.log('WebSocket listenKey:', listenKey);
    return listenKey;
  } catch (error) {
    console.error('Error retrieving WebSocket listenKey:', error);
    throw error;


I get: msg: 'Invalid API-key, IP, or permissions for action.'.
I tried using both test and production APY_KEY. Maybe the API_KEY is different than SPOT API_KEY?

I forgot to add my IP to IP access restrictions on Binance site.

A last question about this topic. When I listen on “message” event for WebSocket I get a stream response but I can’t get the correct data. How to do this?

Sorry, what do you mean exactly by “you can’t get the correct data”? Are you subscribing to the correct Websocket Stream for your needs?

I found the solution. Once I get the response as stream I use KSON.parse(response.toString()) to get the json format of the response. Now the code works fine. Many thanks.