Docs Guides

Calling out from your agent

Your agent places calls with LiveKit's SIP API on the endpoint's outbound trunk. RelayDock checks the caller number and sends the call to your carrier.

1. Find the outbound trunk

RelayDock creates the outbound trunk in your LiveKit project when the endpoint is set up. It is named rd-<SIP ID>-out, and its trunk ID (ST_…) is on the endpoint page under Outbound trunk. Over the API it is platform_objects.outbound_trunk_id:

curl "https://api.relaydock.io/api/v1/endpoints/3f6c2a1e-8b4d-4c7a-9e21-5d0b7f9a6c13" \
  -H "Authorization: Bearer $RELAYDOCK_API_KEY"

The outbound trunk exists when the endpoint has at least one outbound caller number. On a trunk endpoint, turn on Outbound calls with the carrier's SIP address too; without it, calls are rejected with 403 no-outbound.

2. Place the call

From an agent, create a SIP participant on that trunk. This is the call RelayDock's own test agent uses (livekit-agents 1.8):

Python
from livekit import api
from livekit.agents import JobContext

OUTBOUND_TRUNK_ID = "ST_…"  # the endpoint's outbound trunk (rd-<SIP ID>-out)


async def call_out(ctx: JobContext, number: str) -> None:
    # sip_number is left empty: LiveKit uses a number from the trunk, which carries
    # exactly the endpoint's outbound caller numbers.
    await ctx.api.sip.create_sip_participant(
        api.CreateSIPParticipantRequest(
            sip_trunk_id=OUTBOUND_TRUNK_ID,
            sip_call_to=number,  # +E.164, for example "+12025550123"
            room_name=ctx.room.name,
            participant_identity="callee",
            wait_until_answered=True,
        )
    )

The same request exists in LiveKit's server SDKs; see LiveKit's SIP documentation for the other options.

3. Choose the caller number

  • Leave sip_number empty. LiveKit then uses a number from the trunk, and the trunk carries exactly the endpoint's Outbound caller numbers.
  • If you set sip_number, it must be one of those numbers. RelayDock rejects any other caller with 403 caller-not-allowed.
  • Don't pass sip.trunkPhoneNumber. On an inbound call through RelayDock, this participant attribute holds the endpoint's SIP ID (ep-…), not a phone number. If your agent passes it as sip_number anyway, RelayDock uses the endpoint's first outbound caller number instead.

Change the outbound caller numbers on the endpoint (outbound_numbers over the API, at most 20); RelayDock updates the trunk in LiveKit.

Call context on inbound calls

RelayDock adds these attributes to the caller's participant on every inbound call:

Attribute Value
relaydock.called The number the caller dialed, in the format your carrier sends it: with or without +, or in national format.
relaydock.endpointThe endpoint's SIP ID, ep-….
relaydock.forwarded_fromThe forwarding number, when the carrier sends a Diversion header.
Python
from livekit.agents import JobContext


async def entrypoint(ctx: JobContext) -> None:
    await ctx.connect()
    caller = await ctx.wait_for_participant()
    called = caller.attributes.get("relaydock.called", "")       # the number the caller dialed, as the carrier sent it
    endpoint = caller.attributes.get("relaydock.endpoint", "")   # the endpoint's SIP ID, ep-…
    forwarded = caller.attributes.get("relaydock.forwarded_from", "")  # only when the carrier sends a Diversion header

To call out from the number the caller reached (for example to bridge them to a colleague), turn relaydock.called into +E.164 and pass it as sip_number. It works only if that number is one of the endpoint's outbound caller numbers:

Python
import re
from collections.abc import Mapping

_E164 = re.compile(r"\+[1-9][0-9]{6,14}")


def caller_from_called(attributes: Mapping[str, str]) -> str:
    """The dialed number as +E.164, to call out with the number the caller reached.

    Carriers may send it without the plus (Telnyx does). Returns "" when there is no
    usable number: then leave sip_number empty and LiveKit uses the trunk's number.
    """
    called = (attributes.get("relaydock.called") or "").strip()
    number = called if called.startswith("+") else f"+{called}"
    return number if _E164.fullmatch(number) else ""

What RelayDock checks

Before sending your agent's call to the carrier, RelayDock rejects it with one of these reasons. The reason is in the call record (reason) and in the X-RelayDock-Reason header of the SIP response.

Reason SIP Why
caller-not-allowed403The caller isn't one of the endpoint's outbound caller numbers.
invalid-number484The dialed number is empty or, on a trunk, not +E.164.
no-outbound403The trunk endpoint has no carrier SIP address for outbound calls.
no-registration503The registration endpoint isn't registered right now.
emergency-not-supported403Emergency numbers such as 112, 911 or 999 can't be called through RelayDock.
blocked-prefix403The destination is on the fraud-prevention list of premium and satellite prefixes.
daily-cap403Today's outbound minute limit is used up; it resets at midnight UTC.

Limits that apply in both directions (concurrent calls, calls per second, billing) and what to do about each reason are in Troubleshooting.