Docs Guides
Transfers
To hand a caller over to another number, your agent asks LiveKit to send a SIP REFER to the carrier. If the carrier refuses, the agent can bridge the call instead by calling the number on the endpoint's outbound trunk.
REFER: the carrier transfers the call
With a REFER, LiveKit asks your carrier to connect the caller to the new number; the call then leaves LiveKit and RelayDock. Call transfer_sip_participant with the caller's identity and a target URI:
- LiveKit accepts
sip:,sips:ortel:targets. Write a phone number astel:+12025550123; a bare+12025550123is rejected with400 invalid_argument. - LiveKit waits for the carrier's final answer before it returns, so allow about 45 seconds, and check the response's status: newer
livekit-apiversions report a failed transfer in the response instead of raising an error. - Your carrier must allow REFER on the connection. Telnyx does: RelayDock verified REFER with live traffic (
202, then a successfulNOTIFY).
Bridge: your agent calls the number
If the REFER fails, create a SIP participant for the new number in the same room, on the endpoint's outbound trunk. The caller and the new party then talk through LiveKit; stop your agent's own audio once the bridge is up. The bridge is an outbound call: the caller number rules apply, and its minutes count.
To show the caller's dialed number to the new party, pass it as sip_number. It must be one of the endpoint's outbound caller numbers; if it isn't, leave sip_number empty.
Code
This is the transfer logic of RelayDock's own test agent (livekit-agents 1.8): REFER first, bridge as the fallback.
import asyncio
import logging
import re
import time
from collections.abc import Mapping
from livekit import api, rtc
from livekit.agents import JobContext
logger = logging.getLogger("transfer")
_E164 = re.compile(r"\+[1-9][0-9]{6,14}")
# livekit-protocol SIPTransferStatus.STS_TRANSFER_FAILED. Newer livekit-api versions return this
# status for a failed transfer instead of raising.
STS_TRANSFER_FAILED = 1
# LiveKit waits for the carrier's final NOTIFY (about 30 s of ringing) before it answers.
REFER_TIMEOUT = 45.0
def refer_target(number: str) -> str:
"""LiveKit accepts sip:, sips: or tel: targets; a bare +E.164 number becomes a tel: URI."""
target = number.strip()
return f"tel:{target}" if _E164.fullmatch(target) else target
def caller_from_called(attributes: Mapping[str, str]) -> str:
"""The number the caller dialed, as +E.164 (empty if there is none)."""
called = (attributes.get("relaydock.called") or "").strip()
number = called if called.startswith("+") else f"+{called}"
return number if _E164.fullmatch(number) else ""
async def transfer(ctx: JobContext, caller: rtc.RemoteParticipant, number: str, outbound_trunk_id: str) -> str:
"""Transfers the caller to number (+E.164). Returns "refer", "bridge" or "failed"."""
try:
response = await asyncio.wait_for(
ctx.api.sip.transfer_sip_participant(
api.TransferSIPParticipantRequest(
room_name=ctx.room.name,
participant_identity=caller.identity,
transfer_to=refer_target(number),
)
),
REFER_TIMEOUT,
)
if getattr(response, "status", None) != STS_TRANSFER_FAILED:
return "refer"
logger.warning("the carrier reported the transfer as failed")
except Exception as exc: # the carrier rejected the REFER, or it timed out
logger.warning("REFER failed: %s", exc)
try:
# Bridge: call the number on the endpoint's outbound trunk and add it to this room.
await ctx.api.sip.create_sip_participant(
api.CreateSIPParticipantRequest(
sip_trunk_id=outbound_trunk_id,
sip_call_to=number,
sip_number=caller_from_called(caller.attributes),
room_name=ctx.room.name,
participant_identity=f"xfer-{int(time.time())}",
wait_until_answered=True,
)
)
except Exception as exc:
logger.error("bridge failed: %s", exc)
return "failed"
return "bridge"Guard against starting a second transfer while one is running, for example when the caller presses the same key twice.
What RelayDock records
The call's timeline in the panel (and in GET /calls/{id}) gets a step for the REFER:
call.transferred(Transferred) when the carrier accepted it, withtransfer_code, for example202. The event feed shows Call transferred.call.transfer_failed(Transfer failed) with the carrier's code.
A bridge appears as a separate outbound call in your call list.