Welcome to FullStack. We use cookies to enable better features on our website. Cookies help us tailor content to your interests and locations and provide other benefits on the site. For more information, please see our Cookies Policy and Privacy Policy.
If you have worked in web application development, you have most likely heard of the term "webhook", which has become more popular with the rise of multiple web application and service integrations. But it is not always clear what exactly it is. To explain what a webhook is, let's briefly review how a regular web API works.
A web API is a web application in which information is requested or sent and acts as a "server" constantly waiting to receive these requests from a "client". In this case, a web API is only waiting to receive requests. Let's say that it never "takes the initiative" to communicate with another application.
What is a webhook?
A webhook is a mechanism for making a web application communicate with other web applications by sending them information when certain events occur. Webhooks are sometimes also called "reverse APIs" because they do not wait for other applications to initiate communication by making requests, but rather they initiate communication by sending information to other applications. Webhooks are an efficient lightweight way to enable real-time notifications and data updates between applications.
Some examples of use cases for webhooks are:
Sync customer data in other applications like CRMs.
Notifying the courier company to collect a product after it has been purchased in an online store.
Sending some kind of data to external data analysis services.
Let's start by globally installing the NestJs CLI tool to initialize and set up NestJS applications.
npm i -g @nestjs/cli
Create a new project with the following command:
nest new webhook-app
This codebase creates a simple web API that exposes a single endpoint at the root address localhost:3000/. Let's say we want to notify a shipping application to ask them to pick up the product. For that, we will create the endpoint to create an order. We will modify the following files:
// app.service.tsimport { Injectable } from'@nestjs/common';
@Injectable()
exportclassAppService{
createOrder(data) {
// we omit the implementation of order creation return data;
}
}
Webhooks are usually implemented using HTTP. So to make the request to notify the shipping application from our Web API, we'll need the NestJs HTTPModule.
// Install the HTTPModule and import it into AppModule.
npm i --save @nestjs/axios
Now, we will trigger the notification to the other application to notify it of the newly created order. We will do it right after calling the service that creates the order. But before that, the other applications must provide us with an endpoint to which we will make the request. We will make use of the following online tool for testing webhooks: https://webhook.site/. So this site will play the role of the other app. Just go to that address and copy the URL shown where it says, "Your unique URL." That's the one we'll use. Do not close the browser tab in order to verify if the webhook is working correctly!
Finally, let's test that the webhook is called correctly when creating a new order. You can use a tool like Postman to make the request to our localhost:3000/order endpoint. We'll send it whatever data we want, we just want to test the webhook flow.
POST /a13b5452-5f37-485b-be82-92b84a1c30ab HTTP/1.1Host: webhook.site
Content-Type: application/json
Content-Length: 85
{
"productId": 1,
"amount": 1,
"deliveryAddress": "Fake Street 123"}
We verify this in the browser tab where we have webhook.site open, and you should see that a request was received with the data that we sent to our webhook. Assuming the webhook.site was the delivery app, you've already been notified to pick up our product at the address we sent you.
Done, we have implemented a basic webhook. As you noticed, using a webhook is very simple, just a matter of making an HTTP call from one application to another. In a more complete application, you will have to consider the extreme cases in which the request fails, such as: making retries, marking the synchronization status with the webhook in the database, and other aspects.
A webhook is a way for one application to automatically send data to another application when specific events occur. Instead of waiting for another system to request information, your app takes the initiative and pushes updates in real time. For example, when a customer places an order, a webhook can notify a shipping provider instantly. This makes webhooks ideal for syncing data between apps, triggering automated workflows, and improving response times.
Why should I use webhooks instead of traditional APIs?
With traditional APIs, a client has to repeatedly request data to check if anything has changed. Webhooks solve this by sending updates automatically whenever an event happens. This reduces server load, speeds up communication, and enables real-time integrations between services. For applications that require instant notifications — like payment confirmations, order updates, or analytics tracking — webhooks are far more efficient than polling endpoints.
How do I implement a webhook in NestJS?
NestJS makes implementing webhooks straightforward. You start by creating an endpoint in your application that can receive and process incoming data. When an event occurs, your application can also act as the sender, making an HTTP request to another service’s webhook URL. NestJS’s HttpModule simplifies sending these requests, and you can test the setup using tools like webhook.site or Postman. Once configured, the webhook automatically triggers whenever the event occurs.
How can I test that my webhook integration works?
One of the easiest ways to test your webhook setup is by using tools like webhook.site or Postman. These tools let you generate a unique URL where your application can send data when an event occurs. By monitoring the incoming requests in real time, you can confirm that your webhook is firing correctly and that the payload matches what you expect. Testing early ensures that your integration works before connecting to a production system.
What are some best practices when building webhooks in NestJS?
When implementing webhooks, reliability and security should be top priorities. Always handle potential failures by adding retry logic in case the receiving application is unavailable. Log webhook events for debugging and tracking purposes. If sensitive data is being sent, secure the communication using HTTPS and validate payload signatures where possible. Finally, make your webhook endpoints lightweight and fast to avoid timeouts and ensure smoother integration across systems.
AI is changing software development.
The Engineer's AI-Enabled Development Handbook is your guide to incorporating AI into development processes for smoother, faster, and smarter development.
Enjoyed the article? Get new content delivered to your inbox.
Subscribe below and stay updated with the latest developer guides and industry insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.