DEV Community

Cover image for Ever wanted to create an API out of your WhatsApp account?
Mohd Salman Ansari
Mohd Salman Ansari

Posted on

Ever wanted to create an API out of your WhatsApp account?

What is Whatsapp? 🀷

dogs-communicating
WhatsApp is a mobile messaging application which allows you to send and receive messages without having to pay for SMS.

WhatsApp is the most popular messaging app in the world with over 1 billion monthly active users.

WhatsApp's end-to-end encryption ensures that your messages are secure from third parties like hackers and government surveillance programs.

What can you do with WhatsApp API? πŸ˜•

Usage Scenarios for the API

  1. Sending programmatic responses to contacts.
  2. Providing Customer support to a Business.
  3. Marketing.
  4. etc.

Let's Begin 🌟

Note: Install Node.js v16+ if you haven’t already.

We are going to use whatsapp-api-nodejs that converts users WhatsApp account into an API.

In addition, whatsapp-api-nodejs can 🀯:

  • send media message such as image, video, document etc
  • send response message data to a specified webhook URL
  • send messages to groups
  • downloads contacts profiles pic
  • send template message
  • create, delete and join a group
  • etc.

To get started clone the repository from Github:

git clone https://github.com/salman0ansari/whatsapp-api-nodejs.git
Enter fullscreen mode Exit fullscreen mode

Change directory into the project and run

npm install
Enter fullscreen mode Exit fullscreen mode

It will take some time to install the dependencies.

If you going to use webhook make sure to create a .env file in the project root. Inside the file add:

WEBHOOK_URL=https://your webhook url
Enter fullscreen mode Exit fullscreen mode

Finally, run npm run start in the project root to start the server. Our applications will be accessed at

http://localhost:3333
Enter fullscreen mode Exit fullscreen mode

Init Instance 🌌

To initialise a server make a GET request
Note: Don't forgot to save KEY value.

ENDPOINT: /init
Enter fullscreen mode Exit fullscreen mode

init

Scanning QR Code πŸ“±

To scan QR code make a GET request

ENDPOINT: /qrcode?key=KEY
QUERY -> KEY = Instance Key
Enter fullscreen mode Exit fullscreen mode

Note: make sure to scan QR ASAP after you init a Instance.

qrcode

Sending a Message πŸ“¨

To send a Text message make a POST request

ENDPOINT: /sendText?key=KEY
QUERY -> KEY = Instance Key
REQUEST BODY:
{
  "msg_data": {
    "id": "919999999999",
    "message": "Hello World"
  }
}
Enter fullscreen mode Exit fullscreen mode

sendText

API-Docs πŸ“‘

To check more enpoint make a GET request

ENDPOINT: /api-docs
Enter fullscreen mode Exit fullscreen mode

api-docs

THAT'S IT 🏁

In the next tutorial we will learn how can we use this API to send memes daily to your friend.

Links πŸ”—

whatsapp-api-nodejs: https://github.com/salman0ansariwhatsapp-api-nodejs

nodejs: https://nodejs.org

Top comments (0)