How to send message through WhatsApp api with Node.js

Hamza Malik
4 min readSep 18, 2024

--

In today’s interconnected world, messaging apps have become an integral part of our daily communication. WhatsApp, with its massive user base, stands out as one of the most popular platforms. For businesses and developers, the ability to send automated messages through WhatsApp can be a game-changer. This article will guide you through setting up an ULTRAMSG account and integrating it with Node.js to send WhatsApp messages programmatically.

Source : Google

What is ULTRAMSG?

ULTRAMSG is a service that provides an API for sending and receiving WhatsApp messages. It allows developers to integrate WhatsApp messaging capabilities into their applications, making it easier to automate communication processes.

Setting Up Your ULTRAMSG Account

  1. Sign Up for ULTRAMSG
  • Visit the ULTRAMSG website.
  • Click on the “Sign Up” button.
  • Fill in your details and create an account.

2. Verify Your WhatsApp Number

  • After logging in, you’ll need to verify your WhatsApp number.
  • Follow the on-screen instructions to scan the QR code with your WhatsApp app.
  • This step links your WhatsApp account to ULTRAMSG.

3. Obtain Your API Credentials

  • Once your account is set up, navigate to the dashboard.
  • Look for your Instance ID and Token. You’ll need these for API authentication.

4. Choose a Plan

  • ULTRAMSG offers different plans based on your messaging needs.
  • Select a plan that suits your requirements and complete the payment process.

Integrating ULTRAMSG with Node.js

Now that your ULTRAMSG account is ready, let’s integrate it with Node.js to send WhatsApp messages.

  1. Set Up Your Node.js Project
  • Create a new directory for your project
  • Initialize a new Node.js project by running
npm init -y
  • Install the necessary dependencies:
npm install axios

2. Create the Main Script

  • Create a new file named app.js in your project directory.
  • Open app.js in your preferred code editor.

3. Write the Code to Send a WhatsApp Message

  • Here’s a basic script to send a WhatsApp message using ULTRAMSG:
const axios = require('axios');

async function sendWhatsAppMessage(to, message) {
const instance_id = 'YOUR_INSTANCE_ID';
const token = 'YOUR_TOKEN';
const url = `https://api.ultramsg.com/${instance_id}/messages/chat`;

const data = {
token: token,
to: to,
body: message,
};

try {
const response = await axios.post(url, data);
console.log('Message sent successfully:', response.data);
} catch (error) {
console.error('Error sending message:', error.response ? error.response.data : error.message);
}
}

// Example usage
sendWhatsAppMessage('1234567890', 'Hello from Node.js!');

4. Replace Placeholders

  • Replace 'YOUR_INSTANCE_ID' with your actual ULTRAMSG Instance ID.
  • Replace 'YOUR_TOKEN' with your ULTRAMSG Token.

5. Run the Script

  • Save the file and run it using Node.js:
node app.js
  • If everything is set up correctly, you should see a success message in the console, and the recipient should receive the WhatsApp message.

Advanced Features and Best Practices

  1. Error Handling
  • Implement robust error handling to manage API failures or network issues.
  • Log errors for debugging and monitoring purposes.

2. Rate Limiting

  • Be aware of ULTRAMSG’s rate limits for your plan.
  • Implement a queue system for high-volume messaging to avoid hitting rate limits.

3. Message Templates

  • For business accounts, use approved message templates to ensure compliance with WhatsApp’s policies.
  • Store templates in a separate file or database for easy management.

4. Attachments and Rich Media

  • ULTRAMSG supports sending images, documents, and other media types.
  • Modify the script to include file attachments when needed.

5. Webhooks for Incoming Messages

  • Set up webhooks to receive incoming messages and automate responses.
  • This enables two-way communication through your application.

6. Security Considerations

  • Never hardcode your API credentials in your script.
  • Use environment variables or a secure configuration management system.

7. Testing

  • Thoroughly test your integration in a sandbox environment before going live.
  • Create test cases for various scenarios, including edge cases and error conditions.

Conclusion

Integrating ULTRAMSG with Node.js opens up a world of possibilities for automated WhatsApp messaging. From customer support to marketing campaigns, the ability to programmatically send WhatsApp messages can significantly enhance your communication strategy.

Remember to always respect users’ privacy and adhere to WhatsApp’s policies when sending automated messages. Use this power responsibly and focus on providing value to your users through relevant and timely communications.

As you become more comfortable with the ULTRAMSG API, explore its additional features like bulk messaging, scheduled messages, and analytics. These advanced capabilities can help you build more sophisticated WhatsApp-based applications and services.

By following this guide, you’ve taken the first step towards leveraging the power of WhatsApp messaging in your Node.js applications. Continue exploring, experimenting, and building to make the most of this powerful integration.

--

--

Hamza Malik
Hamza Malik

Written by Hamza Malik

0 Followers

Experienced Full-Stack Developer with over years of crafting seamless web experiences.

No responses yet