How to send polls to WhatsApp Groups via API
Learn how to send poll messages to WhatsApp Groups using the Wassenger API, which makes it super easy to automate WhatsApp communication for your business and customers.
Let’s take a closer look at WhatsApp polls, from exactly how they work, to the best ways businesses can use polls in their marketing strategies and automation from the Wassenger API.
🤩 🤖 Wassenger is a complete communication platform and API solution for WhatsApp. Explore more than 100+ API use cases and automate anything on WhatsApp by signing up for a free trial and getting started in minutes!
Requirements
- To have a WhatsApp number already linked to the platform and online.
- Group WhatsApp ID (WID) that you can find in two ways:
How to obtain the Group WhatsApp ID
You can obtain the Group WhatsApp ID by using one of these methods:
- Web: go to number’s settings > Groups > Copy the Group WID.
- API: query the available groups in your number using this endpoint.
API endpoint
We will use the following API endpoint to send a new poll:
Prepare the request
Target API URL using the POST method
https://api.wassenger.com/v1/messages
Required HTTPS headers > Obtain your API key here
Content-Type: application/json
Token: $API_TOKEN
Use the body in JSON format to send the new poll
{
"group": "$GROUP_ID@g.us",
"poll": {
"name": "Which product of ours do you use the most?",
"options": [
"Product A - Wireless Headphones",
"Product B - Smartwatch",
"Product C - Fitness Tracker",
"Product D - Bluetooth Speaker",
"Product E - Smartphone Charger",
"Product F - Laptop Stand",
"Product G - Home Security Camera",
"Product H - Smart Light Bulb",
"Product I - Portable Power Bank"
]
}
}
🖥️ Looking for a code example? Go to the API live tester and get ready-to-use code examples in 15+ programming languages, including Python, JavaScript, PHP, C#, Java, Ruby, Go, Powershell, cURL and more.
Send polls using code
Explore how to use the code in your browser without installing any software.
Also, you can find different languages you can test on Replit.com:
# Examples requires to have installed requests Python package.
# Install it by running: pip install requests
import requests
url = "https://api.wassenger.com/v1/messages"
payload = {
"group": "$GROUP_ID@g.us",
"poll": {
"name": "Which product of ours do you use the most?",
"options": ["Product A - Wireless Headphones", "Product B - Smartwatch", "Product C - Fitness Tracker", "Product D - Bluetooth Speaker", "Product E - Smartphone Charger", "Product F - Laptop Stand", "Product G - Home Security Camera", "Product H - Smart Light Bulb", "Product I - Portable Power Bank"]
}
}
headers = {
"Content-Type": "application/json",
"Token": "API TOKEN GOES HERE"
}
response = requests.post(url, json=payload, headers=headers, params=querystring)
print(response.json())
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.wassenger.com/v1/messages",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'group' => '$GROUP_ID@g.us',
'poll' => [
'name' => 'Which product of ours do you use the most?',
'options' => [
'Product A - Wireless Headphones',
'Product B - Smartwatch',
'Product C - Fitness Tracker',
'Product D - Bluetooth Speaker',
'Product E - Smartphone Charger',
'Product F - Laptop Stand',
'Product G - Home Security Camera',
'Product H - Smart Light Bulb',
'Product I - Portable Power Bank'
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Token: API TOKEN GOES HERE"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!
// Examples requires to have installed pecl_http package, a simple and elegant HTTP client for PHP.
// Install it by running: pecl install pecl_http
// More information: https://pecl.php.net/package/pecl_http/3.2.0
<?php
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->append(json_encode([
'group' => '$GROUP_ID@g.us',
'poll' => [
'name' => 'Which product of ours do you use the most?',
'options' => [
'Product A - Wireless Headphones',
'Product B - Smartwatch',
'Product C - Fitness Tracker',
'Product D - Bluetooth Speaker',
'Product E - Smartphone Charger',
'Product F - Laptop Stand',
'Product G - Home Security Camera',
'Product H - Smart Light Bulb',
'Product I - Portable Power Bank'
]
]
]));
$request->setRequestUrl('https://api.wassenger.com/v1/messages');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setHeaders([
'Content-Type' => 'application/json',
'Token' => 'API TOKEN GOES HERE'
]);
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
// This code requires you to have installed Unirest package.
// Documentation: https://kong.github.io/unirest-java/#requests
// Installation: http://kong.github.io/unirest-java/
HttpResponse<String> response = Unirest.post("https://api.wassenger.com/v1/messages")
.header("Content-Type", "application/json")
.header("Token", "API TOKEN GOES HERE")
.body("{\"group\":\"$GROUP_ID@g.us\",\"poll\":{\"name\":\"Which product of ours do you use the most?\",\"options\":[\"Product A - Wireless Headphones\",\"Product B - Smartwatch\",\"Product C - Fitness Tracker\",\"Product D - Bluetooth Speaker\",\"Product E - Smartphone Charger\",\"Product F - Laptop Stand\",\"Product G - Home Security Camera\",\"Product H - Smart Light Bulb\",\"Product I - Portable Power Bank\"]}}")
.asString();
Need more? Explore all our WhatsApp poll examples and go all in!
- 🚀 Send a poll message to a user
- 📈 Send a poll to a group
- 📈 Schedule a poll message to be sent in the future
- ⚡ Send poll to a channel
- 🔥 Send votes on an existing user poll
- 🏆 Send votes on an existing group poll
- 🌟 Remove votes on an existing user poll
🤩 🤖 Wassenger is a complete communication platform and API solution for WhatsApp. Explore more than 100+ API use cases and automate anything on WhatsApp by signing up for a free trial and getting started in minutes!
Live testing using the API
FAQs
I have multiple numbers connected: how do I send messages through a specific number?
If you have multiple numbers connected to your account, you need to specify the device
field in the JSON body with the target WhatsApp number device ID (24 characters hexadecimal value) you want to send the messages through.
If the device
field is not specified, messages will be sent through the first connected WhatsApp number in your account.
Here is an example of how to send a message through a specific WhatsApp number
How to send messages to multiple phone numbers
You have to send multiple API requests, one per target phone number.
For instance, if you want to send a message to 10 phone numbers, you should send 10 independent HTTPS requests to the API.
There is no option to send multiple messages in a single API request.
How to validate if a phone number can receive WhatsApp messages
You can validate if a given phone number is linked to a WhatsApp account and can receive messages.
The API provides an endpoint that can validate whether a given phone number exists in WhatsApp or not.
The only requirement is to have at least one WhatsApp number connected to the platform in your current account.
For more details, please check out the API endpoint documentation here.
Before you check if a phone number exists on WhatsApp, you can also validate and normalize the format of a list of phone numbers by using the numbers validator API endpoint. This endpoint only validates the correct E164 format, but it does not check whether the phone number effectively exists on WhatsApp.
Note: The number of WhatsApp check validations is limited per month based on your subscription plan. Please check out the pricing table for more details about the limits.
Looking for more answers? Check out the extended FAQs.
Further useful resources
API Documentation
For more details about the endpoint API, please check the documentation where you will find all the details about the accepted request params, possible success or error responses and ready-to-use code examples in multiple programming languages:
https://app.wassenger.com/docs/#tag/Messages/operation/createMessage
🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!