What is a WebHook?

webhook

A few days ago I needed to consume a webhook and it caused some confusions in my mind. Now it’s clear for me but at that time, I couldn’t understand what exacly it was, how it worked and the difference between a webservice and a webhook.

And that’s why I am here now writting this post (:

Definition

Webhooks are “user-defined HTTP callbacks”. They are usually triggered by some event, such as pushing code to a repository or a comment being posted to a blog. When that event occurs, the source site makes an HTTP request to the URI configured for the webhook. Users can configure them to cause events on one site to invoke behaviour on another. The action taken may be anything. https://en.wikipedia.org/wiki/Webhook

Webhook vs webservice

It’s like an inverted API endpoint. Instead of making a call for any API, you define a callback URL to which the service that provide the webhook will send an HTTP POST for your API. That’s the difference between the webhook and webservice.

Consuming a Webhook

The first thing you need to do is to set up a URL to your webhook provider. Generally, you can do it through a control panel or an API. Basically, that’s all . After a registered URL, the events will be sent to you. So now, you need to be prepared to receive them!

Below we can see a list of events and their flow. According to these events you can start creating your API.

SENDGRID-WEBHOOK-3

SENDGRID-WEBHOOK-2

https://sendgrid.com/docs/API_Reference/Webhooks/event.html

Data sent

When and what kind of data will the provider send me?

It depends on your provider. Probably there is a documentation listing the events and the kind of data you need to expect. Most of webhooks will POST data to you in one of two ways: as JSON or XML to be interpreted, or as a form data (application/x-www-form-urlencoded or multipart/form-data).

Example of data:

SENDGRID-WEBHOOK-5

https://sendgrid.com/docs/API_Reference/Webhooks/event.html

Conclusion

Webhooks are excellent and provide us a better real time integration. It’s growing and most of the big companies already have it. Sure, you can create a webhook and not just consume them. This post was just an introduction but there are other important things, for example how to debug it and security things. You can find these kind of things easily (:

Why to write a good code?

font-end-vs-back-end

A few weeks ago, I came across this image above and because of that I’ve decided to write this post, showing the importance of writing a good code.

What does “a good code” mean?

I think the variables may change slightly depending on the language we are using, but there are some general rules that apply to all languages.

There are some points:

  • When you have to add or modify a feature, is it too difficult?
  • Do you constantly break existing functionality when making changes?
  • Is it hard to understand some code that you are seeing for the first time?

If you’ve answered “yes”, you need to start reviewing your code.

Why to write a “good code”?

Doesn’t matter if we are talking about client side or server side. The consequences of writing a bad code can be terrible for the developer himself.

What is my code doing?
This question seems to be crazy, but I am sure that you have already asked yourself.

It is just a text field, should be easier to implement
Yes, sometimes an easy thing can become a problem or take a long time to implement.

I just modified that and nothing else works.
Functions and code blocks should be well defined and isolated, so you would not destroy the whole system when editing a single line.

How to write a “good code”?

  1. Plan Before You Write
  2. Commenting & Documentation
  3. Good Indentation
  4. Avoid Deep Nesting
  5. Consistent Variable Names
  6. Code Refactoring

If you do not know some concept above, please just google it. I’ve listed a few important ones, but there are a lot more.

Conclusion

Try to challenge yourself in writing a better code. Ask for help from a more experienced developer on how you could improve your code. And the most important, before writing the code, think about the best way to do it, even if it takes a few precious minutes, I’m sure it will be worth it.