D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
whatsapp-api
/
Filename :
README.md
back
Copy
<div align="center"> # ๐ WhatsApp REST API using whatsapp-web.js Simple and powerful REST API built with **Node.js** and **Express** on top of **whatsapp-web.js** to send WhatsApp messages programmatically. </div> --- ## ๐ฆ Prerequisites - Node.js (version 18 or newer is recommended) - npm (comes with Node.js) - Chrome/Chromium browser (used internally via puppeteer) - An active WhatsApp account on any phone --- ## ๐ Project Setup ### 1๏ธโฃ Get the code Either clone the repository or download it as a ZIP and extract it. ### 2๏ธโฃ Install dependencies From inside the project folder: ```bash cd whatsappwebjs npm install ``` This will install: - express - whatsapp-web.js - qrcode-terminal --- ## โ๏ธ Configuration & Server Start ### 1๏ธโฃ Start the server From inside the project folder: ```bash npm start ``` or: ```bash node src/index.js ``` The server listens on: - `PORT` environment variable if provided - otherwise `3000` by default ### 2๏ธโฃ First-time WhatsApp linking (QR Code) On the first run: - **whatsapp-web.js** will launch in headless mode - A QR code will be printed in the terminal - On your **WhatsApp** mobile app: - Open the menu (โฎ on Android, Settings on iOS) - Choose **Linked devices** - Tap **Link a device** - Scan the QR code shown in your terminal After scanning successfully, the session is stored in: - `.wwebjs_auth/` On subsequent runs you will not need to scan the QR again unless you delete the session data or log out from linked devices. --- ## ๐ API Endpoints The server is built with Express and exposes three main endpoints: ### 1๏ธโฃ Health check โ `/health` - **Method:** `GET` - **Description:** Simple check to verify the server is running. #### Request example: ```bash curl http://localhost:3000/health ``` #### Response example: ```json { "status": "ok" } ``` --- ### 2๏ธโฃ WhatsApp session status โ `/session-status` - **Method:** `GET` - **Description:** Check whether the WhatsApp client is ready to send messages. #### Request example: ```bash curl http://localhost:3000/session-status ``` #### Response example: ```json { "ready": true } ``` - `ready: true` โ client is initialized and can send messages - `ready: false` โ client is still initializing or is disconnected --- ### 3๏ธโฃ Send WhatsApp message โ `/send-message` - **Method:** `POST` - **Content-Type:** `application/json` - **Description:** Send a text message to a WhatsApp number. #### Request body schema: ```json { "phoneNumber": "201234567890", "message": "Hello from API" } ``` - `phoneNumber`: - Phone number including country code - Can contain spaces or symbols (they are stripped automatically) - `message`: - Text content you want to send #### Example using `curl` (PowerShell on Windows): ```bash curl -X POST http://localhost:3000/send-message ^ -H "Content-Type: application/json" ^ -d "{\"phoneNumber\": \"201234567890\", \"message\": \"Hello from API\"}" ``` > Note: `^` is used in PowerShell to split the command into multiple lines. #### Successful response example: ```json { "success": true, "id": "true_201234567890@c.us_XXXXXXXXXX", "timestamp": 1710000000, "to": "201234567890@c.us" } ``` #### Possible error responses: - Client not ready: ```json { "error": "WhatsApp client not ready" } ``` - Missing required fields: ```json { "error": "phoneNumber and message required" } ``` - Unexpected error while sending: ```json { "error": "Failed to send message" } ``` --- ## ๐ง How the script works (high-level) Main entry point: `src/index.js` - Creates an Express app: - Parses JSON bodies via `app.use(express.json())` - Creates a WhatsApp client: - Uses `LocalAuth` to persist session automatically - Configures puppeteer in headless mode with secure flags (`--no-sandbox`, `--disable-setuid-sandbox`) - Listens to WhatsApp client events: - `qr` โ prints QR code in terminal using `qrcode-terminal` - `ready` โ sets `clientReady = true` - `disconnected` โ sets `clientReady = false` - When the Express server starts listening: - Calls `client.initialize()` to bootstrap the WhatsApp session --- ## โ ๏ธ Notes & Best Practices - Do not share the `.wwebjs_auth` folder with anyone; it contains your WhatsApp session data. - Make sure the machine running this script has a stable internet connection. - If your WhatsApp session changes or you log out of all devices: - You may need to delete `.wwebjs_auth` and restart the server to scan a new QR code. - Avoid using this script for spam or bulk unsolicited messages; your number may be banned by WhatsApp. --- ## ๐ ๏ธ Extending and customizing Since the codebase is small and straightforward, you can easily: - Add new endpoints (send images, files, templated messages, etc.). - Integrate the API with other systems (admin panels, CRM, billing systems, and more). - Add authentication (API keys, JWT, etc.) to protect your endpoints. --- ## ๐งพ Useful commands - Start the server: ```bash npm start ``` - Run the main file directly (without npm scripts): ```bash node src/index.js ``` --- ## ๐ Quick usage recap 1. Install prerequisites (Node.js). 2. Navigate to the project folder and run: - `npm install` 3. Start the server: - `npm start` 4. Scan the QR code from the WhatsApp mobile app. 5. Confirm `/session-status` returns `ready: true`. 6. Use `/send-message` endpoint to send messages via the REST API.