I want to send sms through sinch. I registered and obtained key and secret from Sinch website. But my code to send message is not working.
import time
from time import sleep
from sinchsms import SinchSMS
# function for sending SMS
def sendSMS():
# enter all the details
# get app_key and app_secret by registering
# a app on sinchSMS
number = '+91XXXXXXXXX'
app_key = 'KEY_ID'
app_secret = 'Key_secret'
# enter the message to be sent
message = 'Hello Message!!!'
client = SinchSMS(app_key, app_secret)
print("Sending '%s' to %s" % (message, number))
response = client.send_message(number, message)
message_id = response['messageId']
response = client.check_status(message_id)
# keep trying unless the status returned is Successful
while response['status'] != 'Successful':
print(response['status'])
time.sleep(1)
response = client.check_status(message_id)
print(response['status'])
if __name__ == "__main__":
sendSMS()
I tried with and without country code. I am getting the following error
Sending 'Hello Message!!!' to +91XXXXXXXXXX
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
<ipython-input-47-97ce28144276> in <cell line: 35>()
34
35 if __name__ == "__main__":
---> 36 sendSMS()
8 frames
/usr/lib/python3.10/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
641 class HTTPDefaultErrorHandler(BaseHandler):
642 def http_error_default(self, req, fp, code, msg, hdrs):
--> 643 raise HTTPError(req.full_url, code, msg, hdrs, fp)
644
645 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 401: Unauthorized
This is actually my private account but I work for Sinch, and in our team we have an app which utilizes Sinch SMS API. Our customers send thousands of SMS messages through Sinch SMS on daily basis.
The error you are getting:
Therefore most likely either your app_key or app_secret is incorrect. My suggestion is to double check these from the Dashboard. I think this is a good document: https://developers.sinch.com/docs/sms/getting-started/python/python-send-sms/
Sidenote 1: I see you are on Python and using the SinchSMS helper from https://github.com/sinch/python-sinch-sms. It's also quite easy to work without this extra helper (not many more code lines added), if you want to reduce your dependencies.
Sidenote 2 about country code: It is my experience that you should always include the leading plus when sending out SMS through Sinch SMS. In my tests it worked also without the plus, however if the recipient replies, the sender number will be according to E.164 and includes the plus. So, if in your app you want to match the outgoing and incoming messages (to maybe create chat-like chained experience for user), it's a bit easier when you always use E.164.