Login using the smk-python-sdk

Hi All, I’m trying to log in using the api package for python on github. I’m following the documentation
And I’m getting a weird module error if I use interactively inside a shell saying no module named ‘smarkets’ but aside from that if I follow the documentation on the sdk’s docs site and follow this block of code I get a timeout and an error saying unable to connect.

smarkets.streaming_api.exceptions.ConnectionError: Error connecting to api.smarkets.com:3801. timed out.
I’m using my email as username in the docs and reference it’s username as username. I asked the tech support team what my username is since I didn’t know if I had one or needed to check. I was told there is no usernames. Why not just say email instead of usernames since none exist? Is that the case?

Anyway this is what I tried from the SDK.
And got the error.

from smarkets.streaming_api.api import (
Session, SessionSettings, StreamingAPIClient)
username = ‘Foo’
password = ‘Barr’
settings = SessionSettings(username, password)
settings.host = ‘api.smarkets.com
session = Session(settings)
client = StreamingAPIClient(session)
s = client.login()
r = client.ping()
print(s,r)

I’ve also tried this and got a 405 error.
import requests
import json
username = ‘foo’
password = ‘barr’
url = ‘https://api.smarkets.com/v3/sessions
data = { ‘password’: password,
‘remember’: ‘true’,
‘username’: username,
}

page = requests.get(url,data)

print(page)

Can anyone give me a solution to logging in?
More preferably working solution to logging in with python SDK.

Thanks

Hi Tom,

The smk-python-sdk can only be used for order related operations (place and cancel bets); but in any case, in order to be able to do so you need a client token, that you can only be generated using the Smarkets API.

The Smarkets API documentation can be found here. At the moment we don’t have a SDK that wraps the Rest API, thought.

You can generate the session token as follows:

import requests 

headers = {"Content-Type": "application/json"}
data = {"username": "<username>", "password": "<password>"}                                        
response = requests.post("https://api.smarkets.com/v3/sessions/", headers=headers, data=data)                                    

session_token = response.json()["token"]

I hope this helps.

Isabel

Hi Isabel,

The code you posted has a key error.

'KeyError: ‘token’

Can you please check?

Hi Tom,

The key for the payload should be json instead of data. Also, please make sure that you are entering the correct credentials. Could you please try the code below?

import requests  
 
headers = {"Content-Type": "application/json"} 
data = {"username": "<username>", "password": "<password>"}                  
                       
response = requests.post("https://api.smarkets.com/v3/sessions/", headers=headers, json=dat
a)                                     
 
if response.status_code != 201: 
    print(response) 
else:  
    session_token = response.json()["token"]

Hi Isabel,

json instead of data fixed the issue.

Thanks for your help!