Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am creating a project using React and Express JS. Here, I try to send pdf to whatsapp using twilio, but it shows a few errors to show pdf and tells that I have to save pdf in public url. What I am expecting is to send pdf to Whatsapp by saving it in local server. Which software is best to send Whatsapp messages freely with pdf and image attachment?

What I have tried:

I tried to send pdf to whatsapp using twilio using React and Express but it is not showing pdf in Whatsapp which software I have to use to solve this issue.
Posted
Updated 5-Nov-23 1:20am
v2

There are two ways to upload PDFs to WhatsApp using their Business API. You either have to host the PDF in a public location, or you have to upload the PDF to the WhatsApp servers first, and then use that. If the POST message you send to WhatsApp uses a link type, that must be a public facing server location. If you want to follow the upload message route, behind the scenes, you will have to upload the message to WhatsApp[^], and then use the ID you get back from the upload in your POST message.

The WhatsApp documentation about this[^].

If you have to use Twilio, then your code is probably going to have to look something like this:
JavaScript
const express = require('express');
const twilio = require('twilio');

const app = express();

// Your Twilio Account SID and Auth Token
const accountSid = 'YOUR_TWILIO_ACCOUNT_SID';
const authToken = 'YOUR_TWILIO_AUTH_TOKEN';

// Create a Twilio client
const client = new twilio.TwilioClient({
  accountSid,
  authToken
});

// Create a route to handle PDF uploads
app.post('/upload-pdf', async (req, res) => {
  // Get the PDF file from the request
  const pdfFile = req.file('pdf');

  // Upload the PDF file to Twilio
  const pdfMedia = await client.media.create({
    content: pdfFile
  });

  // Get the URL of the uploaded PDF file
  const pdfUrl = pdfMedia.uri;

  // Send the PDF file to your customer
  await client.messages.create({
    body: 'Here is your PDF file:',
    mediaUrl: pdfUrl,
    to: req.body.toNumber
  });

  // Return a success response
  res.sendStatus(200);
});

// Start the Express server
app.listen(3000, () => {
  console.log('Server listening on port 3000');
});
Note: Code sourced from bard.google.com query.
 
Share this answer
 
This is because Twilio requires the media file to be hosted on a publicly accessible URL to be sent through WhatsApp. To accomplish this, you can upload the PDF to a cloud storage service such as Amazon S3, Google Cloud Storage, or Azure Blob Storage and make it publicly accessible. Once the PDF is uploaded and accessible via a URL, you can provide this URL to Twilio's API for sending the WhatsApp message with the PDF attachment.
Other alternatives for sending WhatsApp messages with media attachments include:
1. Vonage (formerly Nexmo)
2. MessageBird
3. Infobip
4. WhatsApp Business API (Official API from WhatsApp, but it has specific requirements and is not free)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900