Generating a listenkey using requests

Hello,

This part of my code is working fine and I am able to print the data as and when it comes.

class InterfaceWS:
    def __init__(self, exchange_name="BINANCE") -> None:
        self.exchange_name = exchange_name

    def connect(self):
        self.ws = websockets.connect(END_POINTS[self.exchange_name]["SPOT"]["TEST"]["ws"])

    def subscribe(self, symbol="btcusdt"):
        self.ws.send(
            json.dumps({"method": "SUBSCRIBE", "params": [f"{symbol}@bookTicker"],"id": 1,}))
        while True:
            response = self.ws.recv()
            print("tick\n", response)

However now i need to fetch user data and am unable to understand
#1 what is the purpose of a listenkey and
#2 how to get a listenkey from the api.

If you could kindly help me with some code that uses requests and not python-binance or python-binance-connector it would be very grateful.

Update: when i am trying to get new listenkey using Postman I am getting this error.
{"code":-2015,"msg":"Invalid API-key, IP, or permissions for action."}

  1. this repo has python code using basic libraries, it should be helpful.https://github.com/binance/binance-signature-examples/blob/master/python/spot.py

  2. listenkey is used a token to get user’s information by websocket.

  3. https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md#create-a-listenkey the document for creating the listenkey.

2 Likes