Unable to validate callback URL and verify token for Meta's WhatsApp Cloud API with Google Cloud Function
I'm trying to set up a webhook in my Google Cloud Function that can receive incoming messages from the WhatsApp Business API and forward them to platform called Front. However, when configuring the webhook on Meta for Whatsapp, I get the error The callback URL or verify token couldn't be validated. Please verify the provided information or try again later.
Here's the relevant code from my index.js file:
const axios = require('axios'); const FRONT_API_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzY29wZXMiOlsicHJvdmlzaW9uaW5nIiwicHJpdmF0ZToqIiwic2hhcmVkOioiXSwiaWF0IjoxNjc5NTE0MDU1LCJpc3MiOiJmcm9udCIsInN1YiI6ImI4MGUzZDExODQyMDUzZTk5OGE0IiwianRpIjoiYmM5NzNlNGQyZTA3YTAzMiJ9.7LBqJ5Kw3O65c4GttZuh4K2Zt7fkGIIq9yI96l06TJ8'; const FRONT_CUSTOM_CHANNEL_WEBHOOK_URL = 'https://api2.frontapp.com/channels/cha_ak6s0/incoming_messages'; const VERIFY_TOKEN = 'whatsappfronttoken'; const handleVerification = (req, res) => { const queryToken = req.query.verify_token; console.log('Verification request received:', req.query); if (queryToken === VERIFY_TOKEN) { res.send(req.query.challenge); } else { console.error('Invalid verify token:', queryToken); res.sendStatus(403); } }; exports.whatsappHandler = async (req, res) => { if (req.query.verify_token) { handleVerification(req, res); } else { const message = req.body; if (!message.contacts || !message.messages) { console.warn('Received message with missing contacts or messages property. Skipping message processing.'); res.sendStatus(200); return; } // Extract relevant information from the WhatsApp message const sender = message.contacts[0].profile.name || message.contacts[0].wa_id; const text = message.messages[0].text.body; // Format the message for Front's custom channel webhook URL const formattedMessage = { sender: { name: sender, handle: sender, }, subject: 'WhatsApp Message', body: text, body_format: 'markdown', }; // Forward the message to Front's custom channel webhook URL try { await axios.post(FRONT_CUSTOM_CHANNEL_WEBHOOK_URL, formattedMessage, { headers: { 'Authorization': `Bearer ${FRONT_API_TOKEN}`, }, }); res.sendStatus(200); } catch (error) { console.error(error); res.sendStatus(500); } } };
What could be causing this issue, and how can I resolve it?
Any assistance or guidance would be greatly appreciated. Thank you!
I've confirmed that my WhatsApp Business API credentials and webhook URL are set up correctly. I've also verified that my Google Cloud Function is deployed and accessible.
I've checked the logs for my Google Cloud Function and when trying to verify the webhook, I see an error Received message with missing contacts or messages property. Skipping message processing
. To bypass this, I tried to return a 200 status as I thought this could be caused by the fact that I was just verifying the webhook and not actually receiving an actual message from Meta.