hey my name is Milan and welcome to the first video in a series of videos that I'm planning to make about implementing web hooks in net in this first video I just want to cover what web hooks are how and when you should use them and I'll show you how you can Implement a simple web hook system that works in memory I'll start by explaining what web hooks are and how you can use them let's say that we have two applications a client application and mind you this could be a backend service it doesn't have
to be a client side application and a service provider which is the actual API or service that we are building and we are the ones offering a web hook so what does the client application do the client registers for a web hook that we are providing and gives us a URL where we can send the web Hook when something happens so this is essentially a way for two applications to communicate with each other using HTTP and it's a push-based communication approach this means that the service provider which is the application that we are maintaining is
going to send a request to the web hook URL when something significant happens and this is very similar to event driven architecture if you are looking for something to compare it to so what happens next after we register for the web hook sometime in the future the event that we register for occurs and it's important to note that this event occurs in our service provider the service provider is then going to send a post request with a pre find payload to the web hook URL that we specified when we were registering for this web hook
let's say that the client application returns 200 okay this completes the web hook flow and we are done with processing the web hook the client application the one that was requesting the web hook can now process this data asynchronously and our service continues to serve any other requests that it needs to now there are also some alternative scenarios that we can explore the first one is what happens if the initial request send the web hook payload fails well in that case we need to implement a retry mechanism so we can try a couple of more
times send a request to the web hook URL hoping that it's going to succeed and in most cases it will and we're going to get back at 200 okay response so that we can complete the web hook request but there's also a situation where the web hook fails even after retries and then we have to implement some sort of compensation mechanism we're going to discuss this in one of our future videos another interesting topic to consider when it comes to web hooks is how do we verify that the web hook URL that was provided to
us in the web hook subscription is actually valid we could Implement some sort of challenge mechanism and this is also something that we're going to explore in a future video so what are the benefits of using web hooks for implementing service to service communication over HTTP first of all this is a push-based or event driven approach and it means it's more efficient instead of the client application having to pull our system for when an update is completed we can Implement a web hook where we we are the ones notifying the downstream service when something significant
happens so this is much more resource efficient it allows our service to handle more requests because it doesn't have to deal with polling now of course there are some drawbacks implementing a web hook system and doing it properly is definitely not simple you'll see in my future videos what are some of the things that you have to consider but at the tip of the iceberg we have to deal with failures retries security is a very big concern when implementing web Hooks and of course we'll have to think of a smart way how we're going to
scale our web hook implementation you'll see that this is a very interesting Challenge and now let me show you how we're going to implement the most basic support for web hooks in a net 9 API all I have is an inmemory repository for some orders and I have two endpoints where I can send a post request to create an order and a get endpoint where I can list the orders that I created we have the order class representing our one simple entity so let's go ahead and create another one that's going to represent a web
hook so I'm going to start by introducing another type so let me create a class I'll call it web hook subscription and I'm actually going to make this a record so let me say public sealed record and I'll even make it a positional record because I only want a few properties on this type so the first thing I'll need is a way to identify my subscription so I'm going to assign an ID property then I'm going to need the event type and this is going to be the reason why I'm actually sending a web hook
and and then I'll need a web hook URL where we are going to send the web hook payload and we can also create a daytime field or property let's call it created on UTC so this is going to be my web hook subscription that the client application is going to request and I'm going to create another type that I'm going to use for my post request to actually create a subscription and it's going to contain the event type and the web hook URL the next thing I'll need is a way to persist my web hook
subscriptions so that I can actually retrieve them and then send a request to the web hook URL now I'm going to create an inmemory repository identical to what I did for my orders and we're going to improve on this in our future videos so let's call this the inmemory web hook subscription repository and I'll change the type inside from an order to uh web hook subscription awesome so let's move this into its own file and I will have to register this with dependency injection so let's go ahead and do that I'll just copy what I
had here and I'll specify in memory web hook subscription repository now when it comes to the actual implementation the get all method isn't all too useful what I'm actually looking for is a way to retrieve web hook subscriptions based on the event type so let's create a method like that that accepts an event type argument and then we're going to filter on the orders where the subscription event type is equal to the event type argument that we get so let's call to list on this and I'm going to rename the backing field from orders to
subscriptions so this is all I need for my inmemory repository now let's actually create an endpoint where I can register for a web hook I'll create a post endpoint using minimal apis and let's say the route is web hooks subscriptions so what do I need to do here I need to accept a create web hook request this is going to be my request body and then I'll need my inmemory web hook subscription repository let's call it subscription repository and the implementation is going to be very straightforward and this is because everything works in memory so
let's create my web hook subscription type let's call it subscription and I'll just call the Constructor and I need to provide a new ID and then the event type and the web hook URL are going to to come from my request then I can specify dat time UTC now and I have my web hook subscription so now I can use my subscription repository to add the actual subscription and I can return results. okay and let's just return the subscription that we created so this is our simple implementation now when I start my API I'll be
able to send a post request to this endpoint And subscribe to an event now what are the event types that I want to support let's say that after we create an order we want to publish an order created event but in our system instead of publishing an event to a message broker we want to dispatch on HTTP request to any subscribed web hook URLs so you can see how this is similar to event driven systems in one way but it's slightly different when it comes to the communication approach the main use cases for web hooks
are integrating with systems that you don't own for example most popular payment providers provide web hook endpoints where you can subscribe to notable events for example stripe offers web hooks for when a payment is made when a payment fails and so on I also had to implement similar functionalities in the past for example I was working on a system where you could upload a file an audio file and the system was responsible for using AI to transcribe the file and generate a text document and it would notify you that the text document is available by
sending you a web hook that you provided in your initial request so we already have this in place we we have our post endpoint for creating subscriptions now we need a way to dispatch our web hooks so I'll create another folder let's call it services and I'm going to add another type inside and this is going to be my web hook dispatcher I'll make this internal and sealed and what are going to be my dependencies I'll need an HTTP client because I'm going to be sending HTTP request and then I'll also need an inmemory web
hook subscription Repository let's create that so let me just align this vertically and let's say that this exposes just one async method called dispatch async where you can dispatch uh web hook request for a given event type and you can also provide the payload now I'm going to use an object because my payload won't be strongly typed and then the implementation is going to be more or less simple I need to access my repository and find any subscri itions for the provided event type then I can iterate over the subscriptions and for each Loop and
for each web hook subscription I want to send a respective web hook request so my request object is going to be an anonymous type let's go ahead and initialize some of the properties let's say that I assign it a new ID by initializing a good then I'll provide an event type from the web hook subscription I'll also provide a subscription ID as the web hook subscription ID then let's provide a Tim stamp for when we send the web hook request and let's also provide the data that is just going to contain my payload so then
what am I going to do I'll say HTTP client post as Json async and I'll send a post request to the web hook URL and I will be sending my Anonymous request object it will be serialized as a Json object project and send to the respective URL so now the next thing I need to do is to register this as a service I'll will say Builder services at HTTP client and I'll just register the web hook dispatcher as a type client this is why I can inject an HTTP client using dependency injection and the only
thing that's left to do is to actually use the web hook dispatcher and I'll do that in my post endpoint when I'm creating the order so let's inject the web hook dispatcher I'll make this asynchronous and let's say after we complete adding the order I can say web hook dispatcher dispatch async and for example the event name could be order created when it comes to the payload you can decide what that's going to be I'm just going to pass in the entire order but you don't have to do this you can create a custom schema
just for the payload that can contain a redacted set of fields so now let's actually try this implementation out and I'll show you how you can test your web hooks I'll use a simple service called web hook. site that gives you a unique URL that you can use for posting your web Hooks and I'm going to copy the unique URL that I was assigned with the application running in the background I'm first going to send the post request to my web hook subscriptions endpoint and I specified my web hook URL the event type that I'm
subscribing for is order created so let's go ahead and send the request you can see it lands in our minimal API endpoint and I'm going to remove this breakpoint and I'll create two more web hook subscriptions and now let's go ahead and create an order so we create an inmemory instance added to the repository and finally we're going to dispatch the order created web hook so let's go into the implementation we're going to retrieve our webook subscriptions we on the repository now obviously there should be some persistence mechanism like a database where you won't to
risk losing data in our initial implementation this will be just a simple inmemory list so now I'm going to rate over each of the web hook subscriptions and I'll send a post request using the HTTP client so let me hit continue this is going to complete and if I head over to web hook. site you're going to see that we received free post requests this is because we had free web hook subscriptions and here you can see some of the details for our post request so it contains the web hook payload with the ID the
event type the subscription ID the timestamp for when we sent the web hook request and it also contains our custom payload representing the order we have two more requests because we had free subscriptions but they all contain the same data because they subscribe to the same event so this is a simple way how you can obtain a throwaway URL to use for testing your web Hooks and in the future videos in this series we're going to explore how we can improve on this web hook implementation we'll start by implementing support for persistence with a database
and then we're going to discuss how we can handle failures how we can improve security and scalability so if you don't want to miss the upcoming videos in this series make sure to subscribe to my channel so that you get notified when they release if you want to continue your learning check out this video next go ahead and smash the like button under this video and until next time stay awesome