Sending SMS With Python
In this tutorial, you will use the Python module SinchSMS to send an SMS message with Sinch. With the Sinch SMS API, you can build anything from a simple notification service to two-factor authentication solutions.
Video
We have also turned this tutorial into a quick video walkthrough. Click on the image below to watch the tutorial.
For SMS pricing by destination, visit our pricing pages.
Setup
- Create a Sinch developer account
- In your developer dashboard, click “Apps” in the left-hand menu
- Click “Create new app”
- Name your app and click “Create”
- Take note of your app key and secret, you will need them in a few minutes
- Install the module using
pip install sinchsms
Sending an SMS via the API
Launch the interactive console by typing python
in your command line and type the below:
import time
from sinchsms import SinchSMS
number = '+yourmobilenumber'
message = 'I love SMS!'
client = SinchSMS(your_app_key, your_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)
while response['status'] != 'Successful':
print(response['status'])
time.sleep(1)
response = client.check_status(message_id)
print(response['status'])
If you don't want to use a module, you can find the source code for the module on GitHub: https://github.com/sinch/python-sinch-sms
What's next?
In the coming months, we will start supporting incoming SMS and have a packaged solution for verifying phone numbers through SMS and calling. Stay tuned.