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.

  1. Navigate to the Azure portal.

  2. In the search bar at the topic, type Event Grid Namespaces, and select Event Grid Namespaces from the results.

    Screenshot that shows the search bar in the Azure portal.

  3. On the Event Grid Namespaces page, select + Create on the command bar. Screenshot that shows the Event Grid Namespaces page with the Create button on the command bar selected.

  4. On the Create Namespace page, follow these steps:

    1. Select the Azure subscription in which you want to create the namespace.
    2. Create a new resource group by selecting Create new or select an existing resource group.
    3. Enter a name for the namespace.
    4. Select the location where you want to create the resource group.
    5. Then, select Review + create.
      Screenshot that shows the Create Namespace page.
    6. On the Review + create page, select Create.
  5. On the Deployment page, select Go to resource after the successful deployment.

Get the access key

  1. On the Event Grid Namespace page, select Access keys on the left menu.
  2. Select copy button next to the access key. Screenshot that shows the Event Grid Namespaces page with the Access keys tab selected.
  3. 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.

  1. Select Topics on the left menu.
  2. On the Topics page, select + Topic on the command bar. Screenshot that shows the Topics page.
  3. On the Create Topic page, follow these steps:
    1. Enter a name for the topic.
    2. Select Create.
      Screenshot that shows the Create Topic page.

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.

  1. Select Deploy to Azure to deploy the solution to your subscription.

    Button to deploy the Resource Manager template to Azure.

  2. On the Custom deployment page, do the following steps:

    1. 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.

    2. For Site Name, enter a name for the web app.

    3. For Hosting plan name, enter a name for the App Service plan to use for hosting the web app.

    4. Select Review + create.

      Screenshot showing the Custom deployment page.

  3. On the Review + create page, select Create.

  4. The deployment takes a few minutes to complete. On the Deployment page, select Go to resource group.

    Screenshot showing the deployment succeeded page with a link to go to the resource group.

  5. 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.

    Screenshot that shows the selection of web app in the resource group.

  6. 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.

    Screenshot that shows the selection of link to navigate to web app.

  7. Confirm that you see the site but no events are posted to it yet.

    Screenshot of a new site.

    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.

  1. 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.
  2. On the Event Grid Namespace page, select Topics on the left menu.
  3. On the Topics page, select the topic you created in the previous step.
  4. Select + Subscription on the command bar. Screenshot that shows the Topic page with Create subscription button selected.
  5. On the Create Event Subscription page, follow these steps:
    1. In the Basic tab, enter a name for the event subscription.

    2. Select Push for the event delivery mode.

    3. For Endpoint type, select Web Hook.

    4. Select Configure an endpoint.

      Screenshot that shows the Create Subscription page with Push selected for Delivery mode and WebHook as endpoint type.

    5. 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.

      Screenshot that shows the Web Hook page with a value for subscriber endpoint.

    6. Back on the Create Subscription page, select Create.

      Screenshot that shows the Create Subscription page with all fields filled.

Send events to your topic

Now, send a sample event to the namespace topic by following steps in this section.

Declare variables

  1. Launch Cloud Shell in the Azure portal. Switch to Bash.

    Screenshot that shows the Cloud Shell.

  2. 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
    
  3. 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
    
  4. 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
    
  5. In the Cloud Shell, run the following command to declare a variable to hold the namespace name.

    topic=TOPICNAME
    

Publish an event

  1. 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
    
  2. 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.

  3. 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.

Screenshot that shows the Azure Event Grid Viewer with a sample received event.

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.