If you are interested in Send SMS in Python Using MSG91 for handling OTPs, notifications, or alerts, this tutorial will show you everything step by step. You will discover how to integrate your Python script with the MSG91 SMS gateway, perform a single SMS transmission, then expand to basic bulk operations.
There are many small companies and side projects that make use of SMS as a time-sensitive notification service. Using the send SMS using python msg91 api, you can set up notifications based on user sign-ups, payments made for an order, appointments fixed, or the time a server goes down.
Why use MSG91 with Python?
Python suits automation, and MSG91 handles reliable SMS delivery. By using them together, you can get scale able tool for your requirements like:
- Send login OTPs and password reset links
- Share order and delivery updates
- Remind users about appointments or due dates
- Alert admins about errors, downtime, or important events
With Python and MSG91, you control everything in code, you decide when and how messages go out. You also keep it easy to maintain, even if your project grows.
Prerequisites
Before you follow this MSG91 python integration tutorial, make sure you have MSG91 account and request library installed into your local system. If not, You can follow below steps.
Get your MSG91 API credentials
Before moving further with example, you need API credentials from MSG91. You can create these credentials for your python project using following below simple steps.
- Log in to your MSG91 account
- Go to the API or Developer section
- Copy your Auth Key (API key)
- Note your sender ID and route if needed
- Confirm your SMS template if your account requires it
You use these values inside your Python script, so keep them safe and private.
Install Request Library
The requests library handles HTTP request or external API calls from your script with easily. To install it into your local system, open terminal and enter below command:
pip install requests
Basic Python MSG91 SMS API example
Let’s start with simple example. In this example, you see a simple msg91 api python code example that sends a single SMS to one mobile number. This shows you how to send sms in python programmatically with very little code.
Create a file named send_single_sms.py:
import argparse
import requests
def main():
parser = argparse.ArgumentParser(description="Send SMS using MSG91")
parser.add_argument("number", help="Mobile number with country code, e.g. 919999999999")
args = parser.parse_args()
auth_key = "YOUR_AUTH_KEY"
sender_id = "SENDERID"
route = "4"
country = "91"
url = "https://api.msg91.com/api/v2/sendsms"
payload = {
"sender": sender_id,
"route": route,
"country": country,
"sms": [
{
"message": "Hello from Python and MSG91",
"to": [args.number]
}
]
}
headers = {
"authkey": auth_key,
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.status_code)
print(response.text)
if __name__ == "__main__":
main()
Run it with below command in your command prompt:
python send_sms.py 919999999999
Here, replace this number with your desired number to send SMS. If your credentials stay correct and your MSG91 account allows SMS to that number, you should receive the test message in a few seconds.
Send bulk SMS using Python script and MSG91
Now let’s move from a single user to many users. With MSG91 and Python, you can send personalised or generic bulk SMS in one request. This helps when you run campaigns, reminders, or announcements.
Here is a simple send bulk sms using python script example:
import requests
auth_key = "YOUR_AUTH_KEY"
sender_id = "SENDERID"
route = "4"
country = "91"
mobile_numbers = [
"919111111111",
"919222222222",
"919333333333"
]
message = "Weekend sale live now! Check your account for details."
url = "https://api.msg91.com/api/v2/sendsms"
payload = {
"sender": sender_id,
"route": route,
"country": country,
"sms": [
{
"message": message,
"to": mobile_numbers
}
]
}
headers = {
"authkey": auth_key,
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.status_code)
print(response.text)
It will send same message to each user automatically. However for sending customized message, you can loop it with changing custom params like name.
Handle errors and responses cleanly
When you Send SMS in Python Using MSG91, you should also handle failures gracefully. Maybe the number is invalid, the API key expired, or your MSG91 balance dropped.
import requests
def send_sms(payload, auth_key):
url = "https://api.msg91.com/api/v2/sendsms"
headers = {
"authkey": auth_key,
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
print("SMS request accepted")
else:
print("SMS request failed")
print(response.status_code)
print(response.text)
You can log failed responses, retry important alerts, or send yourself an email when something breaks. This approach keeps your messaging reliable, even as your system grows.
Conclusion
You now understand how to send sms programmatically using python MSG91 for both single messages or bulk messages. You have viewed a basic msg91 API example in python, learned how to create a python sms gateway API example, as well as learned to personalize messages for real people in real scenarios.
With Send SMS in Python Using MSG91, you get a highly flexible and powerful tool to communicate with customers in various international countries. Just begin with a single script, test it on your own number, and then use it in your application for sign-ups, orders, alerts, and reminders.
