Prerequisites
- Azure CLI installed and signed in. For example:
az --versionandaz login. See How to install the Azure CLI. - An Azure OpenAI resource endpoint (for example,
https://{resource}.openai.azure.com/) and an API key available asAZURE_OPENAI_API_KEY.
Webhook server setup
A webhook server is an application that listens for and processes automated messages (webhooks) sent by Azure OpenAI when specific events occur.Create the webhook listener application
Create a file calledapp.py with the following Flask application that receives and processes webhook events:
Create a requirements.txt file
Create arequirements.txt file in the same directory as your app.py file:
Create an Azure web app
Deploy your webhook server usingaz webapp up. The command must be run from the folder where the app.py code for your application is located.
- Create a resource group if it doesn’t exist.
- Create an App Service plan.
- Create a web app.
- Deploy your code.
https://unique-webhook-handler-name.azurewebsites.net/webhook
Create webhook endpoint
With Azure OpenAI webhook endpoints must be created using the REST API. Register your listener to receive webhook events for specific event types.The signing secret is only shown once during creation. Secrets can be stored securely using Azure Key Vault.
Configure webhook secret in Azure Web App
Set the webhook signing secret as an environment variable in your Azure Web App:Testing webhook messages
First confirm that your web app finished restarting by checking the log stream:- Python
- REST
- Output
The initial test signature won’t pass validation. For production testing, we can trigger actual events from Azure OpenAI with valid signatures.
background=True set.
Security best practices
Securing your webhook endpoint is critical. Follow these recommendations:Signature verification
Always verify the webhook signature to ensure requests are from Azure OpenAI:Idempotency
Use theWebhook-ID header to prevent duplicate processing:
Timeout handling
Respond quickly to avoid webhook timeouts. Offload heavy processing to background threads:Additional recommendations
- Store your signing secret securely; it’s only shown once during creation.
- Use HTTPS for all webhook endpoints.
- Protect and rotate Azure access tokens regularly.
- Validate incoming requests using the signing secret.
Event processing examples
Here are some common patterns for processing webhook events:Call logging
Notification system
Managing webhook endpoints
List webhook endpoints
Update webhook endpoint
Update webhook properties such as name, URL, and registered event types. The signing secret can’t be updated through this operation.
Delete webhook endpoint
Remove a webhook endpoint using its webhook ID:Common issues and troubleshooting
Handling webhook requests on a server
When an event happens that you’re subscribed to, you’ll receive a POST request with the following structure:Webhook-ID: Unique identifier for idempotency - use this to prevent duplicate processingWebhook-Timestamp: Unix timestamp of the delivery attemptWebhook-Signature: Cryptographic signature to verify authenticity from OpenAI
object: Always “event” for webhook eventsid: Unique event identifiertype: Event type (for example, “realtime.call.incoming”)created_at: Unix timestamp when the event was createddata: Event-specific data containing call information and SIP headers
Webhook events reference
The following event types are available for webhook registration:Example payload
Clean up resources
When you no longer need the webhook server, you can delete the Azure Web App and its associated resources.Delete the web app only
To delete just the web app while keeping the resource group and other resources:Additional suggested configurations
- Implement proper logging and monitoring for webhook events.
- Add database storage for call records.
- Set up alerting for failed webhook deliveries.
- Implement retry logic for downstream service calls.
- Add authentication for your webhook endpoint if needed.