 Hello. In this next section, we'll be talking about webhooks, but we're going to go over the fundamentals of webhooks and what they are and build one from scratch to give you intuition about how they work and what they are. So webhooks are very fundamental in software engineering. They're everywhere. If you google webhook, you'll see a lot of different references to webhooks. Particularly, you'll see webhooks used in many different dev tools. So you'll see things about GitHub, integrating webhooks on GitHub, Zapier, Twilio, Stripe, SendGrid, so on and so forth, webhooks are very common in a lot of developer tools and in a lot of infrastructure. In other words, webhooks are really common mechanisms that different infrastructure providers use to communicate with each other. And it's like a flexible way for you to package a communication between one tool, like weights and biases, and another tool. So what is a webhook? Well, if you Google it and try to read about it, it can be kind of confusing. So for example, the Wikipedia page webhooks defines webhook as a web development. It's a method of web development, basically where you have a web application with custom callbacks. it's a method of web development basically where you have a web application with custom callbacks if you don't know what a callback is that's fine a callback is just a piece of a function or some code that is going to be executed at a later time now even that i think is a little bit more complicated all this terminology is more complicated than it needs to be for you to understand what a web hook is and i think the for you to understand what a webhook is. And I think the best way to understand what a webhook is, is to basically build one. And we can do that with Python. And so that's what we're going to do right now, is let me open some code. So there's two components to a webhook. One is a web server that will receive the webhook and then the client that's going to send the webhook and so i have the code in this repo called 1db modal webhook and let me go ahead and open uh the the code that i want to go over. So the first thing we want to make is the webhook server. And we're going to use FastAPI for this. FastAPI is a very popular Python library for creating web servers. And basically what this is, is we do the usual FastAPI things, things is we set up the the server and this class is actually the data this is a pydantic model is basically like a data class if you have if you're not familiar with pydantic it's it's it's like a data class that just specifies what kind of information though the web server is going to receive from the client so it's going to receive some kind of package that has all of these fields. An event type, an event author, an alias, artifact version, so on and so forth. And I made this particular kind of data for a very specific reason. This is actually a mock-up of what weights and biases sends to a web server. We don't have to get into that now. It doesn't really matter what this is, to be honest with you. This is just the data that this particular web server expects, and it's going to validate that in this schema. This underscore, this dunder str, this is just customizing exactly how this information is printed out so if you don't know about dunder str this is just a special method you can override in python that lets you control how something is printed out and in this case we're just printing out all of these different variables in a very particular way. Finally, we have the web server. The web server has different endpoints. So this is the root endpoint here. And this root endpoint is going to get two different, is going to get the event, which is this kind of this JSON that has this schema. And then it's going to get a token. The token is going to be pulled from the header. And I'll show you that in a moment. The token is going to check that the token is the secret random token. This is not secure, by the way. This is just me showing you a very basic workflow, a very basic webhook. And then this is just a check to say, hey, if the token isn't correct, just raise an exception. And this is just, you don't have to, you know, this is just a little bit of a verbose exception, but just want to set the stage for how you would do it properly. of a verbose exception but just want to set the stage for how you would do it properly. And if the token is correct, the web server is going to return this message. That's all it's going to do. And so what do we mean by callback? So callback is just any function that you would execute when receiving this event. In this case, we're just returning the string, but you can imagine there could be another function like, let's say, pull model or something like that, and then you would execute that here. That would be an example of a callback where this web server receives some information. Upon validating the information, it decides to execute some code. We don't have to get into that because I don't want to confuse you. I want to just show you the very basics of a webhook. Like, what does it mean? What does it mean? And so, let's start this web application so I'm gonna go ahead and open the terminal and I'm gonna zoom in so you can see my screen this is a little bit of a split split screen so hopefully you can see it let's just zoom in all the way make it really big let's make it really really big big. Let's make it really, really big here. Okay. Let's make this side really, really big as well. Okay. So the first thing we want to do is start that web server. So I'm going to type in this command. Let me just get back to it. Oops. There you go. So UVicorn is just the thing that runs the web server. And the name of this file is fastapiserver.py. So this is fastapiserver.py. And then this is the app. The name of the application is app. That's why you have UVvicorn fast API server app. This starts the web server. And you can see on this side, this is kind of serving, this is the logs of the web server. And what we want to do is we want to send a payload to the web server, like some kind of request. And so this file curl.sh has a minimal request basically what we can do is say we have this curl command let me just zoom in a little bit more so this curl command again this is in curl.sh has the following headers headers has the authorization header. You have to make sure that you put this bear word in here, otherwise it's not going to work. And then you want to have the content type is JSON, and we're going to send exactly that schema that we saw here in the base model. We have the event type, the event author the event author the alias artifact version and so on and so forth you can see that this is that's exactly the information that we're sending it that's that's what it expects and here is where it's going to check that this token is going to match the secret random token is actually is going to match. The Seeker Random Token is actually going to match. That's what it's checking. And then you don't have to mind this stuff up here. This is just saying use localhost unless I say otherwise. And so if we go back here, we can just run that shell script. And when I run that shell script, you see message, the event is processed successfully, and you see these logs printed to these logs have been printed. And these logs being printed in this way is just the result of this specific Dunder STR definition because this gets printed. See where it says print event right here on this side? This print event is actually what's responsible for the event being printed like this. And we'll come back to this. This is how when we deploy this web server onto a more production-grade application, you're gonna be looking for logs like this. And this is actually, this information is kind of the same things, the same type of information that weights and biases will send you. It doesn't make sense now, like what this information means, like what is an event type, what's an alias, What does that mean? That will make sense. I just wanted to ground you on like what a web server is. And what you should do is like play with this. You should say, okay, let me kind of, what happens if I mess this up? Like what if I take this out and then, you know, try to send a request? What happens? And you get an error. See, like this unprocessable entity, and it says, hey, does he have a field required that you didn't send? Something is wrong. And we can put that back and try again. And then you see now it's successful, and now we have those logs printed again so this is key this is the basic kind of idea of what web hooks are just one application so on this side this is just me using curl sending some data to another application which in this case was this fast API server. And what's going on is in production, what you'll do is instead of using curl, we're going to use weights and biases to send this information to some application. That could be GitHub. That could be another machine learning infrastructure application, we're actually going to use something called modal. Just because modal is very lightweight and is accessible to everybody, just to show you how you can wire up weights and biases to modal and kind of understand what is happening. But this like a minimal example is really helpful for you to sort of understand what's happening when I show you webhooks and weights and biases. Thank you.
Webhooks
Webhooks are very common in a lot of developer tools and infrastructures. Webhooks are common mechanisms that different infrastructure providers use to communicate with each other. It is a very flexible way for you to package information between one tool, like Weights & Biases, to another tool. 

Resources: 

-W&B Modal Web Hooks repo - code for this lesson! https://github.com/hamelsmu/wandb-modal-webhook/tree/62c91b055a343f802410ee328e6d70ec602c7ee
-FastAPI documentation: https://fastapi.tiangolo.com/

-If you want to learn more about Pydantic, check out our newest LLM Engineering course on Structured Outputs: https://www.wandb.courses/courses/steering-language-models