How to pass SIP header while making outbound call ...
# ask-ai
f
How to pass SIP header while making outbound call in livekit agents python library
t
Thanks for reaching out, Amjad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking to pass SIP headers during an outbound call using the LiveKit Agents Python library! You can easily do this by using the
headers
field in the
CreateSIPParticipantRequest
object. This lets you include your custom SIP X-* headers right in the INVITE request. Here’s a friendly example to guide you:
Copy code
from livekit import api

# Initialize the Livekit API client
livekit_api = api.ApiClient(
    api_key="your-api-key",
    api_secret="your-api-secret",
    base_url="your-livekit-server-url"
)

# Define custom SIP headers
custom_headers = {
    "X-Custom-Header": "value1",
    "X-Another-Header": "value2"
}

# Create SIP participant with custom headers
response = livekit_api.sip.create_sip_participant(
    livekit_api.sip.CreateSIPParticipantRequest(
        sip_trunk_id="your-sip-trunk-id",
        sip_call_to="+1234567890",  # The phone number to call
        room_name="outbound-call-room",
        participant_identity="caller-identity",
        headers=custom_headers,  # Pass the custom headers here
        wait_until_answered=True
    )
)

print("SIP Call ID:", response.sip_call_id)
Just make sure to replace placeholders like
"your-api-key"
,
"your-api-secret"
, and
"your-livekit-server-url"
with your actual LiveKit credentials and server URL. Also, update
"your-sip-trunk-id"
with the ID of your configured SIP trunk. The
headers
dictionary holds the custom X-* headers you want to send with the SIP INVITE request. These will be passed exactly as you set them to the SIP endpoint, which can be really helpful for routing, logging, or any other needs your SIP setup supports. Sources: livekit/livekit_sip.pb.go | livekit-protocol/livekit/protocol/sip.pyi | test/integration/sip_test.go
f
would this work for
From
and
P-Asserted-Identity
headers?
t
Absolutely, you can pass the
From
and
P-Asserted-Identity
headers using the
headers
field in the
CreateSIPParticipantRequest
object. Here’s a friendly example to guide you:
Copy code
from livekit.protocol.sip import CreateSIPParticipantRequest

create_request = CreateSIPParticipantRequest(
    sip_trunk_id="your-trunk-id",
    sip_call_to="+1234567890",
    room_name="my-room",
    participant_identity="sip-participant",
    headers={
        "From": "sip:caller@example.com",
        "P-Asserted-Identity": "sip:asserted@example.com"
    }
)
Just a couple of important things to keep in mind: • Your SIP provider needs to allow modification of these headers. Sometimes providers might restrict or overwrite them for security reasons. • For setting the actual calling number (caller ID), it’s best to use the
sip_number
field in
CreateSIPParticipantRequest
to make sure everything routes correctly. • Headers set at the SIP trunk level will combine with or be overridden by the headers you specify in the request. It’s a good idea to double-check with your SIP provider to ensure they support this, so you can avoid any call rejections or unexpected header changes. If you have any more questions, I’m here to help! Sources: protobufs/livekit_sip.proto | SIP APIs | LiveKit docs | Preserving Caller ID During SIP Transfers in LiveKit Agents