Send audio messages using WhatsApp API

Wassenger
7 min readJun 3, 2024

--

Do you need help with customer inquiries, promotions, and updates you must send? Imagine if you could automate these tasks, like sending audio voice-like recording messages on WhatsApp directly to customers or group chats, to get everything done quickly and efficiently. With Wassenger, this is easily done!

Whether it’s a reminder, notification, or promotion, your audio messages ensure effective delivery and improve the customer experience. Plus, you save time and offer a better user experience using audio instead of text.

In this tutorial, we’ll guide you step-by-step on how to set up and use the Wassenger API to automate the delivery of WhatsApp audio (voice recording-like) messages to🚀

Ready-to-use code examples in Python, C#, PHP, JavaScript, Ruby, Java, Go, and cURL are available here.

🫣 Don’t want to use programming? No problem! Explore our new no-code WhatsApp Campaigns feature. Import your contacts, define a message, set a delivery date and relax! 🥳 🥳

🤩 🤖 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!

This tutorial will teach you how to send audio messages to phone numbers and group chat lists using the API.

You can send audio files in any of the following: MP3, OGG, MP4, ACC.

If you need to send messages from code, you can use any programming language to perform HTTPS API requests. Below is the live API tester with ready-to-use code examples in various programming languages.

Requirements

API endpoints

In this tutorial we will use the following API endpoint:

You can send audio messages in a single API request by passing the remote URL where the file is stored. Click here to test it

Send audio message from URL

To send an audio message like a voice-recoding audio (PTT or Push-To-Talk) as a multimedia message, in this case, an audio file, you should provide a remote public URL (Internet reachable) pointing to the audio file content.

To send an audio message (PTT) by using the following methods using the API:

  • Remote URL using JSON with an url field with the publicly accessible URL to download the audio file.

Example audio MP3 audio file to use

https://download.samplelib.com/mp3/sample-9s.mp3

Note: in case the URL is not publicly accessible, it returns a non-valid HTTP status (>=400) or it returns non-binary content, such as HTML or text, the API will return an error.

Target API URL (POST)

https://api.wassenger.com/v1/messages

Required HTTPS headers

Content-Type: application/json
Token: $API-TOKEN

Request body in JSON format

{
"phone": "+1234567890",
"media": {
"url": "https://download.samplelib.com/mp3/sample-9s.mp3",
"format": "ptt"
}
}

Depending on the programming language or HTTP client software you are using, the process might vary. To make it simpler, we need to highlight the most popular tools, such as Postman, and any other programming languages.

🤩 🤖 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!

Send audio 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 = {
"phone": "+1234567890",
"media": {
"url": "https://download.samplelib.com/mp3/sample-9s.mp3",
"format": "ptt"
}
}
headers = {
"Content-Type": "application/json",
"Token": "ENTER API KEY HERE"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)
var axios = require("axios");

var options = {
method: 'POST',
url: 'https://api.wassenger.com/v1/messages',
headers: {'Content-Type': 'application/json', Token: 'ENTER API KEY HERE'},
data: {
phone: '+1234567890',
media: {url: 'https://download.samplelib.com/mp3/sample-9s.mp3', format: 'ptt'}
}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
<?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 => "{\"phone\":\"+1234567890\",\"media\":{\"url\":\"https://download.samplelib.com/mp3/sample-9s.mp3\",\"format\":\"ptt\"}}",
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;
}
// 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('{"phone":"+1234567890","media":{"url":"https://download.samplelib.com/mp3/sample-9s.mp3","format":"ptt"}}');

$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();
package main

import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)

func main() {

url := "https://api.wassenger.com/v1/messages"

payload := strings.NewReader("{\"phone\":\"+1234567890\",\"media\":{\"url\":\"https://download.samplelib.com/mp3/sample-9s.mp3\",\"format\":\"ptt\"}}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Token", "ENTER API KEY HERE")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

fmt.Println(res)
fmt.Println(string(body))

}
  • C# (RestClient)
// This code requires you to have installed RestSharp package.
// Documentation: https://restsharp.dev
// Installation: https://www.nuget.org/packages/RestSharp

var client = new RestClient("https://api.wassenger.com/v1/messages");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Token", "ENTER API KEY HERE");
request.AddParameter("application/json", "{\"phone\":\"+1234567890\",\"media\":{\"url\":\"https://download.samplelib.com/mp3/sample-9s.mp3\",\"format\":\"ptt\"}}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://api.wassenger.com/v1/messages")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request["Token"] = 'ENTER API KEY HERE'
request.body = "{\"phone\":\"+1234567890\",\"media\":{\"url\":\"https://download.samplelib.com/mp3/sample-9s.mp3\",\"format\":\"ptt\"}}"

response = http.request(request)
puts response.read_body
// 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", "ENTER API KEY HERE")
.body("{\"phone\":\"+1234567890\",\"media\":{\"url\":\"https://download.samplelib.com/mp3/sample-9s.mp3\",\"format\":\"ptt\"}}")
.asString();

🤩 🤖 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!

Send audio messages to a phone number

Sending a new media message via API is easy, you simply need to know the target phone number in E164 format and the audio file URL.

You can send messages to any phone number worldwide that has a WhatsApp account, there is no need to add a phone number as a contact in your mobile device agenda or contact list.

Send audio messages to a group chat on WhatsApp

Similar to the previous scenario, you can also send media messages to group chats.

To do so, you need to know the target group chat ID and the audio file remote URL.

To send messages to a group, you must be a participant of it with send message permissions. If you are the not the administrator of the group, you should permission to send messages.

Prepare the request

Target API URL (POST)

https://api.wassenger.com/v1/messages

Required HTTPS headers

Content-Type: application/json
Token: $API_KEY

Request body in JSON format

Sending a message to a group chat:

{
"group": "1234567890-100000000@g.us",
"media": {
"url": "https://download.samplelib.com/mp3/sample-9s.mp3",
"format": "ptt"
}
}

Send audio messages to a WhatsApp Channel

Similar to the previous scenario, you can also send media messages to WhatsApp Channels.

To do so, you need to know the target WhatsApp Channel ID and the audio file URL.

To send messages to a WhatsApp Channel, you must be the owner of the channel.

Target API URL (POST)

https://api.wassenger.com/v1/messages

Required HTTPS headers

Content-Type: application/json
Token: $API_KEY

Request body in JSON format

Sending a message to a WhatsApp Channel:

{
"channel": "12345678901234567@newsletter",
"media": {
"url": "https://download.samplelib.com/mp3/sample-9s.mp3",
"format": "ptt"
}
}

Live test sends media messages using the API

You can now play, debug and live test the API directly from your browser, explore and try more API examples and get in one click ready-to-use code snippets available in 15+ different programming languages 😎

Test the API live tester

🤩 🤖 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!

FAQ

Can I use Wassenger for chatbots?

Yes, you can build your chatbot with our API and webhooks. To do so, you need to subscribe to any Platform plan allowing you to implement chatbots on top of the API.

Explore more from our related article here.

What type of media files can be sent?

You can send images (JPEG, PNG, WEBP), videos (MP4, 3GPP), audios (MP3, OGG, MP4, ACC), gifs (MP4) and documents (PDF, PPT, PPTX, DOCX, DOC, ZIP, RAR, other binary types).

Check out the API documentation for more details.

How can I validate phone numbers?

You can validate whether a given phone number exists in WhatsApp or not, and therefore can receive messages in WhatsApp, by using the Number exists API endpoint.

Please note you must have at least one WhatsApp number connected to the platform to perform the validation.

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/Files

--

--

Wassenger

The complete WhatsApp API + Team Chat + CRM + AI Assistant for smart Businesses and Teams. Automate and work more productively with your clients on WhatsApp!