Webhooks are a great tool to automatically track activity in your Labelbox organization, but they can be tricky to properly set up. In this post, I’ll give a step-by-step guide to setting up a server in an AWS EC2 instance and connecting it to Labelbox to receive webhooks. For more general information on webhooks, check out the Labelbox docs page on webhooks.
First, we’ll need to go to our workspace settings in Labelbox, which can be found by clicking on your icon in the bottom left corner
Then we’ll select the Webhooks tab and click Set up webhook. I’ll be creating a global webhook in this guide, but the steps will be the same for a project-specific webhook. We’ll leave the webhook creation fields empty for now and go over to AWS to set up an EC2 instance for our webhook.
From the EC2 landing page, select Launch instance. From this page, all you need to do is create a name for the instance and the rest of the settings can be left as the default. We’ll set up the traffic security rules for this instance later.
Now click on Launch instance on the right and click on the id of the newly created instance to get to the instance summary page. Look for the Public IPv4 DNS field as this is the URL that we will need to use for our webhook settings. Copy the Public IPv4 DNS and head back to Labelbox.
In the webhook settings, we’re going to put http://<your_EC2_URL>:8000/webhook in the URL field. Make sure to replace <your_EC2_URL> with the value that you copied from the Public IPv4 DNS. The URL should look something like this:
Next add a secret that will be used to verify that requests are coming from Labelbox and select the topics that you want to receive webhooks for. I’ll be selecting all topics for this guide. Finally, click on the Save button at the bottom of the screen.
Now our webhook is fully configured on the Labelbox side, but we have some more work to do in AWS in order to start receiving webhooks. Head back to AWS and select the Security tab below the instance summary. Click on the security group URL, then click on “edit inbound rules” on the following page. Click on “Add rule” and then create a rule with the following settings:
- Type - Custom TCP
- Port range - 8000
- Source - Anywhere-IPv4
Click on Save rules and head back to your instance. Make sure the instance is running and then click on Connect. Click on Connect again on the following page and you should find yourself looking at a Linux terminal for your instance.
Run the following commands to set up a file and directory for your webhook server:
Next we’ll need to install pip in order to install some of the python packages that we’ll be using to set up the webhook server. Run the following commands to install pip:
Now that pip is properly installed, we can install some of the packages we’ll need, for now we only need flask:
pip install flask
We’re now ready to create our server, run
sudo nano app.py and paste the following code:
from flask import Flask, request import json import hmac import hashlib secret = b"<replace with secret>" app = Flask(__name__) @app.route("/webhook", methods=["POST"]) def print_webhook_info(): payload = request.data computed_signature = hmac.new(secret, msg=payload, digestmod=hashlib.sha1).hexdigest() if request.headers["X-Hub-Signature"] != "sha1=" + computed_signature: print( "Error: computed_signature does not match signature provided in the headers" ) return "Error", 500, 200 print("=========== New Webhook Delivery ============") print("Delivery ID: %s" % request.headers["X-Labelbox-Id"]) print("Event: %s" % request.headers["X-Labelbox-Event"]) print("Payload: %s" % json.dumps(json.loads(payload.decode("utf8")), indent=4)) return "Success" app.run(host="0.0.0.0", port=8000)
Use ctrl-o to save the file and ctrl-x to exit nano. Now run
python3 app.py to start the server. With the server running, go back to Labelbox and submit a label in a project to make sure the webhooks are properly set up. If everything is working correctly, you should see the webhook printed in the terminal. It may take some time after submitting the label before you see the webhook.
I’ll be making some additional posts in the next few weeks expanding on this topic with some more in-depth use cases for webhooks. Please post any questions you have in the comments below or share any interesting use cases you’ve found for webhooks!