Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between API and Webhook from a programmer's perspective

I came across the term Webhook recently (in Azure Alerts, Github deployment, etc.). In my effort to understand the difference between API and Webhook, I read the explanations in stackexchange and sendgrid. My understanding is that the difference is in the way an API is invoked. But this sounds confusing. I think any API does following,

  1. may accept some inputs
  2. perform some action
  3. respond to the caller

If the above mentioned is true, then there is no need for APIs to be categorised based on how they are invoked. At least, this is true from coding perspective as the API is going to work anyway as long as it is invoked and gets the inputs (if required).

Just to explain my understanding of these terms with an example, Azure currently offers (in preview mode as of writing this question) creating Action Groups while setting up Alerts. Two of the alert types supported are Azure Function and Webhook. I could create a Http Trigger Azure function and use it in two Alerts where first one is of Azure Function type and the second one is of the Webhook type. To my surprise, the Webhook type Alert works even though I am using a Http Trigger Azure function (note that, Azure offers template for creating Webhook Azure function too). Below given is the Http Trigger Azure Function I wrote to setup the Webhook for Alert (actual link to code with instructions).

#r "Newtonsoft.Json"
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public async static Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, ICollector<string> outputQueueItem)
{
    log.Info("C# HTTP trigger function processed a request.");    

    string jsonContent = await req.Content.ReadAsStringAsync();
    JToken activityLog = JObject.Parse(jsonContent.ToString())
        .SelectToken("data.context.activityLog");

    //captures the details of the resource group being modified
    log.Info(string.Format("Resource group '{0}' was {1} on {2}.",
        (string)activityLog["resourceGroupName"],
        ((string)activityLog["subStatus"]).ToLower(), 
        (DateTime)activityLog["eventTimestamp"]));

    return req.CreateResponse(HttpStatusCode.OK);
}

At least in the Azure example, it looks like the Webhook Azure function does nothing special, otherwise the Webhook type Alert would not have worked with a Http Trigger Azure function. So, is there any difference in the way of coding or thought process when you code a REST API and Webhook using WebAPI?

like image 299
Martin Avatar asked Apr 30 '26 09:04

Martin


2 Answers

Functionally, there is no real difference, but they are different.

With an API, the author defines the spec (audience, protocol, verb, arguments, etc.) for the given endpoint, whereas with a Web Hook, this is specified by a third party.

It is important to maintain the differentiation, as it will help you or other developers to understand how the function is being consumed, and, in the case of a Web Hook, what parts of your solution will be impacted if its intended consumer changes its specification.

like image 180
Byron Jones Avatar answered May 02 '26 01:05

Byron Jones


After diffing through both Azure HTTP Trigger as well as the "Generic Web hook" functions, I see no difference except that the Web hook mode are offered on both constructs and can be interchangeably used, where the Web hook mode needs a API key whereas its optional on the HTTP Trigger construct.

The Generic JSON option lets you dictate how your Web hook can look like unlike third party providers like Git or Slack.

like image 40
user10244115 Avatar answered May 02 '26 00:05

user10244115