A production-ready WhatsApp bridge built with Go that provides a REST API interface for WhatsApp messaging using the whatsmeow library. This bridge allows you to send and receive WhatsApp messages programmatically while maintaining message history in a local SQLite database or a PostgreSQL database.
- WhatsApp Integration: Connect to WhatsApp using QR code authentication
- Web QR Interface: Modern web interface for QR code scanning (no terminal access required)
- REST API: Send messages, download media, and retrieve chat/message history via HTTP endpoints
- Message History: Store and retrieve message history with SQLite or PostgreSQL
- Media Support: Send and receive images, videos, audio, and documents
- Group Chat Support: Handle both individual and group conversations
- History Sync: Sync message history from WhatsApp servers
- Real-time Messaging: Receive messages in real-time
- Database Flexibility: Supports both SQLite (default) and PostgreSQL (via configuration)
- Go 1.19 or higher
- SQLite3 (default) or PostgreSQL (optional)
-
Clone the repository:
git clone <repository-url> cd whatsapp-bridge
-
Install dependencies:
cd whatsapp-bridge go mod tidy
From the project root directory:
cd whatsapp-bridge
go run main.go database.go qr_web.goThe application will:
- Start the WhatsApp client
- Launch the web QR interface
- Display a QR code both in the web interface and terminal (backup)
- Start the REST API server on port
8080 - Begin listening for incoming messages
- Open your browser and navigate to
http://localhost:8080 - You'll see a modern web interface with the QR code
- Open WhatsApp on your phone
- Go to Settings → Linked Devices → Link a Device
- Scan the QR code from the web page
- The page will automatically update when connected
If you prefer the terminal, the QR code is also displayed there as a backup option.
- Run the application
- Scan the QR code with your WhatsApp mobile app (WhatsApp > Settings > Linked Devices > Link a Device)
- Once connected, the bridge will start receiving messages
The WhatsApp Bridge runs all services on a single port:
- Combined Service:
http://localhost:8080- Modern web interface for QR code authentication
- REST API for sending messages, downloading media, etc.
- Health check endpoint
- Real-time connection status
This consolidated approach makes the application ideal for deployment on platforms like Google Cloud Run that require a single port.
POST /api/send
Send a text message or media file to a WhatsApp contact or group.
Request Body:
{
"recipient": "1234567890@s.whatsapp.net",
"message": "Hello, World!",
"media_path": "/path/to/file.jpg" // Optional for media
}Response:
{
"success": true,
"message": "Message sent successfully"
}POST /api/download
Download media from a received message.
Request Body:
{
"message_id": "message_id_here",
"chat_jid": "1234567890@s.whatsapp.net"
}Response:
{
"success": true,
"message": "Media downloaded successfully",
"filename": "downloaded_file.jpg",
"path": "/path/to/downloaded/file.jpg"
}GET /api/messages/<chat_jid>?limit=<limit>
Retrieve message history for a specific chat.
Parameters:
chat_jid: WhatsApp JID of the chatlimit: Number of messages to retrieve (optional, default: 50)
GET /api/chats
Retrieve a list of all chats with their last message timestamps.
GET /api/db/status
Check the health and connection status of the database.
whatsapp-bridge/
├── main.go # Main application code
├── qr_web.go # QR web interface
├── database.go # Database adapter
├── Dockerfile # Docker container definition
└── store/ # Local storage directory
To build the Docker image:
cd whatsapp-bridge
docker build -t whatsapp-bridge .To run the Docker container:
docker run -p 8080:8080 -v $(pwd)/store:/app/store whatsapp-bridgeThis will:
- Map port 8080 from the container to your host machine
- Mount the local
storedirectory to persist data between container restarts
The Docker container supports the following environment variables:
PORT: The port to run the server on (default: 8080)DATABASE_URL: PostgreSQL connection string (optional, falls back to SQLite if not provided)
To deploy to Google Cloud Run:
- Build and push the Docker image to Google Container Registry:
# Set your Google Cloud project ID
PROJECT_ID=your-project-id
# Build the image with Google Cloud Build
gcloud builds submit --tag gcr.io/$PROJECT_ID/whatsapp-bridge
# Or build locally and push
docker build -t gcr.io/$PROJECT_ID/whatsapp-bridge .
docker push gcr.io/$PROJECT_ID/whatsapp-bridge- Deploy to Cloud Run:
gcloud run deploy whatsapp-bridge \
--image gcr.io/$PROJECT_ID/whatsapp-bridge \
--platform managed \
--allow-unauthenticated \
--region us-central1 \
--memory 512Mi- For persistence, consider:
- Using a PostgreSQL database (set
DATABASE_URLenvironment variable) - Mounting a persistent volume (for production use)
- Using a PostgreSQL database (set
-
Session Persistence: WhatsApp sessions need to persist between container restarts. Use PostgreSQL for session storage in production.
-
QR Code Access: When deploying to Cloud Run, you'll access the QR code via the deployed URL.
-
Timeouts: Configure Cloud Run with appropriate request timeout settings (recommended: 5-10 minutes) to handle long-running operations.
-
Memory: Allocate at least 512MB of memory to ensure stable operation.
MIT License