skip to Main Content

following these docs https://mukulsib.readme.io/reference/sendwhatsappmessage

i wrote this code:

import axios from 'axios';

const API_KEY = '/i got api key from brevo SMTP & API page/';

export default async function handler(req, res) {
  if (req.method === 'POST') {
    const { phoneNumbers, Message } = req.body;

    for (const phone of phoneNumbers) {
        const options = {
            method: 'POST',
            headers: {
              accept: 'application/json',
              'content-type': 'application/json',
              'api-key': API_KEY,
            },
            body: JSON.stringify({
                senderNumber: '123456789', 
                text: Message, 
                contactNumbers: [phone,]
            })
        };

            try {
                const response = await axios.request(options);
                console.log(`Message sent successfully to ${phone}`);
                console.log('Response data:', response.data);
            } catch (error) {
                console.error(`Error sending message to ${phone}:`, error.message);
            }
        }
    
            res.status(200).json({ message: 'Messages sent' });
        } else {
            res.status(405).json({ error: 'Method not allowed' });
        }
    }

and when i try it on the docs page i get this error:
{"code": "permission_denied", "message": "Your account is not registered"}

2

Answers


    1. Ensure your API key is correctly assigned.
    2. With Axios, you typically don’t need to set the Content-Type to application/json; Axios does this automatically when you provide a data object.
    3. Your error handling seems appropriate, but you might want to log error.response.data to get more detailed API error messages if available
    import axios from 'axios';
    
    const API_KEY = '/i got api key from brevo SMTP & API page/';
    
    export default async function handler(req, res) {
      if (req.method === 'POST') {
        const { phoneNumbers, Message } = req.body;
    
        for (const phone of phoneNumbers) {
          try {
            // Construct the request configuration object
            const response = await axios.post('https://api.brevo.io/api/whatsapp/send-message', {
              senderNumber: '123456789',
              text: Message,
              contactNumbers: [phone],
            }, {
              headers: {
                accept: 'application/json',
                'api-key': API_KEY,
              },
            });
    
            console.log(`Message sent successfully to ${phone}`);
            console.log('Response data:', response.data);
    
          } catch (error) {
            console.error(`Error sending message to ${phone}:`, error.message);
            // More detailed logging if the response data is available
            if (error.response && error.response.data) {
              console.error('Detailed error:', error.response.data);
            }
          }
        }
    
        res.status(200).json({ message: 'Messages sent' });
      } else {
        res.status(405).json({ error: 'Method not allowed' });
      }
    }
    Login or Signup to reply.
  1. Seems like an issue with your API key. It might not be the correct one or the validity might be over. Can you regenerate the key again and try. Also, logging the proper formatted error in catch can help you find the issue, if it is anything other than API key (authorization issue)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search