Search code examples
python-3.xalgorithmic-trading

API xStation xtb log in troubles


Someone knows how to log in to xtb API ? http://developers.xstore.pro/documentation/

Python request:

import requests, json

parameters = {
    "command" : "login",
    "arguments" : {
        "userId" : "10576375",
        "password": "PASSWORD"
    }
}

response = requests.get("https://xapia.x-station.eu:5124", params=parameters)
data = response.json()
print(data)

Response :

ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

Did I do something wrong?


Solution

  • It requires an SSL connection. Try the following (works for me):

    import socket
    import ssl
    import json
    
    host = 'xapia.x-station.eu'
    port = 5124
    USERID = 123456
    PASSWORD = 'YOURPASSWORD'
    
    host = socket.getaddrinfo(host, port)[0][4][0]
    
    s = socket.socket()
    s.connect((host, port))
    s = ssl.wrap_socket(s)
    
    parameters = {
        "command" : "login",
        "arguments" : {
            "userId": USERID,
            "password": PASSWORD
        }
    }
    packet = json.dumps(parameters, indent=4)
    s.send(packet.encode("UTF-8"))
    
    END = b'\n\n'
    response = s.recv(8192)
    if END in response:
        print('Print login: {}'.format(response[:response.find(END)]))
    
    parameters = {
        "command" : "logout"
    }
    packet = json.dumps(parameters, indent=4)
    s.send(packet.encode("UTF-8"))
    
    response = s.recv(8192)
    if END in response:
        print('Print logout: {}'.format(response[:response.find(END)]))