Skip to main content
Azure OpenAI webhooks enable your applications to receive real-time notifications about API events, such as batch completions or incoming calls. By subscribing to webhook events, you can automate workflows, trigger alerts, and integrate with other systems seamlessly. This guide walks you through setting up a webhook server, securing your endpoints, deploying, and troubleshooting common issues.

Prerequisites

  • Azure CLI installed and signed in. For example: az --version and az 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 as AZURE_OPENAI_API_KEY.
Install the required Python packages:

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 called app.py with the following Flask application that receives and processes webhook events:

Create a requirements.txt file

Create a requirements.txt file in the same directory as your app.py file:

Create an Azure web app

Deploy your webhook server using az webapp up. The command must be run from the folder where the app.py code for your application is located.
This command will:
  • Create a resource group if it doesn’t exist.
  • Create an App Service plan.
  • Create a web app.
  • Deploy your code.
Your webhook URL will be: 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:
After setting the environment variable, restart your web app:

Testing webhook messages

First confirm that your web app finished restarting by checking the log stream:
Once the restart is complete, you can test your webhook endpoint by sending sample REST API events.
The initial test signature won’t pass validation. For production testing, we can trigger actual events from Azure OpenAI with valid signatures.
Now launch the log stream for your webhook listener webapp if it isn’t still running:
To test again with a valid signature make a call with the responses API with background=True set.
Python output:
Log stream output:

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 the Webhook-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:
Header
  • Webhook-ID: Unique identifier for idempotency - use this to prevent duplicate processing
  • Webhook-Timestamp: Unix timestamp of the delivery attempt
  • Webhook-Signature: Cryptographic signature to verify authenticity from OpenAI
Payload
  • object: Always “event” for webhook events
  • id: Unique event identifier
  • type: Event type (for example, “realtime.call.incoming”)
  • created_at: Unix timestamp when the event was created
  • data: 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.