Learn webhooks in under an hour with Node, IFTTT and Twitter
Tags: tutorial, programming, javascript, webdev
Published: 2020-11-17 10:41:00

*Learn how webhooks work and put together a simple Webhook integration with Node, IFTTT and Twitter in under an hour.*

In this guide, you'll learn what webhooks are and how they work. You'll then put together a simple Webhook integration using for IFTTT and Twitter, using Node and a simple Express app.

### What are webhooks?
Webhooks are like APIs in reverse. With an API, you make requests to an API provider. Webhooks reverse this flow.

Instead of you making a request to the API provider, the Webhook provider makes a request to you. Your code receives the request, then does something with it. Examples of webhook requests would be Stripe sending you a notification you about a new payment or IFTTT sending you a notification of a new tweet from your Twitter account.

This diagram is a quick high level overview of how webhooks work
![What are webhooks](https://dev-to-uploads.s3.amazonaws.com/i/xtn0qlbvh6fgb67zgbu9.png)

### Get the code and run the app
We'll be using node and JavaScript for this integration. Before you begin, I suggest downloading the code from [Github](https://github.com/cipher-code/express-api-webhook-example).

If you haven't got them already, install Git, NodeJS and NPM (which is packaged with NodeJS).

Clone the source `git clone https://github.com/cipher-code/express-api-webhook-example.git`
Then run `npm install` to set up the project.

Open up `app.js`. It should look something like this:
```js
const express = require('express');
const app = express();
const port = 3000;
const bodyParser = require('body-parser');
app.use(bodyParser.json());

app.post('/tweeted', (request, response) => {
    const { body } = request;
    console.log(body.tweet);
});

app.listen(port, () => {
    console.log(`Express api/webhook app listening at http://localhost:${port}`);
});
```

This is a very basic express.js application with a single `/tweeted` endpoint which will take tweet text from the request and log it to the console. Its basically the express.js hello world example but with a different endpoint.

The top parts of the code where you see all of the `require()` statements are importing express and setting up the dependencies.

This part of the code allows express to parse requests in JSON format. We are going to configure IFTTT to send us requests in this format.
```js
const bodyParser = require('body-parser');
app.use(bodyParser.json());
```

Down the bottom, `app.listen()` starts node's internal web server, which will listen at `http://localhost:3000`.

Run `npm start` to start the app and you'll see output like this:
![app started](https://dev-to-uploads.s3.amazonaws.com/i/6xrnpl2isedc8xlcyxgu.png)

### Configuring IFTTT
Head over to [IFTTT](https://ifttt.com). If you don't yet have an account, sign up.

Log in and click create in the top right hand corner
![ifttt create](https://dev-to-uploads.s3.amazonaws.com/i/abulmbvbpkxi35wvgz6j.png)

Then next to if this, click Add
![if this](https://dev-to-uploads.s3.amazonaws.com/i/hh5g233jvghoujeowodf.png)

Search for twitter, then click on Twitter
![click twitter](https://dev-to-uploads.s3.amazonaws.com/i/cmnv5vx7oo4hpf98ld9l.png)

Click "New tweet by you"
![click new tweet by you](https://dev-to-uploads.s3.amazonaws.com/i/3qic9wlcrqnwy4yyuett.png)

Click "Create Trigger"

![create trigger](https://dev-to-uploads.s3.amazonaws.com/i/7pe731qxyt4yaq03g41x.png)

Next to then that, click Add
![then that](https://dev-to-uploads.s3.amazonaws.com/i/y033avmknm40bpqafpme.png)

Search for webhooks, then click Webhooks
![click webhooks](https://dev-to-uploads.s3.amazonaws.com/i/l0dqefz4zty3cm5tfv0g.png)

Click make a web request
![click make a web request](https://dev-to-uploads.s3.amazonaws.com/i/in4v6nzs8y2c5kw2hlim.png)

You'll now see a form like this
![empty ifttt form](https://dev-to-uploads.s3.amazonaws.com/i/sputq67tju8rzh3rblhy.png)

You can see that there is a field for URL. This is where IFTTT will send the webhook request to.

You may remember that when you started the app, it gave you a URL of `http://localhost:3000`. If you are new to webhooks, you might think you could enter in `http://localhost:3000/tweeted` into this URL field.

However, this will not work. This is because your `localhost` is only visible to your machine. IFTTT can't see it. You need to have your app running with a Public URL. You could deploy your code to a remote server and get a public URL that way, but that would take alot of time to set up and configure and you want to learn webhooks in less than an hour, right?.

To get a public URL quickly, we can use a simple tool I've built called [expose.sh](https://expose.sh?utm_source=dev.to&utm_campaign=learnWebhooksInUnderAnHour) which will create a public url that will forward requests via a tunnel to your local machine. Head over to [https://expose.sh/install](https://expose.sh/install?utm_source=dev.to&utm_campaign=learnWebhooksInUnderAnHour) and follow the instructions for your operating system. Copy and paste the appropriate code shown into the terminal for Linux/Mac OS X or download the zipped executable for Windows.

The app is running on port `3000`. To get a public URL you need to run `expose 3000` (or `expose` followed by the port number your app is running on).

Expose.sh will then generate a public URL for you
![expose.sh running](https://dev-to-uploads.s3.amazonaws.com/i/21f8trp1vns3ktkyuvdw.png)

Using the URL starting with `https://`, enter in the URL followed by `/tweeted` into the URL field

Select `POST` for the Method.

For Content Type, select `application/json`.

In the Body field, copy and paste this text:
```js
{"tweet": "<<<{{UserName}}>>> tweeted <<<{{Text}}>>>"}
```

The filled form should look something the image below. Click "Create Action" at the bottom once you've verified all the settings are entered in. Make sure they are all correct, especially the Method (`POST`) and Content Type (`application/json`). Otherwise the app may ignore IFTTT's webhook requests.
![ifttt webhook form filled](https://dev-to-uploads.s3.amazonaws.com/i/9f0jws4wb6yhtnk2wmad.png)

On the next screen, click "Finish"
![ifttt finish](https://dev-to-uploads.s3.amazonaws.com/i/5mz8t3a1zr5eim9u214w.png)

Now click "Settings" to view the settings of your new IFTTT applet. Keep the settings page open in a tab, you'll come back to it later
![click settings](https://dev-to-uploads.s3.amazonaws.com/i/5sz6zg59izsz5lmuix8w.png)

Then make a tweet
![tweet](https://dev-to-uploads.s3.amazonaws.com/i/22gulwk24icwuyqbqpba.png)

Normally IFTTT can take up to an hour to poll for new tweets, but were going to give it a kick since we don't want to wait for that long. Go back to the settings page you opened in a tab earlier, then click "Check Now". This will trigger IFTTT to check for new tweets and send a webhook notification to your application with the new tweet you just made.
![click check now](https://dev-to-uploads.s3.amazonaws.com/i/6dzthn5sdjl6jghsbfot.png)

If you've followed everything correctly, you should now see the tweet logged to the console
![tweet logged to console](https://dev-to-uploads.s3.amazonaws.com/i/0cnwkowntq8l6mncvhfn.png)

Now you're all set 😀.

### Recap: Key points
- Webhooks are requests sent by a webhook provider that are consumed by your application
- They are like APIs in reverse. With an API provider, you send a request to them. With Webhook provider, they send a request to you
- You need a public URL for your application to test webhook integrations end to end. There are a few tools out there that can give you one but I suggest [expose.sh](https://expose.sh?utm_source=dev.to&utm_campaign=learnWebhooksInUnderAnHour) because the syntax is easier to work with than other tools where you need to specify a protocol and port.

### Conclusion
Now that you've set up your first webhook integration, you could expand on this further. The IFTTT Twitter integration supports sending notifications for things like retweets of your tweets and other useful stuff. There are also thousands of IFTTT triggers you can plug into "If This" for everything from weather information to Facebook. You could set your smart bulb to switch on at dusk, make your smart sprinkler water your lawn based when it hasn't rained and lots of other cool stuff.


### Video guide
Here is the video version of this guide, with a working demo: https://www.youtube.com/watch?v=gbNK1a7e4ng