Overview
The VoicePact WebSocket API enables real-time, bidirectional communication for contract status updates, payment notifications, and system events. Each client connects with a unique identifier and can receive both personal and broadcast messages.Connection Endpoint
WebSocket URL
client_id(string, required): Unique identifier for the client connection, typically a phone number
Connecting to WebSocket
JavaScript Example
Python Example
Message Types
Client-to-Server Messages
Ping
Check connection health and latency. Request:Contract Status Request
Query the status of a specific contract. Request:Server-to-Client Messages
Contract Update
Sent when a contract status changes. This message is sent only to parties involved in the contract. Payload:type: Always"contract_update"contract_id: Unique identifier of the contractstatus: New status of the contract (e.g., “active”, “completed”, “terminated”)timestamp: Unix timestamp (seconds) when the update occurred
Payment Update
Broadcast to all connected clients when a payment event occurs. Payload:type: Always"payment_update"contract_id: Contract associated with the paymentstatus: Payment status (e.g., “pending”, “completed”, “failed”)amount: Payment amount in USDtimestamp: Unix timestamp (seconds) when the payment was processed
Connection Lifecycle
1. Connection Establishment
When a client connects, the server:- Accepts the WebSocket connection
- Registers the client with the provided
client_id - Logs the connection event
2. Active Connection
During an active connection:- Client can send messages to the server
- Server can send personal messages to the specific client
- Server can broadcast messages to all connected clients
- Connection is maintained until explicitly closed or network failure
3. Disconnection
When a client disconnects:- Server removes the client from active connections
- Logs the disconnection event
- No further messages will be sent to that client
Error Handling
Invalid JSON
If the server receives malformed JSON, it logs an error but maintains the connection. The client will not receive a response for invalid messages.Unknown Message Types
Messages with unrecognizedtype fields are logged but ignored. The connection remains active.
Connection Drops
If the WebSocket connection is interrupted:- Client should implement reconnection logic with exponential backoff
- Use the same
client_idto maintain consistency - Re-subscribe to any necessary event streams after reconnection
Best Practices
Implement Heartbeat
Implement Heartbeat
Send periodic
ping messages to detect connection failures early and maintain connection health.Handle Reconnection
Handle Reconnection
Implement automatic reconnection with exponential backoff to handle temporary network issues.
Validate Message Format
Validate Message Format
Always validate incoming messages before processing to prevent errors from unexpected payloads.
Use Unique Client IDs
Use Unique Client IDs
Each connection should have a unique
client_id. In VoicePact, phone numbers are typically used as client identifiers to ensure users receive updates specific to their contracts.Rate Limits
The WebSocket API currently does not enforce rate limits, but clients should avoid sending excessive messages to prevent server overload.
Security Considerations
Next Steps
REST API
Explore the REST API for contract and payment operations
Webhooks
Set up webhooks for server-to-server notifications