Sending WhatsApp Messages with Python using APIs

πŸ’¬ 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:

  1. WhatsApp Business API (official)

  2. Third-party APIs (like Twilio)

  3. 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 or cron 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)


Python

Machine Learning