title | description | ms.service | ms.topic | ms.date | ms.author | author | ms.custom |
---|---|---|---|---|---|---|---|
Workflow automation after API registration - Azure API Center | Learn how to set up a notification workflow to set API status in your organization's API center using Azure Logic Apps and Microsoft Teams. | azure-api-center | how-to | 10/18/2024 | danlep | dlepow |
This article shows how to set up an automated notification workflow for updating the status of an API after it's registered in your organization's API center. Adapt this example to automate workflows for other types of events in your API center.
Setting up a notification workflow can be useful for several reasons:
- Real-time updates - Receive alerts immediately when certain events occur, such as API registration or API definition updates. Quickly address issues or take further actions based on these events.
- Automation - Save time and reduce manual monitoring. For example, set up alerts for when a new API is registered, an API definition changes, or API analysis reports are generated.
- Improved user experience - By integrating notifications, keep users informed about the status of their requests or actions. This can include approval processes for your APIs, changing custom metadata based on criteria.
- Collaboration - Send notifications to different team members based on their roles (for example, API administrator, API developer), ensuring that the right people are informed and can take appropriate actions.
In this simplified example:
- Registering an API in your API center triggers an event that runs an Azure Logic Apps workflow.
- The workflow sends a notification in Microsoft Teams to a designated individual.
- The individual decides the status of the API registration directly from the notification in Microsoft Teams.
- The workflow updates the API status metadata in the API registration based on the individual's decision. API status is a custom metadata property which you set up in your API center.
You can adapt this example to meet your organization's notification and governance requirements for your API center. You can also trigger a similar automated cloud workflow in Power Automate.
- An API center in your Azure subscription. If you haven't created one already, see Quickstart: Create your API center.
- A logic app in your Azure subscription. For demonstration purposes, you can use a Consumption logic app. For steps to create one, see Quickstart: Create an example Consumption logic app workflow using the Azure portal.
- Permissions to assign RBAC roles in your API center.
- The Event Grid resource provider registered in your subscription. If you need to register the Event Grid resource provider, see Subscribe to events published by a partner with Azure Event Grid.
The example workflow in this article sets the value of an example custom metadata property in your API center for the status of an API registration.
To create a custom api-status property in your API center:
- In the Azure portal, navigate to your API center.
- In the left menu, under Assets, select Metadata > + New metadata.
- In the Details tab, enter the following details:
- In Title, enter api-status.
- In Type, select Predefined choices.
- Add the following predefined choices: new, pending, approved. Select Next.
- In the Assignments tab, next to APIs, select Optional.
- Optionally make assignments to Deployments and Environments. Select Next.
- Review the configuration and select Create.
For this scenario, the logic app uses a managed identity to access the Azure API center. Depending on your needs, enable either a system-assigned or user-assigned managed identity. For configuration steps, see Authenticate access and connections to Azure resources with managed identities in Azure Logic Apps.
:::image type="content" source="media/set-up-notification-workflow/managed-identity-logic-app.png" alt-text="Screenshot of configuring managed identity in the portal.":::
Assign the logic app managed identity the necessary permissions to access the API center. For this scenario, assign the Contributor role to the managed identity.
- In the Azure portal, navigate to your API center and select Access control (IAM).
- Select + Add > Add role assignment.
- Select Privileged administrator roles and then select Contributor. Select Next.
- On the Members page, in Assign access to, select Managed identity > + Select members.
- On the Select managed identities page, search for and select the managed identity of the logic app. Click Select and then Next.
- Review the role assignment, and select Review + assign.
This section provides the manual steps to configure an event subscription that triggers a logic app workflow when an API is registered in your API center.
Configure a workflow step to trigger the logic app workflow when an event occurs in the API center.
In the portal, navigate to your logic app.
In the left menu, under Development tools, select Logic app designer.
Select Add a trigger.
Search for Azure Event Grid, and select the When a resource event occurs trigger.
In the When a resource event occurs pane:
- In Resource type, select Microsoft.ApiCenter.Services.
- In Subscription, select your subscription.
- In Resource Name, enter the full resource name of your API center, in the following form:
/subscriptions/<subscription ID>/resourceGroups/<resource group nam>/providers/Microsoft.ApiCenter/services/<API Center name>
. - In Event Type Item - 1, enter or select Microsoft.ApiCenter.ApiAdded.
:::image type="content" source="media/set-up-notification-workflow/when-resource-event-occurs.png" alt-text="Screenshot of When a resource event occurs action in the portal.":::
Add a workflow step to initialize a variable that stores the ID of the API that's registered.
- Select Add an action.
- In the search box, enter Variables.
- Under Variables, select Initialize variable.
- In the Initialize variable pane:
- In Name, enter subjectvar.
- In Type, select String.
- In Value, enter
/
and select Insert dynamic content. - Under When a resource event occurs, select Subject.
:::image type="content" source="media/set-up-notification-workflow/initialize-subjectvar-variable.png" alt-text="Screenshot of initializing subjectvar variable in the portal.":::
Add a workflow step to initialize a variable to store the version of the API Center management API. This version is needed for the HTTP requests in the workflow.
Tip
Initializing a variable for the version makes it easy to change the value later, as the management API gets updated.
- Select Add an action.
- In the search box, enter Variables.
- Under Variables, select Initialize variable.
- In the Initialize variable pane:
- In Name, enter versionvar.
- In Type, select String.
- In Value, enter
?api-version=2024-03-01
.
:::image type="content" source="media/set-up-notification-workflow/initialize-versionvar-variable.png" alt-text="Screenshot of initializing versionvar variable in the portal.":::
Add a workflow step to make an HTTP GET request to get API details from the API center.
- Select Add an action.
- In the search box, enter HTTP.
- Under HTTP, select HTTP.
- In the HTTP pane:
- In URI, enter
https://management.azure.com/
(including the trailing forward slash). After the forward slash, enter/
, select Insert dynamic content, and then select the variables subjectvar and versionvar, in that order. - In Method, select GET.
- Under Advanced parameters, select Authentication.
- In Authentication type, select Managed Identity.
- In Managed identity, select System-assigned managed identity.
- In Audience, enter
https://management.azure.com/
.
- In URI, enter
:::image type="content" source="media/set-up-notification-workflow/http-request-get.png" alt-text="Screenshot of HTTP GET request action in the portal.":::
Add a workflow step to parse the JSON output of the preceding HTTP request.
- Select Add an action.
- In the search box, enter Parse JSON.
- Under Data operations, select Parse JSON.
- In the Parse JSON pane:
- In Content, enter
/
and select Insert dynamic content. - Under HTTP, select Body.
- In Schema, enter the following:
{ "type": "object", "properties": { "type": { "type": "string" }, "properties": { "type": "object", "properties": { "title": { "type": "string" }, "kind": { "type": "string" }, "description": { "type": "string" }, "lifecycleStage": { "type": "string" }, "externalDocumentation": { "type": "array" }, "contacts": { "type": "array" }, "customProperties": { "type": "object", "properties": {} } } }, "id": { "type": "string" }, "name": { "type": "string" }, "systemData": { "type": "object", "properties": { "createdAt": { "type": "string" }, "lastModifiedAt": { "type": "string" } } } } }
- In Content, enter
:::image type="content" source="media/set-up-notification-workflow/parse-json.png" alt-text="Screenshot of parse JSON action in the portal.":::
Add a workflow step to post the notification as an adaptive card in Microsoft Teams.
Select Add an action.
In the search box, enter Teams.
Under Microsoft Teams, select Post adaptive card and wait for a response. If prompted, sign in to your Microsoft Teams account.
In the Post adaptive card and wait for a response pane:
- In Post as, select Flow bot.
- In Post in, select an appropriate option for your Teams setup. For testing, you can select Chat with Flow bot.
- In Message, enter the following text for an adaptive card. Modify the text as needed.
{ "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", "version": "1.4", "body": [ { "type": "TextBlock", "text": "Hi API Admin,", "weight": "Bolder", "size": "Medium" }, { "type": "TextBlock", "text": "A new API has been registered.", "wrap": true }, { "type": "TextBlock", "text": "API Title: **{{apiTitle}}**", "wrap": true }, { "type": "TextBlock", "text": "Please provide the status for this API:", "wrap": true }, { "type": "Input.ChoiceSet", "id": "apiStatus", "style": "expanded", "choices": [ { "title": "New", "value": "new" }, { "title": "Pending", "value": "pending" }, { "title": "Approved", "value": "approved" } ], "isRequired": true, "errorMessage": "Please select a status." } ], "actions": [ { "type": "Action.Submit", "title": "Submit" } ] }
Select the text
{{apiTitle}}
in the message and delete it. Enter/
and select Insert dynamic content. Under Parse JSON, select Body title to replace the selected text with the dynamic content.In Recipient, enter the email address of the individual who receives notifications.
:::image type="content" source="media/set-up-notification-workflow/post-adaptive-card.png" alt-text="Screenshot of post adaptive card to Teams action in the portal.":::
Add a workflow step to initialize the value of a variable that stores the API status value returned from the Teams adaptive card.
- Select Add an action.
- In the search box, enter Variables.
- Under Variables, select Initialize variable.
- In the Initialize variable pane:
- In Name, enter statusvar.
- In Type, select String.
- In Value, enter
@body('Post_adaptive_card_and_wait_for_a_response')?['data']?['apiStatus']
.
:::image type="content" source="media/set-up-notification-workflow/initialize-statusvar-variable.png" alt-text="Screenshot of initializing statusvar variable in the portal.":::
Add a workflow step to make an HTTP PUT request to update the API properties in your API center.
- Select Add an action.
- In the search box, enter HTTP.
- Under HTTP, select HTTP.
- In the HTTP pane:
- In URI, enter
https://management.azure.com/
(including the trailing forward slash). After the forward slash, enter/
, select Insert dynamic content, and then select the variables subjectvar and versionvar, in that order. - In Method, select PUT.
- In Body, enter the following:
{ "properties": { "customProperties": { "api-status": "@variables('statusvar')" }, "title": "@body('Parse_JSON')?['properties']?['title']", "description": "@body('Parse_JSON')?['properties']?['description']", "lifecycleStage": "@body('Parse_JSON')?['properties']?['lifecycleStage']", "kind": "@body('Parse_JSON')?['properties']?['kind']" } }
- Under Advanced parameters, select Authentication.
- In Authentication type, select Managed Identity.
- In Managed identity, select System-assigned managed identity.
- In Audience, enter
https://management.azure.com/
.
- In URI, enter
:::image type="content" source="media/set-up-notification-workflow/http-request-put.png" alt-text="Screenshot of HTTP PUT request action in the portal.":::
Save the workflow in the Logic app designer. When the workflow is complete, it should look similar to the following image:
:::image type="content" source="media/set-up-notification-workflow/logic-app-designer-workflow.png" lightbox="media/set-up-notification-workflow/logic-app-designer-workflow-large.png" alt-text="Screenshot of complete workflow in Logic app designer in the portal.":::
Confirm that the event subscription is provisioned successfully in your API center. It might take a few minutes for the event subscription to be provisioned.
- In the Azure portal, navigate to your API center.
- In the left menu, select Events > Event Subscriptions.
- Check that the logic app is listed under Name, and the Endpoint is Webhook.
:::image type="content" source="media/set-up-notification-workflow/logic-app-event-subscription.png" alt-text="Screenshot of a logic app event subscription in the portal.":::
Test the event subscription by registering an API in your API center:
In the Azure portal, navigate to your API center.
Register an API in your API center. After the API is registered:
- The event subscription triggers the logic app workflow.
- The logic app workflow runs and sends a notification to the individual in Microsoft Teams.
In Microsoft Teams, view the adaptive card, make an API status selection, and select Submit. :::image type="content" source="media/set-up-notification-workflow/teams-adaptive-card.png" alt-text="Screenshot of adaptive card in Microsoft Teams.":::
The logic app workflow updates the api-status property in the API registration in your API center.
In your API center, view the API details to see the updated value for the custom api-status property.
:::image type="content" source="media/set-up-notification-workflow/view-api-custom-property.png" alt-text="Screenshot of updated API registration in the portal.":::
To get details about the logic app run and troubleshoot any issues:
- In the Azure portal, navigate to your logic app.
- In the left menu, under Development Tools, select Run History.
- Select the run to see the details of each step.