New protocol version released: This page may contain outdated information.
API Reference
Complete reference documentation for the HAIP Server API, including all methods, events, and configuration options.Server Class
HAIPServer
The main server class that handles all HAI Protocol operations.Constructor
config- Configuration object (optional)
Configuration
HAIPServerConfig
FlowControlConfig
Public Methods
start()
Start the server and begin accepting connections.stop()
Stop the server and close all connections.registerTool()
Register a new tool with the server.tool- Tool definition object
unregisterTool()
Remove a tool from the server.toolName- Name of the tool to remove
getTools()
Get all registered tools.getSession()
Get a specific session by ID.sessionId- Session identifier
getStats()
Get current server statistics.broadcast()
Send a message to all connected sessions.message- HAIP message to broadcast
sendMessage()
Send a message to a specific session.sessionId- Target session IDmessage- HAIP message to send
sendToolDone()
Send a tool completion message to a session.sessionId- Target session IDcallId- Tool call identifierstatus- Completion status (‘OK’, ‘ERROR’, ‘CANCELLED’)result- Tool result (optional)
closeAllConnections()
Close all WebSocket connections.Events
The HAIPServer extends EventEmitter and emits various events.Server Events
’started’
Emitted when the server starts successfully.‘stopped’
Emitted when the server stops.Connection Events
’connect’
Emitted when a new client connects.‘disconnect’
Emitted when a client disconnects.‘handshake’
Emitted when a client completes handshake.Message Events
’textMessage’
Emitted when a text message is received.‘audioChunk’
Emitted when an audio chunk is received.‘binary’
Emitted when binary data is received.Run Events
’runStarted’
Emitted when a run starts.‘runFinished’
Emitted when a run finishes.‘runCancelled’
Emitted when a run is cancelled.Tool Events
’toolCall’
Emitted when a tool is called.Error Events
’error’
Emitted when an error occurs.HTTP Endpoints
Health Check
Endpoint:GET /health
Response:
Statistics
Endpoint:GET /stats
Response:
WebSocket Endpoints
WebSocket Connection
Endpoint:ws://localhost:8080/haip/websocket?token=<jwt-token>
Parameters:
token- JWT authentication token (required)
SSE Endpoints
Server-Sent Events
Endpoint:GET /haip/sse?token=<jwt-token>
Parameters:
token- JWT authentication token (required)
HTTP Streaming Endpoints
HTTP Streaming
Endpoint:POST /haip/stream
Headers:
Authorization: Bearer <jwt-token>(required)Content-Type: application/json
Types
HAIPSession
HAIPServerStats
HAIPRun
HAIPToolExecution
HAIPToolDefinition
Error Codes
Authentication Errors
INVALID_TOKEN- JWT token is invalid or expiredMISSING_TOKEN- JWT token is required but not provided
Message Errors
INVALID_MESSAGE- Message format is invalidUNSUPPORTED_TYPE- Message type is not supported
Tool Errors
TOOL_NOT_FOUND- Requested tool does not existTOOL_EXECUTION_ERROR- Tool execution failed
Flow Control Errors
INSUFFICIENT_CREDITS- Not enough credits for the channelCHANNEL_PAUSED- Channel is paused
Run Errors
MISSING_RUN_ID- Run ID is required but not providedRUN_NOT_FOUND- Run does not existMAX_RUNS_EXCEEDED- Maximum concurrent runs exceeded
Environment Variables
Required
JWT_SECRET- Secret key for JWT token signing
Optional
PORT- Server port (default: 8080)HOST- Server host (default: 0.0.0.0)NODE_ENV- Environment (development/production)JWT_EXPIRES_IN- JWT expiration time (default: 24h)MAX_CONNECTIONS- Maximum connections (default: 1000)HEARTBEAT_INTERVAL- Heartbeat interval in ms (default: 30000)HEARTBEAT_TIMEOUT- Heartbeat timeout in ms (default: 5000)ENABLE_CORS- Enable CORS (default: true)ENABLE_COMPRESSION- Enable compression (default: true)ENABLE_LOGGING- Enable logging (default: true)
Flow Control
FLOW_CONTROL_ENABLED- Enable flow control (default: true)FLOW_CONTROL_INITIAL_CREDITS- Initial credits (default: 1000)FLOW_CONTROL_MIN_CREDITS- Minimum credits (default: 100)FLOW_CONTROL_MAX_CREDITS- Maximum credits (default: 10000)FLOW_CONTROL_CREDIT_THRESHOLD- Credit threshold (default: 200)FLOW_CONTROL_BACK_PRESSURE_THRESHOLD- Back-pressure threshold (default: 0.8)FLOW_CONTROL_ADAPTIVE_ADJUSTMENT- Adaptive adjustment (default: true)FLOW_CONTROL_INITIAL_CREDIT_MESSAGES- Initial message credits (default: 1000)FLOW_CONTROL_INITIAL_CREDIT_BYTES- Initial byte credits (default: 1048576)
Examples
Basic Server Setup
Custom Tool Implementation
Monitoring and Statistics
Next Steps
- Quick Start - Get up and running quickly
- Configuration - Configure the server
- Tools - Add custom tools
- Deployment - Deploy to production