Sending an SMS message from Python is simple. Using the Voximplant platform, you just need to setup some credentials, pick a phone number, and use a simple API command. In this tutorial we will show how to make a simple command line application that lets you send a message of your choice to any SMS enabled number.
Step 1. Get Voximplant Credentials
Local telecom carriers charge money to terminate SMS and spammers can abuse the system, so you will need some credentials before you are allowed to send a message. If you don’t have a Voximplant account, you can sign-up for one at https://voximplant.com/sign-up/.
Then head to the Service Accounts section inside settings from the menu on the left.
You can optionally limit the role of the service account and give it a description. Click Generate Key and you will be asked to save a JSON file with your credentials.
Keep this file secure - it gives access to your Voximplant account. You will need to delete the key and repeat this procedure to create a new one if you lose it or want to change the credentials.
This example assumes you have it in the same folder as your main Python SMS app.
See here for more information on Voximplant’s service accounts.
Step 2. Pick a phone number
You will need to have a phone number to send an SMS. Voximplant let’s you choose one from nearly 60 countries. On the Voximplant control panel, select My phone numbers from the menu on the left and then select Buy new phone number in the upper right corner.
The phone number interface will be displayed. You’ll need to select one or more real numbers and click Buy.
After you’ve bought a number, you'll immediately see this number on the list.
Note Voximplant also offers free test numbers, but these will not work with the SMS service.
Now edit that number, and click the enable SMS slider and save:
Now you have an SMS enabled number!
See more on choosing an SMS number and even doing that programmatically here.
Step 3. Get the Voximplant API Client Library
We will use Python in this example, but you can follow a similar procedure for any of Voximplant’s other API client libraries -- node.js, php, .NET, and Go. See here for more on integrating with Voximplant’s API clients.
Python project setup
Voximplant’s Python API Client Library works with both Python 2.x and 3.x. You will need setuptools
>= 18.5 installed.
Go to your project folder and install the SDK using:python -m pip install --user voximplant-apiclient
Python script
Now let’s make a simple Python application where you specify the destination number and message as command line parameters using your favorite editor.
import sys
from voximplant.apiclient import VoximplantAPI, VoximplantException
if __name__ == "__main__":
api = VoximplantAPI("c23057e8-35be-KEEP_THIS_SECURE_private.json")
SOURCE = "14388002812" # Enter your Voximplant phone number here
DESTINATION = sys.argv[1]
SMS_BODY = ' '.join(sys.argv[2:])
try:
print('Sending "{}" to {} '.format(SMS_BODY, DESTINATION))
res = api.send_sms_message(SOURCE, DESTINATION, SMS_BODY)
print(res)
except VoximplantException as e:
print("Error: {}".format(e.message))
Make sure you enter the phone number you purchased in step 2 for the `SOURCE` variable.
Testing
Now just run your script:
python sms.py 1442038083060 Hello from Voximplant!
The console will read:
Sending "Hello from Voximplant!" to 1442038083060
{'result': 1, 'transaction_id': 924902460004, 'fragments_count': 1}
You should get a SMS response:
High Volume A2P Messaging
https://voximplant.com/docs/references/httpapi/sms#a2psendsms
Other tricks
High Volume A2P Messaging
SendSmsMessage
is intended for low volume, peer-to-peer transactions. For higher volume use cases, Voximplant also has a dedicated Application 2 Person (A2P) messaging API. To send higher volumes of messages at the same time and features like alphanumeric sender, you should use the A2PSendSms API.
Note: this API is restricted and not available by default. Email support@voximplant.com for access.
Long messages
If your message is longer than 140 characters, Voximplant will automatically break these into multiple fragments:
python sms.py 1442038083060 random: avL4GRT8qEGvbkYNidv26oE61CvuywxOrruCHJOgE9idtettAmiFWQ0zO600OCw8dszwgmY27p321HBBoeQYkKDR6tzwA9UPKixUYvQWRXmdxoGdcYQ7gJ0PBpzs8QEPeXUAQl35Vt1MIzaQY4AZSHIOYQQjCAZkujWVRJZEvOBNNQyXxvzGZh3hMALdmzJy2YUhHTqN
Sending "random: avL4GRT8qEGvbkYNidv26oE61CvuywxOrruCHJOgE9idtettAmiFWQ0zO600OCw8dszwgmY27p321HBBoeQYkKDR6tzwA9UPKixUYvQWRXmdxoGdcYQ7gJ0PBpzs8QEPeXUAQl35Vt1MIzaQY4AZSHIOYQQjCAZkujWVRJZEvOBNNQyXxvzGZh3hMALdmzJy2YUhHTqN" to 16174018889
{'result': 1, 'transaction_id': 923383490004, 'fragments_count': 2}
Simplifying credentials
Instead of linking to your credentials with api = VoximplantAPI(credentials.json)
, you can also use the VOXIMPLANT_CREDENTIALS
environment variable:
export VOXIMPLANT_CREDENTIALS=/Users/demo/voximplant_python_sms/c23057e8-35be-KEEP_THIS_SECURE_private.json
Receiving SMS
Voximplant uses a callback mechanism when an inbound message is sent to your SMS number. See our How to receive SMS guide for that.
Using CURL instead
For debugging and quick scripting, sometimes it is helpful to use curl from the command line. The client libraries make accessing Voximplant easy, but you can also initiate HTTP API commands via curl:
curl https://api.voximplant.com/platform_api/SendSmsMessage/?account_id=1&source=14388002812&destination=1442038083060&sms_body=Hello%20from%20Voximplant! -H "$(bash jwt.sh credentials.json)"
See the guide for using CURL with Voximplant here and the HTTP SMS methods with examples for curl and all the HTTP API client libraries here.
So much more
There is so much more to the Voximplant platform. See our getting started guide and documentation for more details.