is the LiveKit WebhookEvent a protobuf message? Wh...
# ask-ai
b
is the LiveKit WebhookEvent a protobuf message? What do I use to serialize the webhook?
t
Thanks for reaching out, Vishnu. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Just to clarify, the
WebhookEvent
is defined as a Protocol Buffers (protobuf) message, but when LiveKit sends webhooks, they actually come through as JSON—not in the binary protobuf format. Here’s the scoop: • The webhook body is encoded as JSON. • The
Content-Type
header is set to
application/webhook+json
. • SDKs use
protojson
to convert the
WebhookEvent
message back and forth between JSON and protobuf format. For example, if you’re using Go, you can use
protojson.Unmarshal
to turn the JSON body into a
WebhookEvent
. And in Node.js, the
WebhookEvent
class has a handy
.fromJsonString()
method to do just that. If you want, I’m happy to share more details or examples for any specific SDK you’re working with—just let me know! Sources: packages/livekit-server-sdk/src/WebhookReceiver.ts | livekit-api/livekit/api/webhook.py | webhook/resource_url_notifier.go | Webhooks | LiveKit Docs
b
Python MessageToDict?
t
MessageToDict
is a handy function that helps convert Protocol Buffer message objects into Python dictionaries. Even though LiveKit webhooks send the
WebhookEvent
as JSON in the request body,
MessageToDict
can come in really useful in more advanced situations, like when you're logging or debugging protobuf-based data. For most webhook processing, the LiveKit SDKs take care of JSON deserialization for you, so you usually don’t need to use
MessageToDict
directly. But if you’re dealing with protobuf objects—for example, for logging or analytics—then
MessageToDict
from
google.protobuf.json_format
can make it easier to get readable data out of them. Feel free to ask if you want me to walk you through it with examples or if anything’s unclear—I’m here to help! Sources: Webhooks | LiveKit Docs | examples/primitives/room_stats.py | packages/livekit-server-sdk/src/WebhookReceiver.ts
b
I'm trying to forward the webhook after adding some extra attributes. Would this work:
Copy code
webhook_data = {
        "webhook_event": MessageToDict(event),  # Forward complete WebhookEvent
        # Add local dev context
        "local_dev_context": {
            "is_local_dev": True,
            "local_user": get_current_dev_user(),
            "forwarded_from_production": True,
        },
    }
t
Your approach to forwarding the webhook after adding extra attributes looks spot on! Let me break it down for you: 1. Convert WebhookEvent to Dictionary: Using
MessageToDict(event)
is a great way to turn the protobuf message into a Python dictionary so you can easily work with it. 2. Add Custom Attributes: Adding a
local_dev_context
field with your custom attributes is super straightforward and fits perfectly with how dictionaries work in Python. 3. Forward the Modified Payload: You can send the updated dictionary as JSON to your desired endpoint using an HTTP client like
requests
. Simple and effective! Here’s a quick example to show how you might do this:
Copy code
from google.protobuf.json_format import MessageToDict
import requests

# Convert WebhookEvent to dictionary
webhook_data = {
    "webhook_event": MessageToDict(event),
    "local_dev_context": {
        "is_local_dev": True,
        "local_user": get_current_dev_user(),
        "forwarded_from_production": True,
    },
}

# Forward the modified payload
forward_url = "https://your-destination.com/webhook"
response = requests.post(forward_url, json=webhook_data, headers={"Content-Type": "application/json"})
If you want me to walk through anything else or share more examples, just let me know—I’m here to help! Sources: livekit-protocol/livekit/protocol/webhook.py | SIP dispatch rule | LiveKit Docs | SIP participant | LiveKit Docs | Webhooks | LiveKit Docs | SIP dispatch rule | LiveKit Docs