DEV Community

loading...
Cover image for Webhook

Webhook

tiago123456789 profile image Tiago Rosa da costa ・3 min read

What is it?

Is one request http using the verb post that is triggered when one event in the application to notify another application.

When do you use it?

One example is api payment where use webhook to notify a third application about status payment. Imagine an api payment implemented one form communication for each client, it’s impossible to implement and maintain it.

So, when your application needs to send data for a third application when an event occurs and needs one simple form to make this, webhook is one good solution for it.

How to work?

I go use an example of the payment api. When a client create one transaction him informed webhook url and payment api when change status the transaction send data for webhook url informated.

Image below illustrate what i spoke:

Alt Text

How to make webhook safe?

The webhook url is public, so anyone can access this url. To make webhook url more safe enable https in webhook url and add secret in webhook url for when application trigger webhook url the third application will check if secret is valid, if yes, process request, case no, reject request. Example webhook url with secret: https://domain_url/rota?secret=value

What situations apply webhook?

  • The first scenery using webhook url to create CI/CD pipeline, explain flow:

    • Add webhook url the jenkins for trigger when push code in repository
    • When webhook url is triggered execute one job
    • The job run tests the project and after trigger another job
    • The second job to deploy changes in staging or production server.
  • The second scenery using webhook url is automate deploy, explain flow:

    • Add webhook url the for trigger when push code in repository
    • When the webhook url is triggered execute one code that changes the project in staging or production server.
  • The third scenery using webhook url is one course platform, I will explain flow:

    • People buy course using credit card
    • Send datas payment api
    • When payment is approved it triggers a webhook url on the course platform to enable courses for people.

The example application

In file index.js is code example:

Alt Text

Explaining image above:

  • In this file I’m use express for create application and define route /webhook
  • In line 9 until 20 I’m check if request has a token valid, case yes, process data, case no, no process.

In the github you set a webhook url. Image example:

Alt Text

Explaining image above:

  • In Payload URL you set url and add token. Example: webhook_url/route?token=value_token
  • In Content type set application/json to application webhook receive data in format json.
  • In section “Which events would you like to trigger this webhook?” I set an event trigger webhook, this case when I make push code for a repository.

When you have one application need handler many webhooks is necessary think about one strategy for scale up and prevent that affect performance the principal application, because when application make request third application this third application can low performance and it is affect your application.

The image below is a solution for this problem is:

Alt Text

Explaining image in above:

  • Actor create transaction api payment
  • When occur one event that is necessary trigger webhook, send data for message queue
  • Another side has a job to get data and make request to notify third application.

So, my friends, here I finish one more article, case you have one question about webhook, comment below I try reply with one solution.

Link the project repository in Github: https://github.com/tiago123456789/webhook-article-project

Discussion (0)

Forem Open with the Forem app