How to Send WhatsApp API using Python | Quickstart

Introduction

In this tutorial, we will create simple examples to send messages via WhatsApp API using Python.

Initially, you must make sure that the http.client library is ready Because we will use this library In this article to send messages to the Ultramsg Gateway.

quicke Example usage for WhatsApp API using Python

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&body=WhatsApp API on UltraMsg.com works good&priority=10&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/chat", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

When the previous code is executed, and if the message is sent successfully, the response will be like this:

{"sent":"true","message":"ok","id":44897}

Send Image

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&image=https://file-example.s3-accelerate.amazonaws.com/images/test.jpg&caption=image Caption&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/image", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Send Document

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&filename=hello.pdf&document=https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/document", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Send Audio

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/audio", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Send Voice

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/voice", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Send Video

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&video=https://file-example.s3-accelerate.amazonaws.com/video/test.mp4&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/video", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&link=https://en.wikipedia.org/wiki/COVID-19&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/link", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Send Contact

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&[email protected]&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/contact", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Send Location

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&address=ABC company \n Sixth floor , office 38&lat=25.197197&lng=55.2721877&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/location", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Send Vcard

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&vcard= BEGIN:VCARD\nVERSION:3.0\nN:lastname;firstname\nFN:firstname lastname\nTEL;TYPE=CELL;waid=14000000001:14000000002\nNICKNAME:nickname\nBDAY:01.01.1987\nX-GENDER:M\nNOTE:note\nADR;TYPE=home:;;;;;;\nADR;TYPE=work_:;;;;;;\nEND:VCARD&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/vcard", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

using requests lib

We can also use requests Library.

Example usage for WhatsApp API using Python & requests lib

import requests
url = "https://api.ultramsg.com/instance16/messages/chat"
payload = "token=1v941eyo9eqixrsi&to=14155552671&body=test message;priority=1&referenceId="
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

finally, you can see Full Whatsapp API Documentation and FAQ.