Get WhatsApp Messages Programmatically
This endpoint will let you get messages that you sent and received on your 2Chat-connected WhatsApp number. Only those messages sent/received after you made the connection to 2Chat will be returned.
Parameters
These are path parameters that should go in the URL you use to invoke the endpoint.
Parameter | Description | Example |
---|---|---|
your-number | The number you connnected to 2Chat. This parameter is required | +595981048477 |
remote-number | The number of your customer. This parameter is optional | +595981111111 |
We return the last 100 messages by default. If you would like to obtain older ones, you can add a page_number
as query parameter to go through older pages.
Getting all messages
Assuming +595981048477
is the number you connected to 2Chat, to get all messages you can call:
https://api.p.2chat.io/open/whatsapp/messages/+595981048477
.
This call will return the last or newest 100 messages you received on that number. If you would like to see older ones, you can start paginating the results:
- Page 1, from 100 to 200:
https://api.p.2chat.io/open/whatsapp/messages/+595981048477?page_number=1
. - Page 2, from 200 to 300:
https://api.p.2chat.io/open/whatsapp/messages/+595981048477?page_number=2
. - Page n: ...
Similarly, if you would like to see messages that +595981111111
sent to your 2Chat-connected WhatsApp number +595981048477
, you can do so by calling:
https://api.p.2chat.io/open/whatsapp/messages/+595981048477/+595981111111
.
And to see the page 1 of that call, you can also add the pagination parameter to the request:
https://api.p.2chat.io/open/whatsapp/messages/+595981048477/+595981111111?page_number=1
.
Invocation
- cURL
- Python
- NodeJS
curl --location --request GET 'https://api.p.2chat.io/open/whatsapp/messages/+595981048477' \
--header 'X-User-API-Key: your_api_key_here' \
--data-raw ''
import requests
url = "https://api.p.2chat.io/open/whatsapp/messages/+595981048477"
payload = ""
headers = {
'X-User-API-Key': 'your_api_key_here'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
var axios = require('axios');
var data = '';
var config = {
method: 'get',
url: 'https://api.p.2chat.io/open/whatsapp/messages/+595981048477',
headers: {
'X-User-API-Key': 'your_api_key_here'
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
Response
The API will return whether it succeded or not to queue the message for sending
{
"success": true,
"page_number": 0,
"messages": [
{
"uuid": "MSGaf1fa3a1-0d8d-4aa5-b117-f9425b651485",
"timestamp": 1670454681.0,
"session_key": "WW-WPN95841312-b54d-46e3-b0bc-6414f4a5296b-5215512345432@c.us",
"message": {
"media": {
"url": "https://2chat-user-data-dev.s3.amazonaws.com/ACC91be87af-5a29-4034-b599-342f2aeb5d52/accm/sfe4d7e29cd9dfad63b535dec54c5992055498dcd13cfc319f723c4b962b8803a.svg",
"type": "image",
"mime_type": "image/svg+xml"
}
},
"remote_phone_number": "+5215512345432",
"channel_phone_number": "+595981048477",
"sent_by": "api"
},
{
"uuid": "MSG8c39a0a9-01cf-46c9-ab46-3c0fb0477e99",
"timestamp": 1670378997.0,
"session_key": "WW-WPN95841312-b54d-46e3-b0bc-6414f4a5296b-5215512345432@c.us",
"message": {
"text": "you can see my replies here"
},
"remote_phone_number": "+5215512345432",
"channel_phone_number": "+595981048477",
"sent_by": "agent"
}
]
}
Field | Description |
---|---|
uuid | The unique identifier of the message |
timestamp | Timestamp of the message in UTC |
session_key | The chat session key that corresponds to this message |
message | The payload of the message |
message.text | The text content of the message, if present |
message.media.url | The 2Chat-hosted media file URL sent or received |
message.media.type | The type of media message |
message.media.mime_type | The MIME type of the message |
remote_phone_number | The WhatsApp phone number of the other end of the conversation |
channel_phone_number | The WhatsApp phone number where the message arrived |
sent_by | Who sent the message. E.g.: api , agent , user |