April 09, 2024

Beginner's guide to webhooks

What are webhooks ?

Webhooks are basically "HTTP callbacks".

You must be familiar with push notifications. These are the notifications that you receive on your phone when you receive a new message, new email etc.

Webhooks are similar. They enable one application to receive notifications from another applcication when a certain event occurs.

How webhooks work ?

Imagine you have a personal blog. You want to get notified through email whenever someone subscribes to your blog. You have third party app that manages your subscriptions. You can use webhooks to achieve this. Here's how it works.

  1. You will register the send email URL with the third party service. This URL will be used to send notifications to your blog.

  2. Whenever someone subscribes to your blog, the third party service will send a POST request to your URL with appropriate metadata in the request body.

  3. Your email service will receive the request and can send an email to you.

Benefits of webhooks

  1. Real-time notifications: Webhooks allow you to receive notifications in real-time, which is crucial for many applications.
  2. You dont need to implement unnecessary polling.
  3. Allows you to triggere actions automatically based on certain events.

Best practices for webhooks

  1. Make sure you have rate limiting configured for your url to prevent abuse.
  2. Make sure you dedup the events to avoid processing the same event multiple times.
  3. Log all the events to a database for auditing purposes.

Try a webhook for yourself

  1. Go to https://webhook.site and create a new webhook. When you open this site, it will generate a unique URL which you can use to send webhooks to.

  2. Now if you send a POST request to this URL, you will see the request in the webhook site.

Curl to make POST call

curl -X POST https://webhook.site/f1842f14-ad9f-4c1b-a5ab-ed1684ee300c -d "key=value"

curl -X POST https://webhook.site/f1842f14-ad9f-4c1b-a5ab-ed1684ee300c -d "key=value"

Event being showin in webhook.site