Overview
The Create Sessions API supports webhook notifications to receive real-time updates about session lifecycle events and task execution status. When you provide awebhook_url during session creation, the system will automatically send HTTP POST requests to your endpoint when specific events occur.
Configuration
Request Parameter
When creating a session viaPOST /v1/sessions, you can optionally include a webhook_url parameter:
Validation
- The webhook URL must be a valid HTTP or HTTPS URL
- Maximum length: 2,000 characters
- Must start with
http://orhttps://
How It Works
1. Session Creation
When you create a session with awebhook_url:
- The webhook URL is stored with the session record
- A
session.createdwebhook is immediately triggered (non-blocking) - The session creation API returns immediately without waiting for webhook delivery
2. Background Monitoring
After a message is sent to the agent (viaPOST /v1/sessions/{session_id}/message), the system starts a background task that monitors the agent’s event stream. This monitoring:
- Runs independently of any active SSE connections
- Automatically triggers webhooks for terminal events
- Stops monitoring after receiving a terminal event (
done,question, orerror)
3. Webhook Delivery
Webhooks are delivered asynchronously using a fire-and-forget pattern:- Non-blocking: Webhook failures never block session operations
- Automatic retries: Failed webhooks are retried up to 3 times with exponential backoff
- Timeout: Each webhook attempt has a 10-second timeout
- Total retry window: Maximum 30 seconds for all retry attempts
Webhook Events
Session Lifecycle Events
session.created
Triggered immediately after a session is successfully created.
Payload:
session.status_changed
Triggered when a session’s status changes (e.g., paused, resumed, cancelled).
Payload:
session.deleted
Triggered when a session is deleted.
Payload:
Task Execution Events
task.started
Triggered when a task starts (after sending a message to the agent).
Payload:
task.completed
Triggered when a task completes successfully.
Payload:
task.question
Triggered when the agent needs user input to proceed.
Payload:
task.error
Triggered when a task encounters an error.
Payload:
Webhook Request Format
All webhook requests are sent as HTTP POST requests with the following characteristics:Headers
Body
The request body is a JSON object containing:event: The event type (e.g., “session.created”, “task.completed”)timestamp: ISO 8601 timestamp in UTCsession: Session metadata object- Additional event-specific data fields
Implementation Details
Retry Logic
The webhook client implements automatic retry with exponential backoff:- Initial attempt: Immediate
- Retry attempts: Up to 2 additional attempts
- Backoff strategy: Exponential (doubles wait time between retries)
- Maximum retry window: 30 seconds total
- Retry triggers: Network timeouts, HTTP errors, connection failures
Error Handling
- Webhook failures are logged but never raise exceptions
- Session operations continue normally even if webhooks fail
- Failed webhooks are logged with error details for debugging
- After all retries are exhausted, the failure is logged and the system moves on
Background Processing
- Webhooks are triggered using
asyncio.create_task()for non-blocking execution - The
trigger_webhook_background()method returns immediately - Webhook delivery happens asynchronously without blocking the API response
Best Practices
Webhook Endpoint Implementation
Your webhook endpoint should:- Respond quickly: Return a 200 status code within 5 seconds
- Be idempotent: Handle duplicate webhook deliveries gracefully
- Validate requests: Verify the request format and required fields
- Log events: Store webhook events for debugging and auditing
- Handle failures: Implement proper error handling and logging
Example Webhook Handler
Security Considerations
- HTTPS: Always use HTTPS endpoints for webhook URLs
- Authentication: Consider implementing webhook signature verification
- Rate limiting: Be prepared to handle multiple webhooks in quick succession
- Validation: Validate webhook payloads before processing
Monitoring
The system logs webhook delivery attempts with the following log events:webhook_delivered: Successful webhook deliverywebhook_retry: Retry attempt (with attempt number and wait time)webhook_failed_permanently: All retries exhaustedwebhook_notification_error: Unexpected error during webhook processing
Example Usage
Creating a Session with Webhooks
- JavaScript
- Python
- HTTPie