RelayClient
RelayClient manages a persistent WebSocket connection to SignalWire’s Relay
service. It handles authentication, automatic reconnection with exponential
backoff, inbound event dispatch, outbound dialing, and SMS/MMS messaging.
Use it when you need imperative, event-driven control over calls rather than
the declarative AI agent approach.
The client supports two authentication modes: project ID + API token, or JWT token. Credentials can be passed directly or read from environment variables.
Properties
project
SignalWire project ID. Set via constructor or SIGNALWIRE_PROJECT_ID environment variable.
token
API token for authentication. Set via constructor or SIGNALWIRE_API_TOKEN environment variable.
jwt_token
JWT token for alternative authentication. Set via constructor or SIGNALWIRE_JWT_TOKEN environment variable.
When provided, project and token are not required.
host
Relay WebSocket endpoint. The default is the endpoint for SignalWire projects; you
do not set your space here. The constructor argument wins, then the SIGNALWIRE_SPACE
environment variable, then the default. RestClient reads SIGNALWIRE_SPACE as the
REST host, so when it is set for REST, pass host="relay.signalwire.com" to keep
the default.
contexts
List of contexts to subscribe to for inbound call and message events.
max_active_calls
Maximum number of concurrent inbound calls the client will track. Calls
arriving beyond this limit are dropped with a log warning. Set via constructor
or RELAY_MAX_ACTIVE_CALLS environment variable. Constructor-only — not
accessible as a public attribute after initialization.
relay_protocol
Server-assigned protocol string from the connect response. Read-only. Used internally for session resumption on reconnect.
Decorators
on_call
Register the inbound call handler. The decorated function is called once for each
calling.call.receive event on the subscribed contexts. The function receives a
Call object with all call properties
and control methods. Only one call handler can be active at a time — calling
@client.on_call again replaces the previous handler.
on_message
Register the inbound SMS/MMS message handler. The decorated function is called for
each messaging.receive event. The function receives a
Message object with message
properties and state tracking. Only one message handler can be active at a time —
calling @client.on_message again replaces the previous handler.
Methods
Async Context Manager
RelayClient supports async with for scoped connections: