If you’re building event-driven applications, you’ve probably considered building them in a Serverless platform. It’s really well suited for having small functions, running when there’s an incoming event, processing the data, and then going dormant. Instead of building your own event loop that sits idle in between events, you’re offloading all that logic to a serverless provider. Being a lazy developer, I’m a fan of writing as little code as possible 😅.

It won’t come as a shock that most of my webhooks are running on serverless instead of on my local machine, or on a VM I have to maintain somewhere. We’ve recently been using Netlify here at Fidel for our documentation previews, so I thought I’d give Netlify Functions a try. In this blog post, I’m going to use them with JavaScript to receive webhook events from an API.

What Are Webhooks?

Before we begin, let’s do a little refresher on what webhooks really are. If you think about APIs as black boxes that allow you to call them up once in a while, webhooks are the mechanism they use to call you back when they’ve got stuff to tell you. When you want to ask an API to do things for your application, you make an HTTP request to the API endpoint, and send some data. When an API wants to send some data to your application, without you having to ask for it every second (also known as polling), they make an HTTP request to your webhook URL.

Now that we know what webhooks are, let’s look at a few things you’ll need to follow along this journey of receiving webhook events with Netlify Functions and JavaScript.

Prerequisites

Creating a Netlify Function

Set Up Netlify CLI

I’m going to use the Netlify CLI to create and deploy a Function. In case you don’t have it installed, now would be a good time to do so.

npm install netlify-cli -g

After you’ve installed the CLI, you’ll also need to authenticate it with your Netlify account.

netlify login

This will open a browser window, asking for permission to access Netlify on your behalf. Go ahead and authorize that.

We’ll also need to create a netlify.toml file in the root of your project folder, and we’ll enable functions there.

[build]
  functions = "functions"
  publish = "dist"

#webhooks #programming #api #serverless #javascript

How to Receive Webhook Events With Netlify Functions and JavaScript
4.00 GEEK