There are many instances when you may need to sent text messages to your customers or anyone – you can do this very easily using Textbelt and Python.
To do so, we would first need to get an API key from Textbelt. You can follow the steps below to get one:
Before moving to the code, lets first install the package required by running:
pip install requests
We are going to use the requests
package to hit the https://textbelt.com/text endpoint, that accepts a POST request, with the following parameters:
Now, take a look at the code:
import requestspayload = {'phone': '<PHONE_NUMBER>','message': '<MESSAGE>','key':'<API_KEY>'}resp = requests.post('https://textbelt.com/text', payload)print(resp.json())
Explanation:
In line 1, we import the required package.
In line 3, we create our payload
that will be sent to the Textbelt API.
In line 9, we create a POST request to our API and pass the payload
.
In line 11, we print the response from the API. Now, the endpoint will respond with the JSON:
success = true
.success = false
.For example, the response for a message that has been sent successfully would look like this:
{"success": true, "quotaRemaining": 40, "textId": 12345}
Another example of a response, when we have run out of quota, would look like this:
{"success": false, "quotaRemaining": 0, "error": "Out of quota"}
So, in this way, you can conveniently send text messages, worldwide, using the Python script.