Use a webhook as a trigger for Azure Logic Apps and Power Automate

Webhooks are simple HTTP callbacks that provide event notifications when something happens in a web service. In Azure Logic Apps and Power Automate you can use webhooks as triggers. A logic app or flow listens for this trigger and performs an action whenever the trigger fires. This tutorial demonstrates how to use a webhook as a trigger via a custom connector defined with an OpenAPI specification.

Note

This article uses GitHub as an example of a service that can send notifications via webhooks, but you can extend the techniques demonstrated here to any service that allows webhooks.

Prerequisites

Enable authentication in GitHub

A web service API that sends a webhook request to Logic Apps or Power Automate typically uses some form of authentication, and GitHub is no exception. GitHub supports several types of authentication. This tutorial uses GitHub fine-grained personal access tokens.

  1. Go to GitHub and sign in if you haven't already.

  2. In the upper right, select your profile picture, and then, in the menu, select Settings.

  3. In the menu on the left, select Developer settings.

  4. Under Personal access tokens, select Fine-grained tokens.

  5. Select the Generate new token button, then confirm your password if requested.

  6. Enter a Token name and Description for the token.

  7. Under Expiration, select an expiration date for the token.

  8. Under Repository access, select Only select repositories, and select the repository you want to give access to.

  9. Under Permissions, select Add permissions > Webhooks > Read and write.

  10. Select the Generate token button.

  11. Make note of your new token. You need to use this token later when adding your webhook connector as a trigger.

    Important

    You can't access this token again. Copy and paste it somewhere to use later in the tutorial.

Define the webhook in the OpenAPI definition

You implement webhooks in Logic Apps and Power Automate as part of a custom connector. To create the connector, you need to provide an OpenAPI definition that defines the shape of the webhook. This tutorial uses a downloaded sample OpenAPI definition for GitHub webhooks.

If you want to create a trigger but you don't have an OpenAPI definition to work from, use the triggers UI in the custom connector wizard to define webhook triggers.

The sample OpenAPI definition contains three parts that are critical to making the webhook work:

  • Creating the webhook
  • Defining the incoming hook request from the web service API (in this tutorial example, GitHub)
  • Deleting the webhook

The connector uses this OpenAPI definition to understand how to create, receive from, and delete webhooks for the GitHub repository.

Create the webhook

The connector uses the web service API (In our example, the GitHub REST API) to create the webhook on the web service side by sending an HTTP POST request to the appropriate webhook creation endpoint for the web service (/repos/{owner}/{repo}/hooks in the case of GitHub).

When you create a new logic app or flow using the connector, the connector sends a POST request to the web service's create webhook endpoint as defined in the connector's OpenAPI definition. It also sends a POST request to this URL if you modify the logic app or flow trigger. In the following sample OpenAPI path configuration, the post property contains the schema of the create webhook request to send to the GitHub REST API.

"/repos/{owner}/{repo}/hooks": {
    "x-ms-notification-content": {
        "description": "Details for Webhook",
        "schema": {
            "$ref": "#/definitions/WebhookPushResponse"
        }
    },
    "post": {
        "description": "Creates a GitHub webhook",
        "summary": "Triggers when a PUSH event occurs",
        "operationId": "webhook-trigger",
        "x-ms-trigger": "single",
        "parameters": [
            {
            "name": "owner",
            "in": "path",
            "description": "Name of the owner of targeted repository",
            "required": true,
            "type": "string"
            },
            {
            "name": "repo",
            "in": "path",
            "description": "Name of the repository",
            "required": true,
            "type": "string"
            },
            {
            "name": "Request body of webhook",
            "in": "body",
            "description": "This is the request body of the Webhook",
            "schema": {
                "$ref": "#/definitions/WebhookRequestBody"
            }
            }
        ],
        "responses": {
            "201": {
            "description": "Created",
            "schema": {
                "$ref": "#/definitions/WebhookCreationResponse"
                }
            }
        }
    }
},

Important

The "x-ms-trigger": "single" property is a schema extension that tells Logic Apps and Power Automate to display this webhook in the list of available triggers in the designer. Be sure to include it.

Define the incoming hook request from the API

Define the shape of the incoming hook request (the notification from GitHub to Logic Apps or Power Automate) in the custom x-ms-notification-content property, as shown in the previous sample. The request doesn't need to contain the entire contents of the request, just the portions you want to use in your logic app or flow.

Delete the webhook

Include a definition for how to delete the webhook in the OpenAPI definition. Logic Apps and Power Automate try to delete the existing webhook if you update the trigger, or if you delete the logic app or flow.

"/repos/{owner}/{repo}/hooks/{hook_Id}": {
    "delete": {
        "description": "Deletes a Github webhook",
        "operationId": "DeleteTrigger",
        "parameters": [
            {
                "name": "owner",
                "in": "path",
                "description": "Name of the owner of targeted repository",
                "required": true,
                "type": "string"
            },
            {
                "name": "repo",
                "in": "path",
                "description": "Name of the repository",
                "required": true,
                "type": "string"
            },
            {
                "name": "hook_Id",
                "in": "path",
                "description": "ID of the webhook being deleted",
                "required": true,
                "type": "string"
            }
        ]
    }
},

No header is included for the delete webhook call. The delete webhook call uses the same connection as the connector.

Important

To enable Logic Apps or Power Automate to delete a webhook, the web service's API must include a Location HTTP header in the 201 response when the webhook is created. The Location header should contain the path to the webhook that's used with the HTTP DELETE method. For example, the Location header included in GitHub's response follows this format: https://api.github.com/repos/<user name>/<repo name>/hooks/<hook ID>.

Import the OpenAPI definition

Start by importing the OpenAPI definition for Logic Apps, or for Power Automate.

Import the OpenAPI definition for Logic Apps

  1. Go to the Azure portal and open the Logic Apps connector you created earlier in Create an Azure Logic Apps custom connector.

  2. In your connector's menu, select Logic Apps Connector, and then select Edit.

    Screenshot of the Edit option for the Logic Apps connector.

  3. Under General, select Upload an OpenAPI file, and then go to the sample OpenAPI file that you downloaded.

    Screenshot of the Upload an OpenAPI file option.

Import the OpenAPI definition for Power Automate

  1. Go to https://make.powerautomate.com/.

  2. In the upper right corner, select the gear icon, then select Custom connectors.

    Screenshot of the gear icon menu with the Custom connectors option.

  3. Select Create custom connector, then select Import a Postman collection.

    Screenshot of the Create custom connector menu with the import options.

  4. Enter a name for the custom connector, go to the sample OpenAPI file that you downloaded, and select Connect.

    Screenshot of the field for entering a custom connector name.

    Parameter Value
    Custom connector title "GitHubDemo"

Finish creating the custom connector

  1. On the General page, select Continue.

  2. On the Security page, under Authentication type, select Basic authentication.

  3. In the Basic authentication section, for the label fields, enter the text User name and Password. These labels appear when you use the trigger in a logic app or flow.

    Screenshot of the Basic authentication label fields.

  4. At the top of the wizard, ensure the name is set to "GitHubDemo", and then choose Create connector.

You're now ready to use the trigger in a logic app or flow, or you can read on about how to create triggers from the UI.

Create webhook triggers from the UI

In this section, you learn how to create a trigger in the UI without having any trigger definitions in your OpenAPI definition. Start with a baseline OpenAPI definition, or start from scratch in the custom connector wizard.

  1. On the General page, make sure you specify a description and URL.

    Parameter Value
    Description "GitHub is a social source code repository."
    URL "api.github.com"
  2. On the Security page, configure basic authentication like you did in the previous section.

  3. On the Definition page, choose New trigger, and fill out the description for your trigger. In this example, you are creating a trigger that fires when a pull request is made to a repository.

    Screenshot of the new trigger general information fields.

    Parameter Value
    Summary "Triggers when a pull request is made to a selected repository"
    Description "Triggers when a pull request is made to a selected repository"
    Operation ID "webhook-PR-trigger"
    Visibility "none" (see below for more information)
    Trigger type "Webhook"

    The Visibility property for operations and parameters in a logic app or flow has the following options:

    • none: displayed normally in the logic app or flow
    • advanced: hidden under an additional menu
    • internal: hidden from the user
    • important: always shown to the user first
  4. The Request area displays information based on the HTTP request for the action. Choose Import from sample.

    Screenshot of the Definition page with the Import from sample option.

  5. Define the request for the webhook trigger, and then select Import. We provide a sample for you to import (in the following section). For more information, see the GitHub API reference. Logic Apps and Power Automate automatically add standard content-type and security headers, so you don't need to define those while importing from a sample.

    Screenshot of the request import dialog for the webhook trigger.

    Parameter Value
    Verb "POST"
    URL "https://api.github.com/repos/{owner}/{repo}/hooks"
    Body See below
    {
      "name": "web",
      "active": true,
      "events": [
        "pull_request"
      ],
      "config": {
        "url": "http://example.com/webhook"
      }
    }
    
  6. The Response area displays information based on the HTTP response for the action. Select Add default response.

    Screenshot of the Definition page Response area with the Add default response option.

  7. Define the response for the webhook trigger, and then select Import. Again, we provide a sample for you to import. For more information, see the GitHub API reference.

    Screenshot of the response import dialog for the webhook trigger.

    {
      "action": "opened",
      "number": 1,
      "pull_request": {
        "html_url": "https://github.com/baxterthehacker/public-repo/pull/1",
        "state": "open",
        "locked": false,
        "title": "Update the README with new information",
        "user": {
          "login": "baxterthehacker",
          "type": "User"
        }
      }
    }
    
  8. In the Trigger configuration area, select the parameter that should receive the callback URL value from GitHub. This parameter is the url property in the config object.

    Screenshot of the Trigger configuration area with the callback URL parameter selected.

  9. At the top of the wizard, enter a name, and then choose Create connector.

Use the webhook as a trigger

After you configure everything, use the webhook through the custom connector in a logic app or flow. Next, create a flow that sends an email whenever your GitHub repo receives a git push.

  1. In https://make.powerautomate.com/, at the top of the page, select My flows.

  2. Select Create from blank.

    Screenshot of the Search hundreds of connectors and triggers option.

  3. In the designer for Power Automate, search for the custom connector you registered earlier.

    Screenshot of searching for the custom connector trigger in the Power Automate designer.

  4. Select the item in the list to use it as a trigger.

  5. Since this is the first time you used this custom connector, connect to it. Enter connection information, and then select Create.

    Screenshot of the new connection information fields.

    Parameter Value
    Connection name A descriptive name
    User name Your GitHub username
    Password The personal access token you created earlier
  6. Enter details about the repo you want to monitor. You might recognize the fields from the WebhookRequestBody object in the OpenAPI file.

    Screenshot of the repository owner and name fields for the trigger.

    Parameter Value
    owner The owner of the repo to monitor
    repo The repo to monitor

    Important

    Use a repo that your account has rights to. The easiest way to do this is to use your own repo.

  7. Select New step > Add an action.

  8. Search for and select the Send an email (V2) action.

  9. Enter text in the Body field and the other fields, using values from the dynamic content dialog box. Values come from the WebhookPushResponse object in the OpenAPI file.

  10. At the top of the page, give the flow a name and choose Create flow.

    Screenshot of the flow name field and Create flow button.

Verification and troubleshooting

To verify everything is set up correctly, select My flows, and then select the information icon next to the new flow to view the run history:

  • You should already see at least one Succeeded run from the webhook creation. This run indicates that the webhook was created successfully on the GitHub side.
  • If the run failed, review the run details to see why it failed. If the failure was due to a 404 Not Found response, your GitHub account likely doesn't have the correct permissions to create a webhook on the repo you used.

Summary

If you configured everything correctly, you receive push notifications in the Power Automate mobile app whenever a git push occurs on the GitHub repository you selected. By using the preceding process, you can use any webhook-capable service as a trigger in your flows.

Next steps

Provide feedback

We greatly appreciate feedback on issues with our connector platform, or new feature ideas. To provide feedback, go to Submit issues or get help with connectors and select your feedback type.