Skip to main content

Architecture

PSTN callers dial your number; your SIP provider forwards the call to the Zero Runtime SIP Gateway, and a routing rule routes it to your self-hosted agent by matching its agent_id.
For outbound calls, your agent dials through an outbound gateway to the SIP provider, which places the PSTN call.
Telephony call flow diagram showing inbound and outbound PSTN call routing through the ZRT SIP Gateway.

Prerequisites

  • Python 3.11 or higher.
  • A ZRT Auth token (ZRT_AUTH_TOKEN). Generate one in the Zero Runtime Dashboard.
  • A SIP provider (such as Twilio) with a phone number, plus access to its origination and termination settings.

Setup the project

Use Python 3.11 or higher, then install the Zero Runtime Agents SDK. Create and activate a virtual environment, then install zrt and python-dotenv. Every provider plugin ships with zrt.
New to uv? See the uv install guide.

Configure environment variables

Store API keys and tokens in a .env file in your project root.
.env

Build the agent

For telephony, the agent must register itself so calls can be routed to it. zrt.serve() registers the agent under its agent_id automatically. The agent half is the same as any voice agent. Pick your language.For telephony, the agent must register itself with Zero Runtime so calls can be routed to it. Give the agent a unique agent_id and start it with zrt.serve(), which registers it automatically. Inbound calls arrive through routing, so there is no need to self-invoke a session.
main.py
With your .env configured and dependencies installed, run the agent:
Keep the process running. It registers with Zero Runtime using the ID MyTelephonyAgent.
Agent running locally and registering with Zero Runtime

Add SIP Configuration

  1. Go to the Zero Runtime Dashboard.
  2. Click Add Number.
  3. Click Configure SIP.
  4. Give a name and add your phone number.

Configure Zero Runtime gateways

Set up two gateways and point your SIP provider (Twilio in this example) at them so calls flow both ways. Use the Dashboard or the API for each.1. Inbound gateway: the entry point for incoming calls.
  1. Copy the Inbound URL from the Zero Runtime dashboard.
  2. Go to Twilio and create a new SIP Trunk.
  3. Go to the Origination section, paste the Inbound URL there, and save it.
2. Outbound gateway: lets the agent dial out to the PSTN.
  1. In Twilio, open the same SIP Trunk and go to the Termination section. Create a Termination URI and add username/password credentials.
  2. Back in the Zero Runtime Dashboard, open the outbound gateway configuration.
  3. Paste the Twilio Termination URI as the address, add the same username/password credentials, and save.

Create a routing rule

Connect the inbound gateway to your agent by ID. The agent ID must match the agent_id you pass to your agent (registered by zrt.serve()).
  1. From your inbound gateway, click Configure rule, then Create new routing rule.
  2. Enter a Routing Rule Name and select API Key authentication.
  3. Choose the Call Direction (Inbound).
  4. Enter the Phone Number and select a Room Type.
  5. Enter the Agent ID (e.g. MyTelephonyAgent) and click Save.

Place a test call

Make sure your main.py is running locally before configuring the telephony settings. The agent must be active to receive incoming calls.
Dial your provider’s phone number from any phone. The worker waits for the caller to join, then greets you as soon as the call connects.

Verify

  • The agent registers under the ID MyTelephonyAgent and the worker process stays running.
  • An incoming call reaches the agent and you hear the greeting.
  • Outbound calls dial the target number through your outbound gateway.
Common errors:
  • agent_id mismatch: the routing rule’s agentId must exactly match the agent’s agent_id.
  • Agent not running: the agent process must be alive to accept the inbound call.
  • SIP misconfiguration: the provider’s origination and termination URIs must point at your Zero Runtime gateways.

What’s Next

Telephony overview

Explore SIP connect, call routing, and integrations.

Managing calls

Handle call transfer, DTMF events, and webhooks.

References