How to send SMS using Python

send-sms-using-python

Python is a much powerful and popular programming language through which you can do almost any programming job, no difference for sending SMS. In this article, I will share how you can send SMS, OTP, Voice calls, etc. to one or multiple phone numbers easily with Fast2SMS Python API.

Why do you need to send SMS with Python?

Let’s say you are working for an application where new user registration or any new action the user is trying to perform and you need to ensure that the action is secure (is done by the actual user). One of the ways to ensure the correct user is OTP.

Let’s think of another situation where you want to send promotional or notification messages to your user. In this case also you can use Python API to send quick message to your users through your application campaign. This is not the end, there are endless use of sending SMS whether it is text, OTP or Voice, which you can send easily using Python API.

About Fast2SMS

In this tutorial, I will use Fast2SMS to send SMS using Python. Fast2SMS is extremely easy to use and provides API to use their services with languages like Python, PHP, JAVA, C#, C, Ruby, JavaScript, and NodeJS.

I personally used Fast2SMS and I found it very useful. They charge ₹5 (Indian rupees) for sending each SMS and ₹0.10 (Indian rupees) for sending each voice message or voice call.

For voice call, they will charge ₹0.10 paisa if the voice length is less than 15 seconds. If it exceeds 15 seconds length then they will charge you multiplication or 15. That means for example if your voice length is 17 (15+2) seconds then also you need to pay two voice all charge which is (₹0.10*2 = ₹0.20).

Also Read:  What is the difference between ' ' and " " in python?

Before registration to Fast2SMS, let me clarify you one thing that they will give you ₹50 rupees free credit but that free credit will only work when you recharge ₹100 rupees. That means when you recharge ₹100 (with 18% GST = 118), you will get ₹150 in your available credit balance.

Send Quick SMS using Python and Fast2SMS

Before sending SMS with Fast2SMS you need to register there and recharge at least ₹100 (with 18% GST = 118). It is so simple, I think I do not need to tell you how to register and add a wallet balance there.

send-sms-and-voice-call-using-fast2sms-python-dev-api

Okay once you register there go to the DEV API section. Here you will find your API_KEY. Copy this API_KEY and paste into the below code.

# Send Quick SMS with Python and fast2sms API
import requests

url = "https://www.fast2sms.com/dev/bulkV2"

# Mention message text and phone number to send
payload = "message=This%20is%20a%20test%20message&language=english&route=q&numbers=9999999999"
headers = {
    'authorization': "YOUR_API_KEY",
    'Content-Type': "application/x-www-form-urlencoded",
    'Cache-Control': "no-cache",
    }

response = requests.request("POST", url, data=payload, headers=headers, verify=False)

print(response.text)
Output:
{"return":true,"request_id":"9s5i3tq42l7w6m8","message":["SMS sent successfully."]}

In the above code, at line 7, you need to mention your message text and the phone numbers where you want to send your message, it can be one or multiple.

Once you run this code the desired person will get a text message with the text “This is a test message” in his phone number. You can change this message and phone number for the SMS campaign.

Send Voice Calls using Fast2SMS Python API

Let’s now send a voice call to some numbers using Fast2SMS Python API. Before running any code please edit your desired phone number and API_KEY.

Also Read:  Understand Set implementation in Python with Example

Since this service is for OTP, you are only allowed to pass integer numbers. I tried with normal text but every time it was going for approval.

Maybe they made it because no one should send dirty or unethical voice messages to anyone. So you only can send numbers (they will convert it to voice using a text-to-voice algorithm).

# Send voice call using Python
import requests

url = "https://www.fast2sms.com/dev/voice"

payload = "variables_values=5599&route=otp&numbers=9999999999"
headers = {
    'authorization': "YOUR_API_KEY",
    'Content-Type': "application/x-www-form-urlencoded",
    'Cache-Control': "no-cache",
    }

response = requests.request("POST", url, data=payload, headers=headers, verify=False)

print(response.text)
Output:
{"return":true,"request_id":"w3hmgqek4dxrpf1","message":["Call sent successfully."]}
Output Audio Call

FAQs

Is there any free API to send SMS?

There are some other alternatives of Fast2SMS like twilio, seven io, etc. But as per my exploration, nothing is free. There can be some free trials for Twilio which I have not explored.

Is it possible to send multiple SMS using Python?

Yes, you can send SMS or voice call to one or multiple phone numbers using Fast2SMS Python API. Please check the code part above to understand how you can send multiple SMS with one click using Python.

Final Thought

For one of my application projects, I was exploring how to send SMS through Python. This article is the result of that exploration. In this article, I used Fast2SMS Python API to send SMS or Voice call but you can also explore other SMS sender providers like Twilio, seven io, way2sms, etc.

This is it for this article. If you have any questions or suggestions regarding this article, please let me know in the comment section below.

Leave a comment