Webhooks are used by a wide variety of services for countless reasons, from GitHub PR notifications to Stripe payment alerts. However, many services do not allow for the addition of custom authorization headers or code-level customization of the calls, so you’ll need to write just a little bit of custom code to use an Algorithmia Algorithm to receive Webhook notifications.
NodeJS / Google Cloud Function example
Here’s a simple Google Cloud Function example that can be easily adapted to any Node server or other cloud function service. Just replace YOUR_API_KEY with your own key, and use the algorithm of your choice instead of demo/Hello
. You can then parse fields out of the incoming request
object, grabbing any relevant info from the service calling the webhook, and passing it on to the downstream Algorithm.
Note that if your Algorithm does anything privileged, you’ll want to validate any authentication passed by the webhook caller. For example, if it was being triggered by a secure GitHub call, you’d check the X-Hub_signature field of the request header.
Python / AWS Lambda example
If Amazon is your preference, head over to the AWS Lambda Console and click “create function” and “author from scratch”. After filling in a function name, pick Python 3.7 as your runtime, then click “Create” and paste the following code:
This sample accepts an S3 event; if that’s not what you’re triggering it with, change the data
line to extract the correct input from the event.
Set your own api_key
from your account, and change algorithm
to be the algorithm you wish to call.
You can adapt this same code to any other Python environment by changing the line from botocore.vendored import requests
to just import requests
, and adapting the lambda_handler
method to match the request signature your framework requires.