How to Send WhatsApp Messages from a Python Script
By 5MinutesAPI Engineering•
Automating Alerts with Python
Python is the go-to language for data science, server monitoring, and automation scripts. Whether you want your server to ping you on WhatsApp when CPU usage spikes, or you want to build a Python-based CRM that messages customers, integrating the WhatsApp API is incredibly simple with 5MinutesAPI.Prerequisites
- Python 3.x installed on your machine.
- The
requestslibrary (install viapip install requests). - An active API Key from the 5MinutesAPI dashboard.
The Python Code
Because 5MinutesAPI provides a clean REST endpoint, you don't need heavy third-party SDKs. A simple HTTP POST request does the heavy lifting.
import requests
import json
url = "https://api.5minutesapi.com/v1/send-message"
api_key = "5m_live_YOUR_SECURE_KEY"
payload = json.dumps({
"to": "12025550178",
"whatsapp_template": "system_alert",
"variables": ["CPU Spike Detected", "98%"]
})
headers = {
'Authorization': 'Bearer ' + api_key,
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, data=payload)
print(response.status_code)
print(response.json())
Handling the Response
The API will instantly return a JSON object confirming that the message has been queued by our Golang routing engine and dispatched to Meta. If the phone number is invalid, our system will capture the Meta webhook and automatically refund your account balance.
Python makes it incredibly easy to wrap this logic into reusable functions, allowing you to trigger global WhatsApp alerts from anywhere inside your automation pipeline.