python sms-02

NITHIN

Python SMS: How to Send Text Messages in Python Using D7 API

Learn How to Send SMS with Python using a D7 account Effortlessly.

In the ever-evolving landscape of programming, Python stands out as a versatile language capable of handling various tasks. One such task that developers frequently encounter is sending SMS messages using Python.

If you’re a developer looking to send text messages using Python, this guide is for you. We’ll walk you through how to build a robust SMS integration using the D7 Messaging API step by step.

Why Send Text Messages in Python?

Python is one of the world’s most popular programming languages, and for good reason. It’s simple, powerful, and has excellent support for external libraries.

When you send text messages in Python, you unlock capabilities like:

  • ✅ Sending personalized SMS at scale
  • ✅ Delivering OTPs instantly
  • ✅ Automating real-time notifications
  • ✅ Tracking delivery status via AP

Step-by-Step Guide: Send Text Messages in Python

Sign up for (or log in to) your D7 account

If you have a Verified D7 account with SMS capabilities, you’re all set! Feel free to jump straight to the code.

If you’re brand new to D7, you can sign upfor a free trial account to get started. Once you've signed up, Claim your free credits with OTP verification and create a D7 application to get an API Token. You will need those values for the code samples below.

Now that you have a D7 application you can start sending messages to mobile devices.

Send an SMS message in Python via the REST API

To send an outgoing SMS message from your D7 account you’ll need to make an HTTP POST to D7.

Direct7 Python package helps you to create a new instance of the Message resource, specifying the recipients, content, originator, report_url, and Unicode parameters of your message.

If you don’t already have the Python package installed you can install it using pip:pip install direct7

Now, create a file named send_sms.py and include the following code:

from direct7 import Client
client = Client(api_token=”API token")
client.sms.send_message(recipients = ["+97150900XXXX","+97845900XXX"], content = "Greetings from D7 API", originator = "SignOTP", report_url = "https://the_url_to_recieve_delivery_report.com", unicode = False)

Replace the placeholder values for the API token with your unique values. You can find these in your API tokens in the d7 dashboard.

You’ll tell Direct7 which Sender/Header to use to send this message by replacing the originator. You can use your brand name with a maximum character limit of 11 or your mobile number with your country code. If you would like to register a new Sender ID, you can submit your details here.

You can also set the Default Originator here, and all your messages will be prefixed with the given originator.

Next, specify yourself as the message recipient by replacing the recipient's number with your mobile phone number. Both of these parameters must use a country code prefix (+ and a country code, e.g.,+16175551212). You can also set the Default Country here, and all your messages will be prefixed with the selected country code.

We also include the content parameter, which contains the content of the SMS we’re going to send. A long message over 160 characters will be split into multiple messages with 153 characters each and forwarded to telecom. When Unicode messages exceed 70 characters, they are split into multiple messages of 63 characters each. Also, billing will be based on the number of message parts sent.

You can also send normal text and Unicode messages by setting the parameter unicode as true or false. Set as false for normal GSM 03.38 characters(English, normal characters). Set as true for non-GSM 03.38 characters (Arabic, Chinese, Hebrew, Greek-like regional languages, and Unicode characters).

You can set the report_url parameter to receive delivery status (DLR) for your message, and specify the callback server URL where you want to receive the message status updates using the report_url parameter. When the delivery status changes, the status updates will be sent to the specified URL. For information on the format of the DLR message, please refer to the "Receiving DLR" section.

Once you’ve updated the code sample, you can test it out by running it from the command line: python send_sms.py

In just a few moments you should receive an SMS!

Let’s take a moment to understand what’s going on behind the scenes when you send this request to D7.

Direct7's response

When Direct7 receives your request to send an SMS via the REST API, it will check that you’ve included a valid API token. Direct7 will then either queue the SMS or return an HTTP error in its response to your request.

When the request is validated, request_id, status, and created time will be returned. A sample response might look like this

{     	 "request_id": "d9835609-a4e0-10ea-a26a-eeafbe700fef",
	"status": "accepted",
	"created_at": "2001-08-05T12:52:38.393Z"}

To see the status of your message, include the following code with the above request_id.

from direct7 import Client
client = Client(api_token="API token")
client. sms.get_status(request_id="request_id")

Key Parameters Explained

  • recipients: List of mobile numbers in international format (e.g., +9715XXXXXXX)
  • content: Your SMS message
  • originator: Sender ID (your brand name or number)
  • report_url: (Optional) URL to receive delivery status updates
  • Unicode: Set to True for languages like Arabic, Hindi, Chinese, etc.

SMS Length & Encoding: What You Should Know

🧾 GSM messages: Up to 160 characters
🈷️ Unicode messages: Up to 70 characters
📏 Long SMS: Automatically split into parts

  • GSM: 153 characters per part
  • Unicode: 63 characters per part

💰 Billing: Based on message parts sent

Best Practices for Sending SMS in Python

Now that you have a working code snippet, let's explore some best practices to enhance the effectiveness of your SMS-sending application.

Error Handling

Implement robust error handling to gracefully manage potential issues, such as network errors or D7 API failures.

Message Personalization

Personalize your messages to create a more engaging user experience. Include dynamic content and user-specific information to make your SMS stand out.

Rate Limiting

Be mindful of rate limiting imposed by D7 API to avoid disruptions. Adjust your sending frequency to comply with D7's guidelines.

Real-World Use Cases of Python SMS Integration

Python is widely used across industries to send SMS for:

  • 🛍️ E-commerce: Order confirmations, shipping alerts
  • 🏦 Banking: OTPs, fraud warnings
  • 🏥 Healthcare: Appointment reminders
  • 📚 EdTech: Class notifications, exam reminders
  • 🧑‍💻 SaaS Platforms: Onboarding, usage alerts

In conclusion, sending SMS in Python can be a seamless process with the right tools and knowledge. By leveraging the power of the D7 library, you can incorporate SMS functionality into your Python applications effortlessly. Remember to follow best practices, personalize your messages, and handle errors gracefully to create a reliable and user-friendly SMS-sending solution.

Want to add real-time SMS messaging to your Python app?

It’s easier than ever to send text messages in Python using the D7 Messaging API.
🚀 Start your integration today!

🔗Start Sending SMS with Python Now
Explore full documentation and generate your API keys in minutes.


bg-img-left bg-img-left

Sign Up and Try D7 API for Free

Start today and enhance your communication workflows.