π¬ 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)