Complete guide to using MessageAgentKit API and MCP Server
MessageAgentKit provides a REST API and MCP (Model Context Protocol) server for integrating WhatsApp messaging capabilities into your applications. The API allows you to send text messages, template messages, schedule messages for approval, and retrieve message history.
https://your-domain.comAll API requests require authentication using a Bearer token in the Authorization header.
Authorization: Bearer YOUR_API_TOKENWhen calling MCP tools, include your API token as the api_token parameter in the tool arguments.
{
"tool": "send_text_message",
"arguments": {
"api_token": "YOUR_API_TOKEN",
"to": "5511999999999",
"text": "Hello!"
}
}/api/messages/textSend a simple text message to a WhatsApp number.
{
"to": "5511999999999",
"text": "Hello from MessageAgentKit!"
}| Parameter | Type | Required | Description |
|---|---|---|---|
to | string | Yes | WhatsApp number in E.164 format (country code + number, no + or spaces) |
text | string | Yes | Message content (max 4096 characters) |
{
"messaging_product": "whatsapp",
"contacts": [...],
"messages": [
{
"id": "wamid.xxx"
}
]
}/api/messages/templateSend a WhatsApp template message (must be pre-approved by WhatsApp).
{
"to": "5511999999999",
"template_name": "hello_world",
"language": "en_US",
"components": []
}| Parameter | Type | Required | Description |
|---|---|---|---|
to | string | Yes | WhatsApp number in E.164 format |
template_name | string | Yes | Name of your approved WhatsApp template |
language | string | Yes | Language code (e.g., en_US, pt_BR, es_ES) |
components | array | No | Template components for dynamic parameters |
/api/messages/schedule/textSchedule a text message for later approval. The message will appear in the dashboard for review before being sent.
{
"to": "5511999999999",
"text": "Scheduled message",
"scheduled_for": "2025-01-20T10:00:00Z"
}| Parameter | Type | Required | Description |
|---|---|---|---|
to | string | Yes | WhatsApp number in E.164 format |
text | string | Yes | Message content |
scheduled_for | string | Yes | ISO 8601 date string for when to send the message |
/api/messages/schedule/templateSchedule a template message for later approval.
{
"to": "5511999999999",
"template_name": "hello_world",
"language": "en_US",
"components": [],
"scheduled_for": "2025-01-20T10:00:00Z"
}/api/messages/listRetrieve the last 1000 messages sent through the API.
{
"messages": [
{
"sent_at": "2025-01-15 10:30:00",
"message_type": "text",
"recipient": "5511999999999",
"message_content": "Hello!",
"status": "sent",
"whatsapp_message_id": "wamid.xxx"
}
]
}/api/messages/scheduledList scheduled messages with optional filters.
| Parameter | Type | Description |
|---|---|---|
status | string | Filter by status: pending, approved, sent, failed, cancelled |
search | string | Search in recipient or message content |
start_date | string | Filter by scheduled date (ISO 8601) |
end_date | string | Filter by scheduled date (ISO 8601) |
/api/messages/scheduled/approveApprove and send multiple scheduled messages.
{
"message_ids": ["uuid1", "uuid2", "uuid3"]
}Get comprehensive analytics data for building charts and dashboards. Perfect for Lovable integration.
/api/analytics/usageReturns analytics data including daily message counts, hourly heatmap, totals, and success rate. Lovable can automatically build message analytics charts using this endpoint.
| Parameter | Type | Required | Description |
|---|---|---|---|
range | string | No | Time range: 7d, 30d, 90d (default: 7d) |
GET https://your-domain.com/api/analytics/usage?range=7d
Authorization: Bearer YOUR_API_TOKEN{
"totalMessages": 34,
"daily": [
{
"date": "2025-01-15",
"count": 10,
"sent": 9,
"failed": 1
},
{
"date": "2025-01-16",
"count": 24,
"sent": 24,
"failed": 0
}
],
"hourlyHeatmap": [
{
"hour": 9,
"dayOfWeek": 1,
"count": 5
},
{
"hour": 14,
"dayOfWeek": 1,
"count": 8
}
],
"successRate": 0.97
}| Field | Type | Description |
|---|---|---|
totalMessages | number | Total number of messages in the specified range |
daily | array | Array of daily message counts with sent/failed breakdown |
hourlyHeatmap | array | Array of hourly message counts by day of week (1=Monday, 7=Sunday) |
successRate | number | Success rate as decimal (0.0 to 1.0) |
The MCP Server exposes tools that can be called by AI agents (Claude, GPT, etc.) to interact with WhatsApp. All tools require an api_token parameter.
Send a text message immediately.
| Parameter | Type | Required | Description |
|---|---|---|---|
api_token | string | Yes | Your API token |
to | string | Yes | WhatsApp number in E.164 format |
text | string | Yes | Message content |
Send a template message immediately.
| Parameter | Type | Required | Description |
|---|---|---|---|
api_token | string | Yes | Your API token |
to | string | Yes | WhatsApp number in E.164 format |
template_name | string | Yes | Name of approved template |
language | string | Yes | Language code |
components | array | No | Template components |
Schedule a text message for later approval.
Same as send_text_message plus:
scheduled_for (string, required): ISO 8601 date stringSchedule a template message for later approval.
Same as send_template_message plus:
scheduled_for (string, required): ISO 8601 date stringAlways use E.164 format for phone numbers. This means:
5511999999999 for Brazil (+55 11 99999-9999)Always implement proper error handling:
try {
const response = await fetch('https://your-domain.com/api/messages/text', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_TOKEN',
'Content-Type': 'application/json'
},
body: JSON.stringify({ to: '5511999999999', text: 'Hello' })
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Request failed');
}
const data = await response.json();
console.log('Message sent:', data.messages[0].id);
} catch (error) {
console.error('Error sending message:', error);
// Handle error appropriately
}WhatsApp Business API has rate limits. Be mindful of:
Use scheduled messages for:
All endpoints return standard HTTP status codes:
| Status Code | Description |
|---|---|
200 | Success |
201 | Created (for scheduled messages) |
400 | Bad Request (missing or invalid parameters) |
401 | Unauthorized (invalid or missing API token) |
404 | Not Found |
500 | Internal Server Error |
{
"error": "Error message description"
}Ensure phone numbers are in E.164 format without + or spaces.
Templates must be approved by WhatsApp before use.
You've exceeded the messaging rate limit. Wait before retrying.
const sendWhatsAppMessage = async (to, text) => {
const response = await fetch('https://your-domain.com/api/messages/text', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.WHATSAPP_API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ to, text })
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error);
}
return await response.json();
};
// Usage
try {
const result = await sendWhatsAppMessage('5511999999999', 'Hello!');
console.log('Message ID:', result.messages[0].id);
} catch (error) {
console.error('Failed to send message:', error.message);
}import requests
import os
def send_whatsapp_message(to, text):
url = 'https://your-domain.com/api/messages/text'
headers = {
'Authorization': f'Bearer {os.getenv("WHATSAPP_API_TOKEN")}',
'Content-Type': 'application/json'
}
data = {'to': to, 'text': text}
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()
return response.json()
# Usage
try:
result = send_whatsapp_message('5511999999999', 'Hello!')
print(f"Message ID: {result['messages'][0]['id']}")
except requests.exceptions.RequestException as e:
print(f"Failed to send message: {e}")curl -X POST https://your-domain.com/api/messages/text \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"to": "5511999999999",
"text": "Hello from MessageAgentKit!"
}'{
"tool": "send_text_message",
"arguments": {
"api_token": "YOUR_API_TOKEN",
"to": "5511999999999",
"text": "Hello from MCP!"
}
}