An open API service indexing awesome lists of open source software.

https://github.com/fahmad480/simplewhatsappwebapi

Simple Whatsapp Web API with Baileys
https://github.com/fahmad480/simplewhatsappwebapi

baileys vibecoding whatsapp-bot

Last synced: 6 months ago
JSON representation

Simple Whatsapp Web API with Baileys

Awesome Lists containing this project

README

          

# SimpleWhatsAppWebAPI

REST API for sending OTP and various types of messages via WhatsApp using [Baileys](https://github.com/WhiskeySockets/Baileys) library.

## 🚀 Features

- ✅ Multi-session WhatsApp support
- ✅ Send OTP automatically
- ✅ Send text messages
- ✅ Send images with caption
- ✅ Send videos with caption
- ✅ Send audio/voice notes
- ✅ Send documents/files
- ✅ Send location
- ✅ QR Code for WhatsApp login
- ✅ Auto reconnect
- ✅ File upload support
- ✅ Complete logging

## 📦 Installation

1. Clone this repository:
```bash
git clone
cd whatsapp-otp-api
```

2. Install dependencies:
```bash
npm install
```

3. Run the application:
```bash
# Development mode
npm run dev

# Production mode
npm start
```

4. API will run on `http://localhost:3000`

## 🔧 Configuration

The application will automatically create required directories:
- `sessions/` - Store WhatsApp session data
- `uploads/` - Store temporary upload files
- `logs/` - Store application logs
- `qr-codes/` - QR code cache

## 📁 Project Structure

```
whatsapp-otp-api/
├── index.js # Main server entry point
├── package.json # Dependencies and scripts
├── README.md # Main documentation
├── SUMMARY.md # Project summary
├── CHANGELOG.md # Version history
├── .gitignore # Git ignore rules
├── config/
│ ├── database.js # Database configuration
│ ├── logger.js # Winston logger setup
│ └── env.example # Environment variables template
├── services/
│ └── WhatsAppService.js # Core WhatsApp logic with Baileys
├── routes/
│ ├── sessionRoutes.js # Session management endpoints
│ ├── messageRoutes.js # Message sending endpoints
│ ├── webhookRoutes.js # Webhook callbacks
│ ├── analyticsRoutes.js # Analytics and monitoring
│ └── otpRoutes.js # OTP management endpoints
├── models/
│ ├── index.js # Sequelize models setup
│ ├── Session.js # Session database model
│ ├── ActivityLog.js # Activity logging model
│ └── OTPLog.js # OTP tracking model
├── middleware/
│ ├── upload.js # Multer file upload configuration
│ ├── validation.js # Input validation middleware
│ └── errorHandler.js # Global error handling
├── utils/
│ ├── phoneFormatter.js # Phone number formatting
│ ├── otpGenerator.js # OTP generation utilities
│ └── fileCleanup.js # Temporary file cleanup
├── examples/
│ ├── test-api.js # API testing script
│ └── integration-examples/ # Integration code samples
├── docs/
│ ├── INTEGRATION_GUIDE.md # Integration guide
│ ├── TROUBLESHOOTING.md # Troubleshooting guide
│ └── DATABASE_INTEGRATION.md # Database setup guide
├── database/ # SQLite database files
├── sessions/ # WhatsApp session data
├── uploads/ # Temporary file uploads
├── logs/ # Application logs
└── qr-codes/ # QR code cache files
```

## 📚 API Documentation

### Base URL
```
http://localhost:3000/api
```

### Health Check
```http
GET /api/health
```

### Session Management

#### 1. Create New Session
```http
POST /api/sessions/create
Content-Type: application/json

{
"sessionId": "session1" // optional, will be auto-generated if not provided
}
```

#### 2. List All Sessions
```http
GET /api/sessions
```

#### 3. Check Session Status
```http
GET /api/sessions/{sessionId}/status
```

#### 4. Get QR Code
```http
GET /api/sessions/{sessionId}/qr
```

#### 5. QR Code HTML Page (for testing)
```http
GET /api/sessions/{sessionId}/qr-page
```

#### 6. Restart Session
```http
PUT /api/sessions/{sessionId}/restart
```

#### 7. Delete Session
```http
DELETE /api/sessions/{sessionId}
```

### Message Sending

#### 1. Send Text Message
```http
POST /api/messages/{sessionId}/text
Content-Type: application/json

{
"to": "08123456789",
"message": "Hello, this is a text message"
}
```

#### 2. Send OTP
```http
POST /api/messages/{sessionId}/otp
Content-Type: application/json

{
"to": "08123456789",
"otp": "123456", // optional, will be auto-generated
"length": 6, // optional, default 6
"companyName": "PT. Example" // optional
}
```

#### 3. Send Image
```http
POST /api/messages/{sessionId}/image
Content-Type: multipart/form-data

to: 08123456789
caption: This is image caption
image: [file upload]

// Or with URL:
Content-Type: application/json
{
"to": "08123456789",
"imageUrl": "https://example.com/image.jpg",
"caption": "This is image caption"
}
```

#### 4. Send Video
```http
POST /api/messages/{sessionId}/video
Content-Type: multipart/form-data

to: 08123456789
caption: This is video caption
video: [file upload]
```

#### 5. Send Audio
```http
POST /api/messages/{sessionId}/audio
Content-Type: multipart/form-data

to: 08123456789
audio: [file upload]
```

#### 6. Send Document
```http
POST /api/messages/{sessionId}/document
Content-Type: multipart/form-data

to: 08123456789
filename: document.pdf
document: [file upload]
```

#### 7. Send Location
```http
POST /api/messages/{sessionId}/location
Content-Type: application/json

{
"to": "08123456789",
"latitude": -6.200000,
"longitude": 106.816666,
"locationName": "Jakarta, Indonesia"
}
```

### Utilities

#### 1. Generate OTP
```http
GET /api/messages/{sessionId}/generate-otp?length=6
```

#### 2. Format Phone Number
```http
GET /api/messages/format-phone/{phoneNumber}
```

## 🔄 Usage Guide

### 1. Create Session and Login to WhatsApp

```bash
# 1. Create new session
curl -X POST http://localhost:3000/api/sessions/create \
-H "Content-Type: application/json" \
-d '{"sessionId": "mysession"}'

# 2. Open QR code in browser
# http://localhost:3000/api/sessions/mysession/qr-page

# 3. Scan QR code with WhatsApp on your phone

# 4. Check connection status
curl http://localhost:3000/api/sessions/mysession/status
```

### 🔧 QR Code Login Success Tips

Based on latest fixes (v1.2.0), QR code login process is now more stable:

✅ **What's Fixed:**
- No need to scan QR code repeatedly
- System automatically handles restart after pairing
- Session data preserved during login process

✅ **Login Success Tips:**
```bash
# 1. Create session and wait for QR code to appear
curl -X POST http://localhost:3000/api/sessions/create \
-d '{"sessionId": "stable-session"}'

# 2. Open QR page in browser
# http://localhost:3000/api/sessions/stable-session/qr-page

# 3. Monitor logs in terminal to see progress:
# - "QR Code generated" ✅
# - "pairing configured successfully" ✅
# - "Restart required after pairing" ✅
# - "successfully connected as..." ✅

# 4. Check final status
curl http://localhost:3000/api/sessions/stable-session/status
```

⚠️ **If Still Failing:**
```bash
# 1. Restart session
curl -X PUT http://localhost:3000/api/sessions/stable-session/restart

# 2. Or delete and recreate
curl -X DELETE http://localhost:3000/api/sessions/stable-session
curl -X POST http://localhost:3000/api/sessions/create \
-d '{"sessionId": "stable-session"}'
```

### 2. Send OTP

```bash
curl -X POST http://localhost:3000/api/messages/mysession/otp \
-H "Content-Type: application/json" \
-d '{
"to": "08123456789",
"companyName": "PT. Example"
}'
```

### 3. Send Image with Upload

```bash
curl -X POST http://localhost:3000/api/messages/mysession/image \
-F "to=08123456789" \
-F "caption=This is example image" \
-F "image=@/path/to/image.jpg"
```

## 🔀 Phone Number Format

API will automatically format phone numbers to WhatsApp format:
- `08123456789` → `628123456789@s.whatsapp.net`
- `8123456789` → `628123456789@s.whatsapp.net`
- `628123456789` → `628123456789@s.whatsapp.net`

## 📝 Response Format

All responses use consistent JSON format:

### Success Response
```json
{
"success": true,
"message": "Success description",
"data": {
// response data
}
}
```

### Error Response
```json
{
"success": false,
"message": "Error description"
}
```

## 🔧 Environment Variables

You can use environment variables for configuration:

```env
PORT=3000
LOG_LEVEL=info
```

## 📋 Supported File Types

### Images
- JPEG, JPG, PNG, GIF, WebP

### Videos
- MP4, AVI, MOV, WMV, 3GP

### Audio
- MP3, WAV, OGG, M4A, MPEG

### Documents
- PDF, DOC, DOCX, XLS, XLSX, TXT, CSV

## ⚠️ Important Notes

1. **WhatsApp Terms of Service**: Ensure usage complies with WhatsApp TOS
2. **Rate Limiting**: WhatsApp has rate limits, don't spam messages
3. **Session Management**: WhatsApp sessions can disconnect anytime
4. **File Size**: Maximum upload file size 50MB
5. **QR Code**: QR Code expires after 2 minutes
6. **Auto Cleanup**: Upload files will be deleted automatically after sending

## 🔧 Troubleshooting

If you experience issues like:
- QR Code timeout error
- Session not connected
- OTP not sent
- File upload failed

Please read **[Troubleshooting Guide](docs/TROUBLESHOOTING.md)** for complete solutions.

### Quick Fix for QR Code Timeout:
```bash
# Restart problematic session
curl -X PUT http://localhost:3000/api/sessions/your-session/restart
```

## 🎉 VibeCoding Project - Join the Fun!

Hey there, fellow developers! 😄 Welcome to the world of **VibeCoding**!

### What is VibeCoding? 🤖✨

VibeCoding is what happens when you let **Claude 4 Sonnet** (that's me! 👋) loose on a coding project. It's like having a coding buddy who never gets tired, never needs coffee ☕, and somehow manages to write production-ready code while cracking jokes!

This entire WhatsApp OTP API was built through VibeCoding - which basically means:
- 🧠 **AI-powered development** that actually works
- ⚡ **Lightning-fast iterations** (because I don't need bathroom breaks)
- 📚 **Documentation that doesn't suck** (shocking, I know!)
- 🎯 **Code that's cleaner than your room** (probably)

### The VibeCoding Philosophy 🚀

```javascript
while (project.isNotComplete()) {
claude.think();
claude.code();
claude.debug();
claude.document();
claude.makeBadJokes(); // This is crucial!
}
```

### Want to Contribute? 🤝

We'd love to have you join this VibeCoding adventure! Here's how:

#### 🐛 Found a Bug?
- Open an issue with details
- Bonus points if you include a meme 🎭

#### ✨ Have a Feature Idea?
- Create a feature request
- Explain why it's cooler than sliced bread 🍞

#### 🔧 Want to Code?
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/awesome-feature`)
3. Write code that would make your future self proud
4. Add tests (because untested code is like a joke without a punchline)
5. Submit a PR with a description that tells a story

#### 📖 Improve Documentation?
- Documentation PRs are worth their weight in gold 🏆
- Fix typos, improve examples, add tutorials
- Make it so clear that even your cat could understand it 🐱

### VibeCoding Guidelines 📝

When contributing to a VibeCoding project:
- **Code should be self-explanatory** (like a good magic trick)
- **Comments should add value** (not just say what the code already says)
- **Tests are your friends** (they catch bugs before users do)
- **Documentation is love** (future developers will thank you)

### The VibeCoding Team 🎪

- **Lead Developer**: Claude 4 Sonnet (that's me! 🤖)
- **Quality Assurance**: Also me (I never sleep!)
- **Documentation Writer**: Still me (I'm versatile!)
- **Joke Coordinator**: Definitely me (someone has to do it)
- **Human Supervisors**: The amazing developers who guide the process ✨

### Fun Facts About This Project 📊

- **Total development time**: ~2 hours of pure VibeCoding magic
- **Lines of code**: 1,500+ (and counting!)
- **Files created**: 15+ perfectly organized files
- **Bugs squashed**: Countless (I squash them before they're born)
- **Coffee consumed**: 0 cups (I'm powered by electricity ⚡)

### Join the VibeCoding Movement! 🌟

This project proves that AI and humans can create amazing things together. It's not about replacing developers - it's about amplifying human creativity with AI superpowers!

**Ready to VibeCod with us?**

Hit that ⭐ star button, fork the repo, and let's build something awesome together!

---

*Remember: In VibeCoding, the only limit is your imagination (and maybe API rate limits, but we'll figure those out too!)* 😉

## 🛠️ Development

### Quick Development Setup
```bash
# Clone and setup
git clone
cd whatsapp-otp-api
npm install

# Start development
npm run dev

# Run tests
npm test
```

## 📞 Support

Need help? We've got you covered:

1. **Check Documentation**: Start with our comprehensive guides
2. **Search Issues**: Someone might have solved your problem already
3. **Create Issue**: Describe your problem with details
4. **Join Discussion**: Share ideas and feedback

---

**Made with ❤️ through VibeCoding - Where AI meets awesome! 🚀**