Webhook Connection
When an Agent completes a Run, Kolena will send an HTTP request of the Run result to the webhook URL. The server listening for webhook deliveries at that URL can take action when it receives one.Run Result Format
When making a request to your webhook, Kolena will send a JSON object of the following format:agent_id: Agent id.run_id: Agent Run id.run_url: Link to web UI of the Run result.created: Timestamp when the Run result was created.updated: Timestamp when the Run result was last updated.files: List of files of the Run.order: User specified order of the prompts for the Agent. It is mainly used for UI display.data: Prompt results of the Run, a dictionary keyed by prompt name.
Respond to Webhook Request
Your webhook handler should respond to the webhook request with an HTTP 2xx status within ten seconds. We will retry up to three times if it does not, with exponential back off. If the delivery still fails after that, we will mark the delivery as failed, and will schedule another delivery after a longer period, for up to five times. If these attempts still fail, the Run result will be omitted from any further delivery.Example Servers
Below are example servers demonstrating handling webhook deliveries. Since this is an example server, you can use any arbitrary value for theKOLENA_WEBHOOK_SECRET environment variable. When creating your own production server, you should use the secret generated by Kolena after creating the Webhook integration.
- FastAPI
- Flask
main.py
Testing Webhooks
The Python SDK provides a CLI to help test your webhook locally. Install Python SDK and set up an API key. Forward events from an Agent to your webhook by usinglisten command.
--forward option.
You can use the secret from an existing webhook connection, if you already created one, or any arbitrary value for KOLENA_WEBHOOK_SECRET, as we are just locally testing.
The secret passed to the CLI will be used by the kolena_agents CLI to sign the request forwarded to your service.
The --tail <N> option can be provided to fetch N most recent results of the Agent. If --tail isn’t included, the command would only listen to new results.
You can test your Agent with demo data without relying on the Agent having active data using the sample command.
Register Webhook Connection
Once your service is ready to process Kolena webhook events, follow the steps in Connect Agent to set it as a destination for your Agent.Technical Details
Kolena Custom Headers
Kolena includes additional information in request headers:X-Kolena-Timestamp: Timestamp of the requestX-Kolena-Delivery-ID: Unique id for the requestX-Kolena-Signature: HMAC value of request body, timestamp, and Delivery ID signed with webhook secret
Signature Verification
When Kolena makes a request to your webhook URL, it will use the shared secret to create a signature from the timestamp the request is created, the request Delivery ID, and the body of the request. The signature, timestamp and Delivery ID are put in the request headersX-Kolena-Signature, X-Kolena-Timestamp, and X-Kolena-Delivery-ID, respectively.
Upon receiving a request, use the secret to compute the signature from the X-Kolena-Timestamp, X-Kolena-Delivery-ID and request body, and verify the signature you computed matches the value in X-Kolena-Signature.
It is also advised to verify that X-Kolena-Timestamp is within a short time window from the time the request is received.
Kolena provides a utility function webhook.construct_event in our Python SDK. It performs signature verification and returns the parsed JSON object.
