Local printer API server for USB printer integration with delivery number system.
- USB printer integration via system print commands
- Print queue management with infinite retry
- Delivery number generation (A-Z cycle: 260 files total)
- File number page separator after every file (File no: 1-10)
- Full-page letter separator after every 10 files
- API key authentication
- Health check endpoint
- Persistent queue storage
- Node.js 18+ installed
- USB printer connected to your computer
- Printer configured in your operating system
-
Clone or download this repository
git clone https://github.com/AdityaPandey-DEV/Printer-API.git cd Printer-API -
Install dependencies
npm install
-
Create
.envfilecp .env.example .env # Or create .env manually -
Configure
.envfilePORT=3001 PRINTER_NAME=HP_Deskjet_525 PRINTER_PATH=/dev/usb/lp0 PRINT_QUEUE_SIZE=100 DELIVERY_NUMBER_START=A API_KEY=your_secure_api_key_here NODE_ENV=production
Important Configuration:
-
PRINTER_NAME: Name of your printer as configured in your OS- Windows: Check in Control Panel > Devices and Printers
- macOS: Check in System Preferences > Printers & Scanners
- Linux: Check with
lpstat -pcommand
-
PRINTER_PATH: Path to printer (usually not needed, but can be set)- Windows: Leave empty or use
COM3format - macOS/Linux: Use
/dev/usb/lp0or similar
- Windows: Leave empty or use
-
API_KEY: Generate a secure random string (use this in funPrinting)# Generate a secure API key (example) node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
-
npm run dev# Build TypeScript
npm run build
# Start server
npm startThe server will start on http://localhost:3001 (or the port specified in .env)
-
Generate a secure API key:
# Option 1: Using Node.js node -e "console.log(require('crypto').randomBytes(32).toString('hex'))" # Option 2: Using OpenSSL openssl rand -hex 32 # Option 3: Using online generator # Visit: https://www.random.org/strings/
-
Add to
.envfile:API_KEY=your_generated_api_key_here
-
Restart the server after updating
.env
Add a print job to the queue.
Headers:
X-API-Key: your_api_key_here
or
Authorization: Bearer your_api_key_here
Body:
{
"fileUrl": "https://example.com/file.pdf",
"fileName": "document.pdf",
"fileType": "application/pdf",
"printingOptions": {
"pageSize": "A4",
"color": "bw",
"sided": "single",
"copies": 1
},
"printerIndex": 1
}Response:
{
"success": true,
"message": "Print job added to queue",
"jobId": "job_1234567890_abc123",
"deliveryNumber": "A202501151"
}Get queue status.
Headers:
X-API-Key: your_api_key_here
Response:
{
"success": true,
"total": 5,
"pending": 2,
"jobs": [...]
}Health check endpoint (no authentication required).
Response:
{
"success": true,
"status": "healthy",
"printer": {
"available": true,
"message": "Printer is available"
},
"queue": {
"total": 0,
"pending": 0
},
"timestamp": "2025-01-15T10:30:00.000Z"
}Format: {LETTER}{YYYYMMDD}{PRINTER_INDEX}
Example: A202501151 = Letter A, Date 2025-01-15, Printer Index 1
Logic:
- Cycles through A-Z (260 files total: 10 files per letter × 26 letters)
- After every file: prints file number page separator (File no: 1, File no: 2, ... File no: 10)
- After every 10 files: prints full-page letter separator (A, B, C...)
- File numbers reset to 1 when moving to next letter
- Date format: YYYYMMDD
- Resets letter and count when date changes
- Jobs are queued and processed sequentially
- Infinite retry on failure (payment is done, so printing must succeed)
- Persistent queue stored in
print-queue.json - Exponential backoff between retries (max 5 minutes)
- Queue survives server restarts
✅ Solutions:
- Use ngrok (recommended for development) - See
SETUP_NETWORK.md - Use Cloudflare Tunnel (recommended for production) - See
SETUP_NETWORK.md - Deploy printer API to VPS (most reliable) - See
SETUP_NETWORK.md
Quick Start with ngrok:
# 1. Install ngrok: brew install ngrok (or download from ngrok.com)
# 2. Start printer API: npm start
# 3. In another terminal: ngrok http 3001
# 4. Copy the forwarding URL (e.g., https://abc123.ngrok-free.app)
# 5. Use this URL in funPrinting PRINTER_API_URLSSee SETUP_NETWORK.md for detailed network setup instructions.
Option A: Using ngrok (Recommended for Development)
-
Install ngrok:
brew install ngrok # macOS # Or download from https://ngrok.com
-
Start printer API:
cd printer-api npm start -
In another terminal, start ngrok:
ngrok http 3001
-
Copy the forwarding URL:
Forwarding: https://abc123.ngrok-free.app -> http://localhost:3001 -
Use this URL in funPrinting:
https://abc123.ngrok-free.app
Option B: Using Local IP (Only if on same network)
-
Find your computer's IP address:
# Windows ipconfig # macOS/Linux ifconfig # or ip addr show
-
Your Printer API URL will be:
http://YOUR_IP_ADDRESS:3001 # Example: http://192.168.1.100:3001Note: This only works if funPrinting can access your local network (usually not possible with cloud hosting).
See SETUP_NETWORK.md for detailed network setup instructions.
-
Open funPrinting
.env.localfile -
Add printer API configuration:
# Printer API Configuration PRINTER_API_URLS=["http://localhost:3001","http://192.168.1.100:3001"] PRINTER_API_TIMEOUT=5000 PRINTER_API_KEY=your_secure_api_key_here INVOICE_ENABLED=true RETRY_QUEUE_ENABLED=true
Important:
PRINTER_API_URLS: Array of printer API URLs (JSON format)- Add multiple URLs if you have multiple printers
- First URL is used by default
PRINTER_API_KEY: Must match theAPI_KEYin printer-api.envPRINTER_API_TIMEOUT: Request timeout in milliseconds
-
Save and restart funPrinting
-
Start Printer API:
cd printer-api npm start -
Test health endpoint:
curl http://localhost:3001/health
-
Test from funPrinting:
- Place a test order
- Complete payment
- Check printer API logs for print job
- Check printer name:
# macOS/Linux lpstat -p # Windows # Control Panel > Devices and Printers
- Update
PRINTER_NAMEin.envto match exactly
- Ensure
API_KEYin printer-api.envmatchesPRINTER_API_KEYin funPrinting.env.local - Check headers are being sent correctly
- Check printer is online and has paper
- Check
print-queue.jsonfile exists and has jobs - Check server logs for errors
- Verify printer API is accessible from funPrinting server
- If funPrinting is on Render (cloud) and printer-api is local:
- Use ngrok or similar tunnel service
- Or use VPN/private network
- Or deploy printer-api to a server accessible from Render
- Ensure
print-queue.jsonfile has write permissions - Check disk space
- Verify
NODE_ENVis set correctly
- Never commit
.envfile to git - Use strong, random API keys
- Consider using HTTPS in production
- Restrict network access to printer API if possible
- Use firewall rules to limit access
printer-api/
├── src/
│ ├── server.ts # Express server setup
│ ├── routes/
│ │ └── print.ts # Print job endpoints
│ ├── services/
│ │ ├── printer.ts # USB printer service
│ │ ├── deliveryNumber.ts # Delivery number logic
│ │ └── queue.ts # Print queue management
│ ├── models/
│ │ └── PrintJob.ts # Print job model
│ └── utils/
│ ├── printerUtils.ts # Printer utilities
│ └── auth.ts # API key authentication
├── dist/ # Compiled JavaScript
├── package.json
├── tsconfig.json
└── README.md
npm run buildnpm testISC
For issues or questions:
- Open an issue on GitHub
- Contact: adityapandey.dev.in@gmail.com
Made with ❤️ for funPrinting Project