Send a Text Message using Python in 15 seconds

So you are a Python guru or maybe just getting started and you want to do something out of the ordinary by allowing your app to communicate with users real time.

You could build an app that sends texts, maybe for an event promoter, they usually want to promote events on the weekend. Let’s say your app wants to record users responses e.g.

Your App texts the user: Hey what did you eat today?

The user responds via text: I had a salad for lunch and pizza for dinner.

This response via text can be recorded in the DB and your app could keep a log of what your customer eats everyday

You could just build a simple app that texts your friends once a week to stay in touch.

The possibilities are endless as you can see

To send a text programmatically your code needs to make an XHR request to an SMS gateway that will read the text details from your request and then initiate a text to the recipient’s phone number

PureText offers a Texting/SMS gateway to which you can make an HTTP GET/POST request with details such as recipient number and body of the text and PureText will deliver the text to the recipient for you.

In order to identify that this request belongs to you, you’ll need to include your API token in the HTTP GET/POST request. Register for a free API token here

A thing to note is that you cannot just use any phone number as the ‘From’ number. The rule of thumb is the number must be registered through the same company who’s SMS gateway you choose to use.

To send a text message using Node.js you don’t need a dedicated short code. Dedicated short codes are expensive and take 2-3 months to get approved, instead, you can now use local 10 digit VOIP numbers or also referred to as long codes to send a  text/SMS programmatically from your Node.js app

So let’s get started, to do a quick and dirty POC you’ll need:

  1. An API token key  (free API token here)
  2. A ‘From’ number through your SMS gateway (Get a free one here)

Copy Paste the following code in your IDE and replace the apiToken and fromNumber values with the API token you will see in your dashboard and the number you will or have reserved on the numbers page in PureText

   
# importing the requests library 
import requests 
  
# defining the api-endpoint  
API_ENDPOINT = "https://api.puretext.us/service/sms/send"
  
# your API key here 
API_KEY = "XXXXXXXXXXXXXXXXX"
  
# data to be sent to api 
data = {'apiToken':API_KEY, 
        'fromNumber':'+1xxxxxxxxxx', 
        'toNumber':'+X-XXX-XXX-XXXX', 
        'smsBody':'SENDING TEXT USING PYTHON'} 
  
# sending post request and saving response as response object 
r = requests.post(url = API_ENDPOINT, data = data) 
  
# extracting response text  
pastebin_url = r.text