Authorization for placing an order

Hello,

I’ve just started here so am probably missing something obvious.

I’m trying to place my first order and am having difficulty understanding from the documentation and from the conflicting answers to queries here exactly how I should pass my session token for authorization. I copied and pasted a Python example but it did not work.

Here is a print of the exact headers string I have supplied to the headers parameter of the requests.post call.

{‘Content-Type’: ‘application/json’, ‘Authorization’: ‘User-Session-Token=[valid-session-token]’}

It is one of my many attempts, based on the answer to a query and a response. I have tried many others all fail

Please can you just give an example of the actual headers string required and I can then construct it in Python myself.

Thank you, Chris

Hi,

It should be as following:

{‘Content-Type’: ‘application/json’, ‘Authorization’: 'Session-Token [valid-session-token]’}

Thank you for your reply, I just tried it with this result.

{‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Session-Token [valid-session-token]’}

{‘data’: {‘description’: ‘Bad request syntax or unsupported method’}, ‘error_type’: ‘BAD_REQUEST’}

It’s rather hard to comment without seeing your full code. Can you please post it here?

Yes, thank you, again. I’m thinking the authorization is OK and it’s complaining about my parameters.

Here’s the code:

def placeorder(sessiontoken, marketid, contractid, price, stake):

    url = 'https://api.smarkets.com/v3/orders/'
    sessionstring = 'Session-Token ' + sessiontoken
    headers = {"Content-Type": "application/json", "Authorization": sessionstring}
    print(headers)
    params = '{"market_id": "' + str(marketid) + '", "contract_id": "' + str(contractid) + '", "price": "' + str(price) + '", "quantity": "' + str(stake) + '", "side": "buy"}'
    print(params)
    response = requests.post(url, headers=headers, params=params)
    reply = response.json()
    return reply

and here’s the console log

{‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Session-Token [valid-session-token]’}
{“market_id”: “9710368”, “contract_id”: “33845673”, “price”: “741”, “quantity”: “1.0”, “side”: “buy”}
{‘data’: {‘description’: ‘Bad request syntax or unsupported method’}, ‘error_type’: ‘BAD_REQUEST’}

Can you try converting both price and quantity to integers? These shouldn’t be strings.

Yes, I did that, similar result.

Then I changed the requests.post call to data=params not params=params.

And now it works!

Brilliant, thank you for your help.

1 Like