Why Cloudflare Tunnel?
No port forwarding
Works without opening firewall ports or configuring NAT
DDoS protection
Cloudflare’s network protects against DDoS attacks
Free HTTPS
Automatic SSL/TLS certificates with no configuration
Custom domain
Use your own domain for professional access
Prerequisites
- Cloudflare account (free tier works)
- Domain managed by Cloudflare DNS
cloudflaredCLI installed
Setup
Install cloudflared
Download from Cloudflare downloads page:
Create tunnel configuration
Create Replace
~/.cloudflared/config.yml:<your-tunnel-id> with the ID from step 3.Start Cloudflare Tunnel
Use
--protocol http2 instead of QUIC (the default) to avoid timeout issues with long-lived SSE connections.Configuration
HAPI environment variables
Cloudflare Tunnel config.yml
Advanced example with multiple services:The catch-all rule (
http_status:404) is required as the last entry.Run as background service
Using systemd (Linux)
Create/etc/systemd/system/cloudflared.service:
Using pm2
Using macOS launchd
Create~/Library/LaunchAgents/com.cloudflare.tunnel.plist:
Telegram integration
With Cloudflare Tunnel providing HTTPS, you can enable Telegram Mini App:Troubleshooting
Tunnel connects but web app doesn’t load
- Verify
HAPI_PUBLIC_URLmatches your tunnel hostname exactly - Check tunnel is using
--protocol http2(not QUIC) - Ensure HAPI hub is running on port 3006
- Test locally:
curl http://localhost:3006/api/health
SSE not working / Real-time updates failing
- Confirm you’re using a Named Tunnel, not Quick Tunnel (TryCloudflare)
- Add to
config.ymlunderoriginRequest: - Use
--protocol http2when starting tunnel
DNS not resolving
- Verify DNS record created:
cloudflared tunnel route dns hapi hapi.yourdomain.com - Check Cloudflare dashboard → DNS → Records
- DNS propagation can take a few minutes
Connection timeouts
- Increase timeout in
config.yml: - Ensure no firewall blocking localhost:3006
Comparison with relay
| Feature | Relay | Cloudflare Tunnel |
|---|---|---|
| Setup complexity | One command | Initial setup required |
| Custom domain | No | Yes |
| Encryption | WireGuard + TLS | TLS via Cloudflare |
| Latency | Direct peer-to-peer | Through Cloudflare network |
| Cost | Free | Free (Cloudflare Free tier) |
| DDoS protection | No | Yes |
| Configuration | Zero | Tunnel config + DNS |
When to use Cloudflare Tunnel
Ideal when you:- Want a custom domain (e.g.,
hapi.yourcompany.com) - Need DDoS protection and Cloudflare features
- Have a Cloudflare account and domain
- Prefer managed infrastructure over relay
- Want team access with consistent URL
Next steps
Telegram Setup
Enable Telegram Mini App (now that you have HTTPS)
Runner Setup
Configure background runner for remote sessions