Skip to content

Latest commit

Β 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

WhatsApp OTP API

πŸš€ Modern WhatsApp OTP Service with Baileys Integration

Production-ready API for sending OTP messages via WhatsApp Web protocol with multilingual support

TypeScript Bun ElysiaJS WhatsApp


✨ Features

πŸ” Authentication & Session Management

  • Persistent WhatsApp Sessions - Automatic session restore using Baileys
  • QR Code Generation - Simple web interface for WhatsApp Web connection
  • Auth State Management - SQLite database with Drizzle ORM for reliable session storage

πŸ“± OTP Delivery System

  • Smart Number Formatting - Automatic international format conversion
  • Reliable Message Delivery - Robust error handling with fallback mechanisms
  • Template Customization - Configurable OTP message templates

🌍 Internationalization

  • Multi-language Support - Indonesian and English localization
  • Runtime Language Switching - Dynamic locale configuration per request
  • Extensible Translation System - Easy addition of new languages

🎨 Admin Interface

  • Modern Dark Theme - Next.js-inspired professional design
  • Real-time Status - Live connection status and message statistics
  • Responsive Design - Mobile-friendly admin panel

πŸ›‘οΈ Type Safety & Validation

  • Full TypeScript Coverage - End-to-end type safety
  • Zod Schema Validation - Runtime request validation
  • Error Boundaries - Comprehensive error handling

πŸš€ Quick Start

Prerequisites

  • Bun >= 1.2.0
  • Node.js >= 18.0.0 (for compatibility)
  • SQLite (included with Bun)

Installation

# Clone the repository
git clone https://github.com/idMJA/WApi.git
cd WApi

# Install dependencies
bun install

# Start the server
bun run start

First Setup

  1. Start the service:

    bun run start
  2. Connect WhatsApp:

    • Open http://localhost:3000/admin.html
    • Scan the QR code with WhatsApp on your phone
    • Wait for connection confirmation
  3. Test the API:

    curl -X POST http://localhost:3000/send-otp \
      -H "Content-Type: application/json" \
      -d '{
        "phoneNumber": "+6281234567890",
        "otp": "123456",
        "websiteName": "MyApp",
        "locale": "id"
      }'

πŸ“– API Documentation

Base URL

http://localhost:3000

Endpoints

πŸ” Authentication

GET /qr

Generate QR code for WhatsApp Web connection.

Response:

{
  "success": true,
  "qr": "data:image/png;base64,..."
}
GET /status

Check WhatsApp connection status.

Response:

{
  "success": true,
  "status": "connected",
  "phoneNumber": "+6281234567890"
}

πŸ“± OTP Services

POST /send-otp

Send OTP message via WhatsApp.

Request Body:

{
  "phoneNumber": "+6281234567890",
  "otp": "123456",
  "websiteName": "MyApp",
  "locale": "id"
}

Response:

{
  "success": true,
  "message": "OTP sent successfully"
}

Parameters:

  • phoneNumber (string, required) - Target phone number in international format
  • otp (string, required) - OTP code to send
  • websiteName (string, optional) - Name of your application
  • locale (string, optional) - Language locale (id or en, defaults to id)

🌍 Localization

GET /locales

Get all available locales.

Response:

{
  "success": true,
  "locales": ["id", "en"]
}
GET /locales/:locale

Get translations for specific locale.

Response:

{
  "success": true,
  "translations": {
    "otpMessage": "Kode OTP Anda untuk {websiteName} adalah: {otp}. Kode ini berlaku selama 5 menit.",
    "defaultWebsiteName": "Aplikasi Kami"
  }
}

πŸ› οΈ Configuration

Environment Variables

Create a .env file in the root directory:

# Server Configuration
PORT=3000
NODE_ENV=production

# Database
DATABASE_URL=./auth_info_baileys/auth.db

# Localization
DEFAULT_LOCALE=id

# WhatsApp Configuration
WHATSAPP_SESSION_PATH=./auth_info_baileys

Custom Localization

To add a new language:

  1. Create a new language file in src/locales/languages/:

    // src/locales/languages/fr.ts
    export const fr = {
      otpMessage: "Votre code OTP pour {websiteName} est: {otp}. Ce code expire dans 5 minutes.",
      defaultWebsiteName: "Notre Application"
    };
  2. Update the locale types:

    // src/locales/types.ts
    export type Locale = 'id' | 'en' | 'fr';
  3. Register the language:

    // src/locales/languages/index.ts
    export { fr } from './fr';

πŸ—οΈ Architecture

Project Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/          # Configuration management
β”‚   β”œβ”€β”€ controllers/     # Request handlers
β”‚   β”œβ”€β”€ db/             # Database and auth state
β”‚   β”œβ”€β”€ locales/        # Internationalization
β”‚   β”œβ”€β”€ routes/         # API route definitions
β”‚   β”œβ”€β”€ services/       # Business logic
β”‚   β”œβ”€β”€ types/          # TypeScript types and schemas
β”‚   └── utils/          # Helper utilities
β”œβ”€β”€ auth_info_baileys/  # WhatsApp session storage
β”œβ”€β”€ admin.html          # Admin panel interface
└── test.html          # Testing interface

Tech Stack

  • Runtime: Bun for high-performance JavaScript/TypeScript execution
  • Framework: ElysiaJS for modern web API development
  • Database: SQLite with Drizzle ORM for type-safe database operations
  • WhatsApp: Baileys for WhatsApp Web protocol implementation
  • Validation: Zod for runtime type checking and validation
  • UI: Vanilla HTML/CSS/JS with modern design principles

πŸ”§ Development

Scripts

# Development server with hot reload
bun run dev

# Production build
bun run build

# Start production server
bun run start

# Clean restart (clears auth data)
bun run start:clean

# Run tests
bun run test

# API testing
bun run test:api

Development Workflow

  1. Make changes to source files
  2. Test locally using the admin panel
  3. Validate with the test API script
  4. Build for production deployment

Debugging

Enable debug logs:

DEBUG=baileys:* bun run start

Check connection status:

curl http://localhost:3000/status

πŸš€ Deployment

Production Deployment

  1. Build the application:

    bun run build
  2. Set environment variables:

    export NODE_ENV=production
    export PORT=3000
  3. Start the service:

    bun run start

Docker Deployment

FROM oven/bun:1-alpine

WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

COPY . .
RUN bun run build

EXPOSE 3000
CMD ["bun", "run", "start"]

Process Management

Using PM2:

pm2 start ecosystem.config.js
// ecosystem.config.js
module.exports = {
  apps: [{
    name: 'whatsapp-otp-api',
    script: 'bun',
    args: 'run start',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'production',
      PORT: 3000
    }
  }]
}

πŸ›‘οΈ Security Considerations

Authentication

  • Keep WhatsApp session files secure
  • Regularly rotate session data
  • Monitor for unauthorized access

API Security

  • Implement rate limiting for production use
  • Add API key authentication
  • Validate all input parameters

Data Protection

  • Encrypt sensitive configuration
  • Use HTTPS in production
  • Regular security audits

🀝 Contributing

We welcome contributions! Please see src/locales/TRANSLATIONS.md for translation contributions.

Development Setup

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

Code Style

  • Follow TypeScript best practices
  • Use meaningful variable names
  • Add comments for complex logic
  • Format with Prettier

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ†˜ Support

Common Issues

Connection Problems:

  • Ensure WhatsApp Web is not open in other browsers
  • Clear auth data and reconnect
  • Check network connectivity

Message Delivery:

  • Verify phone number format
  • Check WhatsApp connection status
  • Review error logs

Localization Issues:

  • Verify locale parameter format
  • Check available locales endpoint
  • Validate translation files

Getting Help

  • πŸ“š Check the documentation
  • πŸ› Report bugs via GitHub Issues
  • πŸ’‘ Request features via GitHub Discussions
  • πŸ“§ Contact support for urgent issues

Made with ❀️ for modern web applications

Back to Top β€’ Bahasa Indonesia β€’ Contribute Translations

About

πŸ” Modern WhatsApp OTP API built with Bun, TypeScript, and Baileys. Features persistent sessions, multi-language support, real-time admin panel, and robust message delivery with SQLite + Drizzle ORM storage.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages