Search code examples
node.jshttpwhatsapp

sending message using whatsapp cloud api with node.js and express


I'm currently working on a project that need to send whatsapp's messages, for this work i'm using my favorite node.js framework Express.js.

Honestly i have never done such kind of work it's my first time dealing with whatsapp cloud api , i did read the entire documentation about how to have everything done with express but when i try to apply what i learned it turns into a nightmare your help will be very important.

Here is the example configuration of my code

    axios({
        method: 'POST',
        url: 'https://graph.facebook.com/v14.0/Phone Number ID/messages',
        data: {
            message_product: 'whatsapp',
            to: 'client number',
            type: "template",
            template: {
                name: "hello_world",
                language: {
                    code: "en_US"
                }
            },
        },
        headers: {
            "Authorization": "Bearer  + token",
            "Content-Type": "application/json"
        }
    }).then(result => {
        console.log(result)
    }).catch(err => {
        console.log(err.message)
    })

And here is the error that i'm getting

read ECONNRESET

Solution

  • I am working in same idea of project and it worked for me

    var options = {
    method: 'POST',
      url: 'https://graph.facebook.com/v15.0/PHONE_NUMBER_ID/messages',
      headers: {
        Authorization: 'Bearer TOKEN',
        'Content-Type': 'application/json'
      },
      data: {
        messaging_product: 'whatsapp',
        to: '{number}',
        type: 'template',
        template: {name: 'hello_world', language: {code: 'en_US'}}
      }
    };
    
    axios.request(options).then(function (response) {
      console.log(response.data);
    }).catch(function (error) {
      console.error(error);
    });