๐ฌ Sending WhatsApp Messages with Python using APIs
Imagine getting a WhatsApp message when your script finishes running, when a price drops, or when someone fills out a form.
With Python and WhatsApp APIs, you can send messages automatically to yourself or others — all from your code.
In this post, you’ll learn:
✅ How WhatsApp APIs work
✅ Free & official ways to send messages
✅ Code examples using Python
✅ Real-world automation ideas
๐ง WhatsApp API Basics
WhatsApp doesn’t offer direct access via smtplib
-like tools. Instead, you use:
-
WhatsApp Business API (official)
-
Third-party APIs (like Twilio)
-
Unofficial tools (like
pywhatkit
,selenium
)
For most users, Twilio is the easiest way to start legally and reliably.
๐ Option 1: Twilio WhatsApp API (Recommended)
Twilio is a trusted service that lets you send WhatsApp messages via its REST API.
๐งฐ Requirements
-
A free Twilio account
-
A verified phone number
-
Python +
twilio
package
๐ฆ Install Twilio SDK
pip install twilio
✉️ Sample Code
from twilio.rest import Client
# Twilio credentials
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
from_whatsapp_number = 'whatsapp:+14155238886' # Twilio sandbox number
to_whatsapp_number = 'whatsapp:+1234567890' # Your number with country code
client = Client(account_sid, auth_token)
message = client.messages.create(
body='๐ Your daily report is ready!',
from_=from_whatsapp_number,
to=to_whatsapp_number
)
print(f"✅ Sent message: {message.sid}")
๐ Sign up and get SID/Auth: https://www.twilio.com/console
๐ Sandbox setup: https://www.twilio.com/console/sms/whatsapp/learn
⚠️ WhatsApp Business API (Advanced & Official)
This is the enterprise-level solution by Meta for approved businesses. Features:
-
Two-way messaging
-
Brand-verified profiles
-
Requires Meta business verification
Use it if you're building:
-
A customer support chatbot
-
A booking system
-
Automated CRM outreach
๐ Learn more: https://business.whatsapp.com/products/platform
๐งช Option 2: pywhatkit
(Unofficial, Browser-Based)
pip install pywhatkit
import pywhatkit as kit
# Send a message at a scheduled time
kit.sendwhatmsg("+1234567890", "Hello from Python!", 15, 30)
⚠️ Requires a visible and logged-in WhatsApp Web session.
๐ง Real-World Use Cases
Use Case | Description |
---|---|
✅ Report Alerts | Send updates from a scraper or report generator |
๐ฆ Order Notifications | Confirm e-commerce orders |
⏰ Appointment Reminders | Notify customers or clients |
๐ Daily Summaries | Send dashboards to your phone |
๐ก️ Best Practices
-
Use official APIs like Twilio for production apps
-
Don’t spam — WhatsApp has limits and policies
-
Use environment variables or
.env
to protect credentials -
Add logging to monitor sent messages
๐ Automate It
Combine with:
-
schedule
orcron
for periodic updates -
Data pipelines or scraping tools
-
Contact forms or booking systems
Example: Send daily report every morning at 8 AM
import schedule
import time
def send_report():
# call Twilio API here
print("Sending daily WhatsApp message...")
schedule.every().day.at("08:00").do(send_report)
while True:
schedule.run_pending()
time.sleep(60)
๐ก Final Thoughts
Python + WhatsApp = powerful communication.
Whether you're a business or a solo builder, you can automate:
-
Customer messages
-
Real-time notifications
-
Custom alerts
-
Dashboard summaries
๐ Want to Go Further?
-
Build a full chatbot with Flask + Twilio
-
Add buttons, emojis, and formatting
-
Store logs of sent messages in Google Sheets or databases
-
Send dynamic attachments (like PDFs or Excel)