Send WhatsApp Messages Programmatically
This endpoint will let you send messages to any WhatsApp-enabled phone number using your own number connected to 2Chat.
Trial accounts are rate-limited to 10 requests per minute (1 request every 6 seconds). If you would like a faster throughput, please upgrade your account.
Parameters
These parameters should be sent as JSON payload in the request body.
Field | Description | Example values |
---|---|---|
from_number | The number you have connected to 2Chat | +595981048477 |
to_number | The number you want to send your message to | +5215512345432 |
to_group_uuid | The WhatsApp group UUID if you intend to send it to a group | WGP768beeef-2b96-4bc7-9b7f-045078568723 |
text | The content of the message you want to send | Test from 2Chat API |
url | The URL of the media file you want to attach to the message. This value is optional | |
pin | Location coordinates. This value is optional |
Sending locations as map pins
In order to send a GPS location in the form of map pins, you will need to provide latitude
and longitude
values mandatorily, and name
, address
and url
optionally.
Example:
"pin": {
"latitude": "-25.7752926",
"longitude": "-56.647703",
"name": "Somewhere",
"address": "Some address 123",
"url": "https://2chat.co"
}
You can send text and multimedia messages, including audio, video, PDFs, images, etc., both to a single person or to a WhatsApp group.
If you are sending attachments using the url
field, make sure the attachment is publicly accessible and is not larger than 16 MB.
If you are using Google Drive or you are not sure if the URL is publicly accessible, download it and reupload it to 2Chat to make sure it is. Learn how here.
You can't use to_number
and to_group_uuid
in the same request. You can either send a message to a number or to a group
but not at the same time.
Learn more about where to get the group's UUID here.
Invocation
- cURL
- Python
- NodeJS
- PHP
curl --location --request POST 'https://api.p.2chat.io/open/whatsapp/send-message' \
--header 'X-User-API-Key: your_api_key_here' \
--header 'Content-Type: application/json' \
--data-raw '{
"to_number": "+5215512345432",
"from_number": "+595981048477",
"text": "Test from 2Chat API",
"url": "https://uploads-ssl.webflow.com/6281a9c52303343ff7c3b269/62a1648ee0273340bf38e3a9_logo-2C.svg"
}'
import requests
import json
url = "https://api.p.2chat.io/open/whatsapp/send-message"
payload = json.dumps({
"to_number": "+5215512345432",
"from_number": "+595981048477",
"text": "Test from 2Chat API",
"url": "https://uploads-ssl.webflow.com/6281a9c52303343ff7c3b269/62a1648ee0273340bf38e3a9_logo-2C.svg"
})
headers = {
'X-User-API-Key': 'your_api_key_here',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
var axios = require('axios');
var data = JSON.stringify({
"to_number": "+5215512345432",
"from_number": "+5215512345432",
"text": " "
});
var config = {
method: 'post',
url: 'https://api.p.2chat.io/open/whatsapp/send-message',
headers: {
'X-User-API-Key': 'your_api_key_here',
'Content-Type': 'application/json'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.p.2chat.io/open/whatsapp/send-message',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"to_number": "+5215512345432",
"from_number": "+595981048477",
"text": "Test from 2Chat API",
"url": "https://uploads-ssl.webflow.com/6281a9c52303343ff7c3b269/62a1648ee0273340bf38e3a9_logo-2C.svg"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-User-API-Key: your_api_key_here'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Results
The invocation above will send a text message along with a 2Chat logo as an attached image.
And that same message will appear in 2Chat (and in your WhatsApp app) as sent Via API
.
Response
The API will return whether it succeded or not to queue the message for sending
{
"success": true,
"message_uuid": "MSG205dec10-523d-4ab3-b739-fd56e7cdeba2",
"batched": true
}
Field | Description |
---|---|
success | true when the request was accepted and successful |
message_uuid | The internal UUID of the message you can use to keep track of it |
batched | true when the message was successfully queued for sending |