Skip to main content

Overview

This guide covers how to establish a WebSocket connection, authenticate your POS client, and maintain a healthy connection with proper heartbeat and reconnection handling.
1

Authenticate

Connect with your API key via headers or query parameters
2

Establish Connection

Open WebSocket connection to the API endpoint
3

Implement Heartbeat

Send periodic ping messages to maintain connection health
4

Handle Messages

Process responses and push notifications

Prerequisites

Before connecting, ensure you have:

API Credentials

API key provided by Modulus Labs for your POS integration

WebSocket Client

A WebSocket client library for your programming language

Network Access

Outbound access to the WebSocket endpoint over port 443

JSON Parser

Ability to serialize and parse JSON messages

WebSocket Endpoint

Connect to the WebSocket endpoint:
This is the sandbox endpoint. Contact support@moduluslabs.io for your credentials.

Authentication

Authenticate during the WebSocket handshake with your x-api-key. Prefer the header; a ?x-api-key= query parameter also works but can leak into logs. See Authentication for headers, the query-parameter form, and the full handshake.

Establishing a Connection

Quickstart Flow

1

Connect to WebSocket Endpoint

Establish a WebSocket connection with your API key:
2

Discover Available Terminals

Request a list of connected terminals:
Receive terminal list:
3

Initiate Payment

Send a payment request to a terminal:
4

Receive Payment Result

The terminal processes the payment and returns the result via push notification:

Heartbeat / Keep-Alive

Send a periodic ping action (about every 30s) and expect a pong to keep the connection alive and detect stale sockets. The settings table and the full Node.js/Python heartbeat implementation are in Actions.

Testing Your Connection

1

Connect to the WebSocket endpoint

Verify successful connection by checking for the open event
2

Send getTerminals action

Verify you receive a terminalsResponse with available terminals
3

Test heartbeat

Send a ping and verify you receive a pong response
4

Test reconnection

Simulate a connection drop and verify automatic reconnection works

Quick Connection Test

Troubleshooting

Possible causes:
  • Invalid or missing API key
  • API key not authorized for WebSocket access
  • Network firewall blocking WebSocket connections
Solutions:
  • Verify your API key is correct
  • Contact support@moduluslabs.io to verify API key permissions
  • Check that outbound WebSocket connections are allowed on port 443
Possible causes:
  • Heartbeat not implemented
  • Network instability
  • Firewall or proxy terminating idle connections
Solutions:
  • Implement heartbeat with 30-second ping interval
  • Check network connectivity
  • Configure proxy/firewall to allow persistent WebSocket connections
Possible causes:
  • Multiple POS instances using the same API key
  • Previous connection not properly closed
Solutions:
  • Ensure only one POS instance uses each API key
  • Request additional API keys for multiple terminals
  • Implement proper connection cleanup on application exit
Possible causes:
  • Message handler not processing all action types
  • JSON parsing errors
  • Connection silently dropped
Solutions:
  • Log all incoming messages for debugging
  • Add try/catch around JSON parsing
  • Implement heartbeat to detect stale connections

Security Best Practices

Protect API Keys

Store API keys in environment variables, not in source code. Never expose keys in client-side JavaScript.

Use Headers

Prefer header-based authentication over query parameters to avoid keys appearing in logs.

Validate Messages

Always validate incoming message structure before processing to prevent errors from malformed data.

Secure Reconnection

When reconnecting, ensure you’re connecting to the legitimate endpoint. Validate SSL certificates.

Next Steps

Actions Reference

Learn how to send commands to terminals

Push Notifications

Handle real-time terminal and payment updates

Core Concepts

Device enforcement and reconnection resilience

API Reference

Complete schema reference for all data types