Skip to main content
This guide covers installing HAPI, configuring the hub, and setting up optional features like Telegram notifications and the background runner.

Prerequisites

HAPI requires at least one AI agent CLI:
npm install -g @anthropic-ai/claude-code
claude --version

Install HAPI CLI

Architecture Overview

HAPI has three components:
ComponentRoleRequired
CLIWraps AI agents (Claude/Codex/Cursor/Gemini/OpenCode), runs sessionsYes
HubCentral coordinator: persistence, real-time sync, remote accessYes
RunnerBackground service for remote session spawningOptional

How They Work Together

┌─────────────────────────────────────────────────────┐
│              Your Machine                           │
│                                                     │
│  ┌─────────┐    Socket.IO    ┌─────────────┐       │
│  │  CLI    │◄───────────────►│    Hub      │       │
│  │+ Agent  │                 │  + SQLite   │       │
│  └─────────┘                 └──────┬──────┘       │
│       ▲                             │ SSE          │
│       │ spawn                       ▼              │
│  ┌────┴────┐                 ┌─────────────┐       │
│  │ Runner  │◄────RPC────────►│   Web App   │       │
│  │ (后台)  │                 └─────────────┘       │
│  └─────────┘                                       │
└─────────────────────────────────────────────────────┘

           [Tunnel / Public URL]

              ┌─────▼─────┐
              │ Phone/Web │
              └───────────┘
Workflow:
  • CLI: Start a session with hapi. Wraps your AI agent and syncs with the hub.
  • Hub: Run hapi hub. Stores sessions, handles permissions, enables remote access.
  • Runner: Run hapi runner start. Spawn sessions from phone/web without keeping terminal open.

Hub Setup

The public relay provides instant remote access with end-to-end encryption:
hapi hub --relay
You’ll see:
┌─────────────────────────────────────────────────────────┐
│                                                         │
│   HAPI Hub Started                                      │
│                                                         │
│   Local:   http://localhost:3006                        │
│   Remote:  https://abc123.relay.hapi.run                │
│                                                         │
│   Access Token: hapi_1a2b3c4d5e6f...                    │
│   (saved to ~/.hapi/settings.json)                      │
│                                                         │
│   [QR CODE]                                             │
│                                                         │
└─────────────────────────────────────────────────────────┘
The relay uses WireGuard + TLS for end-to-end encryption. Your data is encrypted from your device to your machine—the relay only forwards packets it cannot read.
UDP blocked? Some networks block UDP traffic. Force TCP mode:
HAPI_RELAY_FORCE_TCP=true hapi hub --relay

Option 2: Local Only

Run the hub without remote access:
hapi hub
# or
hapi hub --no-relay
The hub listens on http://localhost:3006 by default. Access from other devices on your local network:
http://<your-computer-ip>:3006

Option 3: Self-Hosted Tunnels

For lower latency or self-managed infrastructure, use your own tunnel:
Cloudflare Tunnel provides secure HTTPS access without exposing ports.
Quick Tunnels not supported! Cloudflare Quick Tunnels (TryCloudflare) do not support SSE, which HAPI requires. Use a Named Tunnel instead.
Setup:
# Install cloudflared
# https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/

# Create named tunnel
cloudflared tunnel create hapi
cloudflared tunnel route dns hapi hapi.yourdomain.com

# Start tunnel (use http2 protocol)
cloudflared tunnel --protocol http2 run hapi
Use --protocol http2 instead of QUIC (default) to avoid timeout issues with long-lived connections.
In a separate terminal:
export HAPI_PUBLIC_URL="https://hapi.yourdomain.com"
hapi hub

CLI Configuration

If your hub is not on localhost, configure the CLI:
export HAPI_API_URL="http://your-hub:3006"
export CLI_API_TOKEN="your-token-here"
Or use interactive login:
hapi auth login
Authentication Commands:
hapi auth status          # Show current config
hapi auth login           # Save token interactively
hapi auth logout          # Clear saved credentials

Configuration Priority

ENV > settings.json > default
When ENV values are set, they’re automatically saved to settings.json.

Configuration Files

~/.hapi/
├── settings.json         # Main configuration
├── hapi.db              # SQLite database (hub)
├── runner.state.json    # Runner process state
└── logs/                # Log files
settings.json example:
{
  "$schema": "https://hapi.run/docs/schemas/settings.schema.json",
  "cliApiToken": "hapi_1a2b3c4d5e6f...",
  "apiUrl": "http://localhost:3006",
  "machineId": "machine_xyz789",
  "listenHost": "0.0.0.0",
  "listenPort": 3006,
  "publicUrl": "https://your-domain.com"
}

Environment Variables

Required

VariableDefaultDescription
CLI_API_TOKENAuto-generatedShared secret for authentication
HAPI_API_URLhttp://localhost:3006Hub URL for CLI connections

Optional

VariableDefaultDescription
HAPI_HOME~/.hapiConfig/data directory
HAPI_LISTEN_HOST127.0.0.1Hub HTTP bind address
HAPI_LISTEN_PORT3006Hub HTTP port
HAPI_PUBLIC_URL-Public URL for external access
CORS_ORIGINS-Allowed CORS origins (comma-separated)
HAPI_RELAY_FORCE_TCPfalseForce TCP mode for relay
HAPI_EXPERIMENTALfalseEnable experimental features
HAPI_CLAUDE_PATH-Custom Claude executable path

Telegram

VariableDescription
TELEGRAM_BOT_TOKENToken from @BotFather
TELEGRAM_NOTIFICATIONEnable notifications (default: true)

Voice Assistant

VariableDescription
ELEVENLABS_API_KEYElevenLabs API key
ELEVENLABS_AGENT_IDCustom agent ID (auto-created if not set)

Runner

VariableDefaultDescription
HAPI_RUNNER_HEARTBEAT_INTERVAL60000Heartbeat interval (ms)
HAPI_RUNNER_HTTP_TIMEOUT10000HTTP timeout (ms)

Runner Setup

The runner enables remote session spawning from your phone:
hapi runner start
Benefits:
  • Spawn sessions remotely without terminal access
  • Sessions persist after terminal closes
  • Your machine appears in the “Machines” list
Runner Commands:
hapi runner start         # Start runner as detached process
hapi runner stop          # Stop runner gracefully
hapi runner status        # Show diagnostics
hapi runner list          # List active sessions
hapi runner logs          # Print log file path
hapi runner stop-session <id>  # Terminate specific session
Check Status:
hapi runner status
Output:
✓ Runner is running
  PID: 12345
  Port: 3007
  Uptime: 2h 34m
  Sessions: 3 active

Background Service Deployment

Keep HAPI running persistently across terminal closes and system restarts.
Simple one-liner for background runs:
# Hub
nohup hapi hub --relay > ~/.hapi/logs/hub.log 2>&1 &

# Runner
nohup hapi runner start --foreground > ~/.hapi/logs/runner.log 2>&1 &
View logs:
tail -f ~/.hapi/logs/hub.log
tail -f ~/.hapi/logs/runner.log
Stop:
pkill -f "hapi hub"
pkill -f "hapi runner"

Telegram Setup

Enable Telegram notifications and Mini App access:
1

Create Bot

Message @BotFather on Telegram:
/newbot
Follow prompts to create your bot and get the token.
2

Configure Hub

export TELEGRAM_BOT_TOKEN="your-bot-token"
export HAPI_PUBLIC_URL="https://your-public-url"

hapi hub --relay
Telegram Mini Apps require HTTPS and a public URL. Use the relay or a tunnel.
3

Bind Account

  1. Message your bot with /start
  2. Tap “Open App”
  3. Enter your CLI_API_TOKEN when prompted
You’ll now receive notifications for permission requests and session events.

Voice Assistant Setup

Enable voice control with ElevenLabs:
1

Get API Key

Sign up at elevenlabs.io and create an API key in Settings → API Keys.
2

Configure Hub

export ELEVENLABS_API_KEY="your-api-key"
hapi hub --relay
HAPI will auto-create an agent if ELEVENLABS_AGENT_ID is not set.
3

Use Voice

In the web app:
  1. Open a session
  2. Click the microphone button
  3. Start speaking
The AI will respond with voice and execute commands.

Diagnostics

Run full system diagnostics:
hapi doctor
Output:
✓ Hub connectivity: OK (http://localhost:3006)
✓ Authentication: Valid token
✓ Claude CLI: Found at /usr/local/bin/claude (v1.2.3)
✓ Cursor Agent: Found at /usr/local/bin/agent (v0.9.1)
✓ OpenCode: Not found
✓ Runner: Running (PID 12345, port 3007)
✓ Sessions: 2 active, 15 archived
✓ Database: ~/.hapi/hapi.db (4.2 MB)

Recent logs:
  [2024-03-06 10:32:15] Session started: session_abc123
  [2024-03-06 10:35:42] Permission approved: edit file.ts
Clean up runaway processes:
hapi doctor clean

Security Best Practices

Token Security:
  • Keep your CLI_API_TOKEN secret
  • Rotate tokens if compromised
  • Use strong, unique tokens for production
Network Security:
  • Always use HTTPS for public access
  • Restrict CORS origins in production
  • Use firewall rules to limit access
Firewall Example (ufw):
# Allow from local network only
ufw allow from 192.168.1.0/24 to any port 3006
Rotate Access Token:
# Generate new token
rm ~/.hapi/settings.json
hapi hub --relay

# Update CLI
hapi auth login

Next Steps

Quickstart

Get up and running in 2 minutes

How It Works

Understand the architecture

GitHub

View source and contribute

Telegram Community

Join the discussion

Build docs developers (and LLMs) love