Send events from API Management to Event Grid (Preview)

API Management integrates with Azure Event Grid so that you can send event notifications to other services and trigger downstream processes. Event Grid is a fully managed event routing service that uses a publish-subscribe model. Event Grid has built-in support for Azure services like Azure Functions and Azure Logic Apps, and can deliver event alerts to non-Azure services using webhooks.

For example, using integration with Event Grid, you can build an application that updates a database, creates a billing account, and sends an email notification each time a user is added to your API Management instance.

In this article, you subscribe to Event Grid events in your API Management instance, trigger events, and send the events to an endpoint that processes the data. To keep it simple, you send events to a sample web app that collects and displays the messages:

API Management events in Event Grid viewer

Prerequisites

  • Use the Bash environment in Azure Cloud Shell.

    Launch Cloud Shell in a new window

  • If you prefer, install the Azure CLI to run CLI reference commands.

    • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For additional sign-in options, see Sign in with the Azure CLI.

    • When you're prompted, install Azure CLI extensions on first use. For more information about extensions, see Use extensions with the Azure CLI.

    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

Create an event endpoint

In this section, you use a Resource Manager template to deploy a pre-built sample web application to Azure App Service. Later, you subscribe to your API Management instance's Event Grid events and specify this app as the endpoint to which the events are sent.

To deploy the sample app, you can use the Azure CLI, Azure PowerShell, or the Azure portal. The following example uses the az deployment group create command in the Azure CLI.

  • Set RESOURCE_GROUP_NAME to the name of an existing resource group

  • Set SITE_NAME to a unique name for your web app

    The site name must be unique within Azure because it forms part of the fully qualified domain name (FQDN) of the web app. In a later section, you navigate to the app's FQDN in a web browser to view the events.

RESOURCE_GROUP_NAME=<your-resource-group-name>
SITE_NAME=<your-site-name>

az deployment group create \
    --resource-group $RESOURCE_GROUP_NAME \
    --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
    --parameters siteName=$SITE_NAME hostingPlanName=$SITE_NAME-plan

Once the deployment has succeeded (it might take a few minutes), open a browser and navigate to your web app to make sure it's running:

https://<your-site-name>.azurewebsites.net

You should see the sample app rendered with no event messages displayed.

Enable Event Grid resource provider

If you haven't previously used Event Grid in your Azure subscription, you may need to register the Event Grid resource provider.

In the Azure portal:

  1. Select Subscriptions on the left menu.
  2. Select the subscription you're using for Event Grid.
  3. On the left menu, under Settings, select Resource providers.
  4. Find Microsoft.EventGrid.
  5. If not registered, select Register.

It may take a moment for the registration to finish. Select Refresh to update the status. When Status is Registered, you're ready to continue.

Subscribe to API Management events

In Event Grid, you subscribe to a topic to tell it which events you want to track, and where to send them. Here, you create a subscription to events in your API Management instance.

  1. In the Azure portal, navigate to your API Management instance.

  2. Select Events (preview) > + Event Subscription.

  3. On the Basic tab:

    • Enter a descriptive Name for the event subscription.
    • In Event Types, select one or more API Management event types to send to Event Grid. For the example in this article, select at least Microsoft.APIManagement.ProductCreated
    • In Endpoint Details, select the Web Hook event type, click Select an endpoint, and enter your web app URL followed by api/updates. Example: https://myapp.azurewebsites.net/api/updates.
    • Select Confirm selection.
  4. Leave the settings on the remaining tabs at their default values, and then select Create.

    Create an event subscription in Azure portal

Trigger and view events

Now that the sample app is up and running and you've subscribed to your API Management instance with Event Grid, you're ready to generate events.

As an example, create a product in your API Management instance. If your event subscription includes the Microsoft.APIManagement.ProductCreated event, creating the product triggers an event that is pushed to your web app endpoint.

Navigate to your Event Grid Viewer web app, and you should see the ProductCreated event. Select the button next to the event to show the details.

Product created event in Event Grid viewer

Event Grid event schema

API Management event data includes the resourceUri, which identifies the API Management resource that triggered the event. For details about the API Management event message schema, see the Event Grid documentation:

Azure Event Grid event schema for API Management

Next steps