Build a WhatsApp Train Running Status & PNR Bot in Node.js
Step-by-step guide to building a production-ready WhatsApp bot for live Indian Railways GPS tracking, PNR verification, and seat availability using Baileys and the irctc-api SDK.
- •Node.js 18+ LTS installed on your machine or VPS
- •An active WhatsApp account on your mobile phone
- •Free IRCTC API Key from https://irctc.dev/dashboard
User sends a WhatsApp message (!live 12558 or !pnr 2265248473) ➔ Baileys socket receives text ➔ Command router dispatches query to https://api.irctc.dev with caching ➔ Bot sends back a richly formatted WhatsApp message with delays and berth status.
Initialize Node.js Project & Install Dependencies
Create a new project directory and install @whiskeysockets/baileys for WhatsApp Web socket connectivity, qrcode-terminal for initial login scanning, irctc-api for railway data, and dotenv for environment variable security.
Configure Environment Variables (.env)
Create a .env file in the root directory to store your secret IRCTC Developer API key securely. Never commit this file to version control.
Initialize the IRCTC API Client & WhatsApp Socket
Create index.ts and initialize the Baileys socket with persistent multi-file authentication credentials so you only need to scan the QR code once.
Handle Incoming Messages & Command Parsing
Listen to incoming messages via sock.ev.on("messages.upsert") and parse commands: !live <trainNo>, !pnr <pnrNumber>, and !help.
Add In-Memory Request Caching to Protect Quotas
When hundreds of users query the same popular train (e.g. 12558 Sapt Kranti), caching the result for 60 seconds saves API credits and cuts response time to under 5ms.
Run 24/7 in Background with PM2 on VPS
Compile TypeScript to JavaScript and configure PM2 process manager to ensure your bot auto-restarts on server reboots or unexpected crashes.
Live Train Running Status API
Test live queries and inspect sub-100ms response payloads directly in the browser sandbox.
Frequently Asked Questions
Related Tutorials
View All Guides →Build a Telegram Alert Bot for Vacant Berths After Chart Preparation
Learn how to monitor Indian Railways chart preparation 4 hours before departure and send instant Telegram push notifications when vacant berths open for Current Booking.
Check Indian Railway PNR Status Programmatically
The definitive developer guide to querying live 10-digit PNR status, passenger manifests, berth allocations, and charting status with Node.js and Python.
Automated 14-Day Seat Availability Tracker in Python
Monitor Indian Railways seat availability across classes and quotas with a recurring Python daemon and instant Telegram/Discord alerts.