Webhooks run a large portion of the "magic" that happens between applications. They are sometimes called reverse APIs, callbacks, and even notifications. Many services, such as SendGrid, Stripe, Slack, and GitHub use events to send webhooks as part of their API. This allows your application to listen for events and perform actions when they happen. In a previous article, we looked at how to . In this article we'll look at how you can listen for webhooks using Python (v3+) with the or frameworks. consume webhooks with Node.js and Express Flask Django This guide assumes you have Python v3 installed on your machine. You can find details on installing python at the . Depending on your setup, the command you want to use may be . official downloads page python python3 You can confirm the installed version by running the following from the terminal: python --version or, if that displays a version below 3: python3 --version What is a webhook Webhooks are called reverse APIs for a reason. Instead of your application sending a request to the API, the API sends the request to your application. While the concept may sound different, the way that we consume webhooks is the same way that an API consumes a request. In most web frameworks, there is the concept of a route. A route allows the application to respond with specific content or data when the user visits a specific URL. The same idea applies to APIs. When you send a request to GitHub for details about a specific organization, such as , the route is where is the name of the organization. https://api.github.com/orgs/Bearer /orgs/:org :org The same concept is applied when receiving webhooks. We establish a route, tell the service where to send the data, and our application sits and waits until a request comes in to that route. There are a few consistencies across webhook implementations. They are normally requests. POST They receive JSON data. They need to respond quickly. Some APIs will require that your application respond within a certain amount of time, otherwise the event will be re-sent. For example, the expects a response back within three seconds. Slack API Receive a webhook with Flask The is a lightweight Python web framework that describes itself as "micro". This allows you to use just what you need, then add more structure later as your project grows. Flask framework For our purposes, this is great as we are only concerned with routing. Make sure Python is installed, then run the following command in your terminal to install flask: python -m pip install Flask You can find full installation and setup details on at the . Flask documentation Next, create a file, such as and add the following: .py main.py flask Flask, request, Response app = Flask(__name__) @app.route( , methods=[ ]) def respond(): print(request.json); Response(status= ) from import '/webhook' 'POST' return 200 This code imports the Flask class along with the request and Response objects. Then instantiates it with a name of before assigning it to the variable. This naming scheme is convention in the Flask documentation. __name__ app Next, we use the decorator to listen for requests made against the path. This decorator calls the function that immediately follows it when a request is made to the route. In this case, that is the function. @app.route POST /webhook respond For the purpose of this example, we out the request as json, then return a with a status code of 200. This response tells the sender that we received the hook. You should be able to run the server using Flask's preferred technique: print Response FLASK_APP=main.py python -m flask run export Thats's it! We now have an app that listens for a webhook with python and flask. Once deployed, requests made to the endpoint will trigger the function. POST respond For example: This is also the URL that you will provide the the service that sends the webhook. https://exampledomain.com/webhook. Receive a webhook with Django Setting up an application in Django is more automated than Flask, but that also comes with a more elaborate file structure. As a more traditional Model-View-Controller (MVC) framework, Django scaffolds out the main parts of the project for you. A full installation guide is available on , but it can also be installed with using the following command: the official Django documentation page pip python -m pip install Django If you're setting up a project from scratch, use the utility to create a new project. If you already have an existing Django project that you want to add webhooks to, skip to the next step. django-admin django-admin startproject example-project This sets up the basis for a new Django project. Navigate to the newly created folder, and you should see a structure similar to the following: example-project/ manage.py example-project/ __init__.py settings.py urls.py asgi.py wsgi.py We can confirm that everything worked by running . python manage.py runserver Django's convention is to set up "apps" within this outer "project". You can avoid this and set up a single-app project by running with a trailing period (.) instead. For this tutorial, we'll mirror the preferred way as shown previously. django-admin startproject example-project . To do that, we'll set up an "app" called . webhooks python manage.py startapp webhooks This creates a new directory called . Great! Now we can write some code. webhooks We'll be focused on three files: , (not yet created), and . webhooks/views.py webhooks/urls.py example-site/urls.py Open the file . Here we will write the logic for handling a route. webhooks/views.py django.http HttpResponse django.views.decorators.http require_POST HttpResponse( ) from import from import @require_POST : def example (request) return 'Hello, world. This is the webhook response.' This code does the following: It imports the object that will be used to send a response. HttpResponse It imports a special decorator to limit the request method. In Django, routes accept all HTTP methods by default and let the views manage which methods they respond to. Calls the decorator to limit the function that follows to only the method. POST Defines a function, named that takes the request as an argument and returns a response. example This function's name will be linked to our file shortly. It does not need to align to a specific path. example urls.py Next, create if it doesn't already exist. This is where we organize routes within this sub-app of our project. webhooks/urls.py django.urls path . views urlpatterns = [ path( , views.example) ] from import from import 'example/' Here, we import from . It defines individual routes and associates them with views. We next import all views. path django.urls Finally, is passed a list of paths. This list is recognized by Django as the routes associated with the application. urlpatterns In this instance, we define a path that targets and is associated with the view , which was the name of our function in . example/ views.example views.py With this done, our application works, but we need to tell the outer project about it. Open . It should look similar to the previous file, but with an existing route. Add a new path like so: example-project/urls.py admin urlpatterns = [ path( , admin.site.urls), path( , include( )) ] 'admin/' 'webhooks/' 'webhooks.urls' If your server stopped, run it again with . python manage.py runserver Now try making a POST request to (replace the host and port with your own if they differ). http://127.0.0.1:8000/webhooks/example/ With that, we have set up a Django project that listens for a webhook at . Once deployed, append this path to the full URL and provide the full URL to the service that sends the webhook. /webhooks/example Testing webhooks locally To test webhooks locally without deploying, we need to open up a connection from our development machine to the outside world. One option is to use . This service allows you to provide outside access to a specific port on your local machine. This works great for our needs. To get started, sign up and follow the installation and getting started instructions. ngrok Once done, if you're on MacOS, you should be able to run in your terminal where 3000 is replaced with the port of your running Python application. For example, a default Django site often runs on port 8000. ./ngrok http 3000 Once up and running, ngrok will provide you with a url that you can use to test your webhook. In Bearer, we provide a "test" button in our Notification settings. Once configured, you'll start receiving webhooks at that URL. Don't forget to change it over to the final, deployed webhook URL once development and testing are complete. What can you do with this information? Once a webhook is configured, it is up to you how to handle the information it receives. You can use this information to react to events in real-time, feature flip parts of your application, or even use it as a way to write data to a database. There are countless capabilities your webhook can have depending on what kind of information the API provider sends. While we built a basic implementation in this article, it is worth mentioning that many services offer ways to validate that a request is coming from the actual source. This can be done by limiting the URLs that access your application, or by matching a secret key. GitHub, for example, allows you to set a secret that will be sent with each webhook. Explore the documentation for the services you use to see how best to work within their setup. If you like the idea of using webhooks to react to monitoring changes with your third-party API providers, . check out what we're building at Bearer.sh