Send a text message

Send a text message message in a chat or to a phone number.

post
sendMessage

https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number
Send a text message to a chat or to a phone number. Only one of the chatId or phone parameters needs to be specified. The message will be sent only if the phone is connected.
Request
Response
Query Parameters
instance
required
string
The instance number
api_token
required
string
The API token
Body Parameters
body
required
string
Message text, UTF-8 or UTF-16 string with emoji 🍏
phone
required
number
Required if chatId is not set. A phone number starting with the country code of the recipient of the message. USA example: 17472822486.
chatId
required
string
Required if phone is not set. Chat ID from the message list (like chat.chatId). Examples: 17633123456@c.us for private messages and 17680561234-1479621234@g.us for the group. Used instead of the phone parameter.
200: OK
In "data" there is a object like message that confirm you that the message is in sending. When the message is sent and/or received you can receive a notification on the url specified in the webhook.
{
"object": "message_whatsapp",
"data": {
"_id": "5cecf4bd9ce23ae3e3ccc1f5",
"message_id": "true_4455994885748@c.us_3EB0C6B50E34D57A0D9C",
"istance_number": "43279",
"body": null,
"body_translate": null,
"type": null,
"senderName": null,
"fromMe": null,
"author": null,
"time": null,
"chatId": null,
"messageNumber": null,
"queueNumber": 6,
"status_queued": true,
"status_queued_time": 1559033019,
"status_sent": null,
"status_sent_time": null,
"status_delivered": null,
"status_delivered_time": null,
"status_viewed": null,
"status_viewed_time": null
},
"has_more": false,
"count_api": 1,
"reset_count_api": 60
}
400: Bad Request
GENERIC ERROR

Example of body parameters

{
"phone":17472822486,
"body":"HI"
}
{
"chatId":"17472822486@c.us",
"body":"HI"
}

Example

cURL
C#
Go
Java
NodeJs
Objective-C
PHP
Python
Ruby
Swift
curl -X POST \
'https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number' \
-H 'Content-Type: application/json' \
-d '{
"phone":17472822486,
"body":"HI"
}'
var client = new RestClient("https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", "{\n\t\"phone\":17472822486,\n\t\"body\":\"HI\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
package main
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number"
payload := strings.NewReader("{\n\t\"phone\":17472822486,\n\t\"body\":\"HI\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"phone\":17472822486,\n\t\"body\":\"HI\"\n}");
Request request = new Request.Builder()
.url("https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number")
.post(body)
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
var request = require("request");
var options = { method: 'POST',
url: 'https://api.mercury.chat/sdk/v1/whatsapp/sendMessage',
qs: { api_token: 'tokenAPI', instance: 'instance_number' },
headers: { 'Content-Type': 'application/json' },
body: { phone: 17472822486, body: 'HI' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
#import <Foundation/Foundation.h>
NSDictionary *headers = @{ @"Content-Type": @"application/json" };
NSDictionary *parameters = @{ @"phone": @17472822486,
@"body": @"HI" };
NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
}
}];
[dataTask resume];
<?php
$request = new HttpRequest();
$request->setUrl('https://api.mercury.chat/sdk/v1/whatsapp/sendMessage');
$request->setMethod(HTTP_METH_POST);
$request->setQueryData(array(
'api_token' => 'tokenAPI',
'instance' => 'instance_number'
));
$request->setHeaders(array(
'Content-Type' => 'application/json'
));
$request->setBody('{
"phone":17472822486,
"body":"HI"
}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
import requests
url = "https://api.mercury.chat/sdk/v1/whatsapp/sendMessage"
querystring = {"api_token":"tokenAPI","instance":"instance_number"}
payload = "{\n\t\"phone\":17472822486,\n\t\"body\":\"HI\"\n}"
headers = {'Content-Type': 'application/json'}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
require 'uri'
require 'net/http'
url = URI("https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n\t\"phone\":17472822486,\n\t\"body\":\"HI\"\n}"
response = http.request(request)
puts response.read_body
import Foundation
let headers = ["Content-Type": "application/json"]
let parameters = [
"phone": 17472822486,
"body": "HI"
] as [String : Any]
let postData = JSONSerialization.data(withJSONObject: parameters, options: [])
let request = NSMutableURLRequest(url: NSURL(string: "https://api.mercury.chat/sdk/v1/whatsapp/sendMessage?api_token=tokenAPI&instance=instance_number")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()