Deliver events to webhooks using namespace topics - Azure portal (preview)
The article provides step-by-step instructions to publish events to Azure Event Grid in the CloudEvents JSON format and deliver those events by using the push delivery model. To be specific, you publish events to a namespace topic in Event Grid and push those events from an event subscription to a webhook handler destination. For more information about the push delivery model, see Push delivery overview.
Note
Azure Event Grid namespaces currently supports Shared Access Signatures (SAS) token and access keys authentication.
If you don't have an Azure subscription, create an Azure free account before you begin.
Create an Event Grid namespace
An Event Grid namespace provides a user-defined endpoint to which you post your events. The following example creates a namespace in your resource group using Bash in Azure Cloud Shell. The namespace name must be unique because it's part of a Domain Name System (DNS) entry.
Navigate to the Azure portal.
In the search bar at the topic, type
Event Grid Namespaces
, and selectEvent Grid Namespaces
from the results.On the Event Grid Namespaces page, select + Create on the command bar.
On the Create Namespace page, follow these steps:
- Select the Azure subscription in which you want to create the namespace.
- Create a new resource group by selecting Create new or select an existing resource group.
- Enter a name for the namespace.
- Select the location where you want to create the resource group.
- Then, select Review + create.
- On the Review + create page, select Create.
On the Deployment page, select Go to resource after the successful deployment.
Get the access key
- On the Event Grid Namespace page, select Access keys on the left menu.
- Select copy button next to the access key.
- Save the access key somewhere. You use it later in this quickstart.
Create a topic in the namespace
Create a topic that holds all events published to the namespace endpoint.
- Select Topics on the left menu.
- On the Topics page, select + Topic on the command bar.
- On the Create Topic page, follow these steps:
Create a message endpoint
Before subscribing to the events, let's create the endpoint for the event message. Typically, the endpoint takes actions based on the event data. To simplify this quickstart, you deploy a prebuilt web app that displays the event messages. The deployed solution includes an App Service plan, an App Service web app, and source code from GitHub.
Select Deploy to Azure to deploy the solution to your subscription.
On the Custom deployment page, do the following steps:
For Resource group, select the resource group that you created when creating the storage account. It will be easier for you to clean up after you're done with the tutorial by deleting the resource group.
For Site Name, enter a name for the web app.
For Hosting plan name, enter a name for the App Service plan to use for hosting the web app.
Select Review + create.
On the Review + create page, select Create.
The deployment takes a few minutes to complete. On the Deployment page, select Go to resource group.
On the Resource group page, in the list of resources, select the web app that you created. You also see the App Service plan and the storage account in this list.
On the App Service page for your web app, select the URL to navigate to the web site. The URL should be in this format:
https://<your-site-name>.azurewebsites.net
.Confirm that you see the site but no events are posted to it yet.
Important
Keep the Azure Event Grid Viewer window open so that you can see events as they are posted.
Create an event subscription
Create an event subscription setting its delivery mode to Push, which supports push delivery.
- Switch to the tab or window with the Event Grid Namespace page open from the tab or window with the Event Hubs Namespace page open.
- On the Event Grid Namespace page, select Topics on the left menu.
- On the Topics page, select the topic you created in the previous step.
- Select + Subscription on the command bar.
- On the Create Event Subscription page, follow these steps:
In the Basic tab, enter a name for the event subscription.
Select Push for the event delivery mode.
For Endpoint type, select Web Hook.
Select Configure an endpoint.
On the Web Hook page, specify the endpoint (for example:
https://spegridsite0520.azurewebsites.net/api/updates
) as shown in the following example, and select Confirm selection.Back on the Create Subscription page, select Create.
Send events to your topic
Now, send a sample event to the namespace topic by following steps in this section.
Declare variables
Launch Cloud Shell in the Azure portal. Switch to Bash.
Run the following command to declare a variable to hold the resource group name. Replace
RESOUREGROUPNAME
with the name of your Azure resource group.resource_group=RESOURCEGROUPNAME
In the Cloud Shell, run the following command to declare a variable to hold the namespace name. Replace
NAMESPACENAME
with the name of your Event Grid namespace.namespace=NAMESPACENAME
Run the following command to declare a variable to hold the access key value you noted down earlier. Replace
ACCESSKEY
with the value of access key to your Event Grid namespace.key=ACCESSKEY
In the Cloud Shell, run the following command to declare a variable to hold the namespace name.
topic=TOPICNAME
Publish an event
Retrieve the namespace hostname. You use it to compose the namespace HTTP endpoint to which events are sent. The following operations were first available with API version
2023-06-01-preview
. You can also get the hostname from the Overview page of your Event Grid namespace in the Azure portal.publish_operation_uri="https://"$(az eventgrid namespace show -g $resource_group -n $namespace --query "topicsConfiguration.hostname" --output tsv)"/topics/"$topic:publish?api-version=2023-06-01-preview
Create a sample CloudEvents compliant event:
event=' { "specversion": "1.0", "id": "'"$RANDOM"'", "type": "com.yourcompany.order.ordercreatedV2", "source" : "/mycontext", "subject": "orders/O-234595", "time": "'`date +%Y-%m-%dT%H:%M:%SZ`'", "datacontenttype" : "application/json", "data":{ "orderId": "O-234595", "url": "https://yourcompany.com/orders/o-234595"}} '
The
data
element is the payload of your event. Any well-formed JSON can go in this field. For more information on properties (also known as context attributes) that can go in an event, see the CloudEvents specifications.Use CURL to send the event to the topic. CURL is a utility that sends HTTP requests.
curl -X POST -H "Content-Type: application/cloudevents+json" -H "Authorization:SharedAccessKey $key" -d "$event" $publish_operation_uri
Verify that Azure Event Grid Viewer received the event
Verify that the Azure Event Grid Viewer web app shows the events it received from Event Grid.
Related content
In this quickstart, you used a webhook as an event handler. For quickstart that uses an Azure event hub as an event handler, see Deliver events to Azure Event Hubs using namespace topics.