Invite WhatsApp Group participants automatically using PHP
In this tutorial, we are going to show you how to get a WhatsApp group invitation link and send it to your contacts with the Wassenger API and PHP
Expanding your audience by sending WhatsApp group invite links is highly effective, thanks to the platform’s impressive 98% open rates. Imagine being able to send personalized recommendations, answer common questions, and analyze market trends automatically and instantly.
However, enhanced WhatsApp privacy features now make it difficult to automatically add users to groups if they haven’t saved your number, as their privacy settings may prevent it.
The good news is there’s a solution covered in this tutorial: you can send a private message to specific users with the group invite link, enabling them to join the group.
To achieve this, you’ll perform two tasks using the API:
1. Obtain the WhatsApp group invitation link: You must be a participant with invite permissions in the target group.
2. Send the group invite link via private WhatsApp message to the user.
Find more information and code examples below!
🤩 🤖 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 get started in minutes!
Requirements
- Have a WhatsApp number already linked to the platform and online.
- Get your Wassenger API key here
- 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 endpoints to send messages to a group:
🖥️ 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.
Get the invitation link using PHP (cURL)
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.wassenger.com/v1/devices/$DEVICE_ID/groups/$GROUP_ID@g.us/invite",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Token: ENTER API KEY HERE"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Get the invitation link using PHP
// 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://mdref.m6w6.name/http
<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://api.wassenger.com/v1/devices/$DEVICE_ID/groups/$GROUP_ID@g.us/invite');
$request->setRequestMethod('GET');
$request->setHeaders([
'Token' => 'ENTER API KEY HERE'
]);
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
You will get something like:
{
"code": "CPBgYNktDdV0nkjzz9",
"url": "https://chat.whatsapp.com/CPBgYNktDdV0nkjzz9"
}
🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!
Now, you can share the link with the contacts you want to invite to your group.
Send a message with the given link using PHP (cURL)
<?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([
'phone' => '+1234567890',
'message' => 'Join our Newsleter group!: https://chat.whatsapp.com/CPBgYNktDdV0nkjzz9'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Token: ENTER API KEY HERE"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Send a message with the given link using PHP
// 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://mdref.m6w6.name/http
<?php
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->append(json_encode([
'phone' => '+1234567890',
'message' => 'Join our Newsleter group!: https://chat.whatsapp.com/CPBgYNktDdV0nkjzz9'
]));
$request->setRequestUrl('https://api.wassenger.com/v1/messages');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setHeaders([
'Content-Type' => 'application/json',
'Token' => 'ENTER API KEY HERE'
]);
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!
Live API testing
You can live-test and play with the API directly from your browser.
Once you are done testing, get the auto-generated code example in your preferred programming language and you will be ready to go.
🤩 🤖 Wassenger is a complete API solution for WhatsApp. Sign up for a 7-day free trial and get started in minutes!