This project uses Baileys to connect to WhatsApp and provides an API to send messages through WhatsApp. The code is set up to display a QR code in the terminal for initial authentication and to send messages via a REST API.
- Display and scan QR code in the terminal console.
- Send messages through a REST API.
- Handle automatic reconnection if the connection is closed due to reasons other than logout.
-
Clone the repository:
git clone https://github.com/aviantotiyo/WhatsApp-API-Gateway-Baileys.git cd <repository-directory>
-
Install dependencies:
Make sure you have Node.js and npm installed. Then run:
npm install
-
Run the server:
node index.js
This will start the Express server on port 3000 and attempt to connect to WhatsApp. A QR code will be displayed in the terminal for you to scan with your WhatsApp app.
Once the server is running and you have scanned the QR code, you can send messages through the API using tools like Postman or curl
.
- URL:
POST /send-message
- Content-Type:
application/json
Request Body Example:
{
"number": "621234567890",
"message": "Hello, this is a test message!"
}
Response Example:
{
"success": true,
"message": "Message sent successfully"
}
Postman Setup:
- Open Postman.
- Set the request type to
POST
. - Enter the URL:
http://localhost:3000/send-message
. - Go to the
Body
tab, selectraw
, and set the type toJSON
. - Enter the JSON data as shown in the example above.
- Click
Send
.
-
If you encounter issues with authentication or need to rescan the QR code, you might need to delete the
auth_info_baileys
folder. This can be done with:rm -rf auth_info_baileys
After deleting, restart the server to generate a new QR code.
-
Make sure to replace
<repository-url>
and<repository-directory>
with the appropriate values for your project.
- Connection Issues: Ensure your internet connection is stable. If you see
Connection closed due to logout
, follow the note above to rescan the QR code. - Errors Sending Messages: Check the logs for detailed error messages and ensure the number format is correct (
621234567890
) Please follow your country code number.
For further assistance, refer to the Baileys documentation or check the issues in the GitHub repository.