Unable to send SMS to device using API from Python

Hello,

I am trying to use the API post methods to send an SMS to my device using a python script.

I am using the below script that I have made up. Python version is 3.7

import json
from urllib.request import Request, urlopen

HOLOGRAM_API_KEY = "MYAPIKEY"
url = "https://dashboard.hologram.io/api/1/sms/incoming?apikey="+HOLOGRAM_API_KEY

print (url)

values ={
  "deviceid": "xxxxx",
  "fromnumber": "+356xxxxxxxx",
  "body": "Hello world!"
}

hd = {"Content-Type": "application/json"}

req = Request(url, json.dumps(values).encode('ascii'), hd)

with urlopen(req) as response:
    json_response = json.load(response)
print(json_response)

This is the error that I am getting:

urllib.error.HTTPError: HTTP Error 400: Bad Request

Can someone guide me to what I am doing wrong please?

Thanks

Can you post the full response you get back? It should tell you which field is wrong for a 400 error.

Does this help?

https://dashboard.hologram.io/api/1/sms/incoming?apikey=xxxxxxxxxxxxxxxxxxxx
{'code': 400, 'msg': 'Bad Request', 'hdrs': <http.client.HTTPMessage object at 0x000001B779722308>, 'fp': <http.client.HTTPResponse object at 0x000001B77970D348>, 'file': <http.client.HTTPResponse object at 0x000001B77970D348>, 'name': '<urllib response>', 'delete': False, '_closer': <tempfile._TemporaryFileCloser object at 0x000001B7790D7108>, 'url': 'https://dashboard.hologram.io/api/1/sms/incoming?apikey=xxxxxxxxxxxxx'}

No… I think you want to do something like print(response.read(1000))

But I tried your script and it works for me which makes me think its something like the device ID being wrong or something else weird in the input.

You are right. It works, when using the correct device ID. :smile:

I was using a wrong ID and did not notice. Now I get a success response

{'success': True, 'data': {'msgcount': 1}}

Thank you for your help!

Great, you’re welcome