Webhook Node — The Versatile Toolbox 🧰

Amudhan
Amudhan
Nov 18, 2020 · 16 min read

Webhooks listen for a request and receive data when an event occurs. This reminds me of myself as a kid, waiting for the school bell to ring. When the bell rang, my workflow in life was to run home for a snack! Webhooks work in a similar manner; they are triggered by events — making webhooks an excellent way of receiving and returning data, as they are very efficient and fast, eliminating the need for maintaining a client state (such as a timestamp, for example) that would be necessitated in a polling-based setup.

The Webhook node is a versatile and powerful node in the n8n ecosystem. It receives HTTP Requests, instead of sending them. This makes the node useful — you guessed it, to receive data from any other service that supports Webhooks.

Image for post
Image for post
Webhook Node — The Versatile Toolbox

The Webhook node is a Trigger node, which means that it serves as the starting point for an n8n workflow. This allows several different services to connect to n8n and run a workflow when data is received. We can use Webhook nodes in workflows where we want to receive data and run a workflow based on the data. The Webhook node also supports returning the data generated at the end of a workflow. This makes it very useful to build a workflow to process data and return the results, for example.

In this article, we are going to explore three examples and dive into how the Webhook node can be used in your workflows to trigger them and help automate tasks:

  1. We will take a look at how to receive and return data via a Webhook node by getting information about the weather for any city.
  2. We will determine how to use authentication in the Webhook node by storing and sending information about the weather for any city via SMS.
  3. We will learn how to receive files and binary data using the Webhook node by detecting, storing, and returning the information about a purchase from the image of a receipt.

Getting Ready

This tutorial assumes that you have n8n up and running. In case you don’t, you can get started by following the instructions on the Quickstart page.

Authentication: The Webhook node supports two methods of authenticating a request that it receives.

  • Header AuthA method of authentication where the specified header parameter must be passed along with the request. This method can be used when you want to authenticate using an API key or an access token, for example.
  • Basic AuthA method of authentication where the specified username and password must be passed along with the request.

HTTP Method: The Webhook node supports receiving two types of HTTP Requests.

  • GET RequestGET requests are typically used to request data from a resource. This type of request is typically used to retrieve data from a service.
  • POST RequestPOST requests are typically used to send data to a resource for a create/update operation. This type of request is typically used to send data to a service.

Path: By default, this field contains a randomly generated webhook URL path, to avoid conflicts with other webhook nodes.

  • You can manually specify a URL path if necessary. A good example would be if you were using n8n to prototype an API; and wanted consistent endpoint URLs.

Response Code: Allows you to customize the HTTP response code that the Webhook node will return upon successful execution.

Response Mode: This dropdown list allows you to select between two response modes.

  • On Received — When this option is selected, the Webhook node will return the specified response code along with the message “Workflow got started.”.
  • Last Node — When this option is selected, the Webhook node will return the specified response code along with the data output from the last node executed in the workflow.

Optional Parameters: The Webhook node also supports several optional methods that can be used during configuration.

  • Response HeadersThis option allows you to specify additional headers in the Webhook response.
  • Raw Body — This option is used to specify when the Webhook node will receive data in a RAW format, such as JSON or XML.
  • Binary Data — This option is available only when the Webhook node is set to receive POST requests. Setting this to ‘true’ lets the Webhook node know that it will receive binary data (such as an image/audio). You can use this option when you expect to receive a file via your Webhook node.

Conditional Parameters: The Webhook node also supports several other parameters, that are used only in certain configurations.

  • Response Data — This option is available only when the Response Mode is set to ‘Last Node’. It allows you to choose which data to return.
  1. All Entries — Choose this option to return all the data generated by the last node in the workflow, as an array.
  2. First Entry JSON — Choose this option to return the first data entry of the last node in the workflow, as a JSON object.
  3. First Entry Binary — Choose this option to return the binary data of the first entry of the last node in the workflow, as a binary file.

We will be using cURL to send requests to our Webhook nodes throughout this tutorial. If you do not already have cURL, you can find instructions to install and use cURL in this guide.

In this article, we will connect to Webhook based workflows via cURL requests. However, a far more interesting proposition would be to use the HTTP Request in node to connect to the Webhook node and send requests.

You could build several of these Webhook based workflows to support a main workflow, powered by HTTP Request nodes. As a starting point, here’s a video that demonstrates how you can send a request to your Webhook based workflows using the HTTP Request node.

Demonstration of how to send request to Webhook based workflows using the HTTP Request node

A quick note about Webhooks in n8n

Does not apply to n8n.cloud users, feel free to skip this section.

To be able to use webhooks, n8n has to be reachable from the web. To make that easy, n8n has a tunnel service, which redirects requests from our servers to your local n8n instance, in case you do not have it self-hosted on a server.

If you choose to not use the tunnel service, keep in mind that your cURL requests must be sent from the machine that n8n is running on.

To enable and use this tunnel service, simply start n8n with the --tunnel option.

Please note that the tunnel is for testing purposes only and should not be used in production.

If you installed n8n with npm, this would be:

If you’re running n8n in Docker, this would be:

Every Webhook node in n8n has two types of URLs — Test and Production. The test URLs are for use when building the workflow, it listens for 120 seconds after the Execute node button is clicked and displays the received data directly in the Editor UI, making it useful to sample your data during the build process.

The production URL listens continuously once the workflow is activated and does not display the data received in the Editor UI, but instead executes the workflow directly. You can review the data generated during production executions by clicking on the ‘Executions’ tab in the left sidebar. You can specify which kinds of execution history to save in the ‘Settings’ tab in the left sidebar, under the ‘Workflows’ dropdown list.

For the purposes of this article, we assume that every webhook request uses the test URL; but if you plan on deploying this workflow for regular use, you will have to create the workflow, use the production URL, and activate the workflow.

Here is a GIF of me demonstrating how to retrieve the production and test webhook URLs in n8n:

Image for post
Image for post
Retrieving the Test and Production URLs from the Webhook node in n8n

Workflow 1 — Get information about the weather for any city

For this example, we will be retrieving the information about the weather in a city using the OpenWeatherMap node and returning the temperature, humidity, wind speed and a short description of the weather.

You can also follow along this example by copying the workflow from n8n.io and making appropriate changes at each step.

First, click on the + button on your n8n dashboard, select the Trigger tab and type “Webhook” into the search box and select the Webhook node. Then, select Last Node for Response Mode. This will return the data from the last node of the workflow as the Webhook response. Finally, select All Entries for Response Data. This tells n8n to return all the data generated by the last node of the workflow in the response.

Now let’s save our workflow to register the webhooks, then click the Execute Node button and send your cURL request.

Let’s send it some data using a cURL request, remember to change the URL to your test webhook URL from n8n:

You should see the data in the Webhook node, similar to this:

Image for post
Image for post
Receiving data using the Webhook node

Next, add an OpenWeatherMap node and connect it to the Webhook node. Configure your OpenWeatherMap credentials by following the instructions here. Note that it might take up to two hours for your OpenWeatherMap API key to get activated. Select Metric for Format (unless you live in the US, Liberia or Myanmar 😉). Click on the gears icon next to the City field and use the variable selector to select the city from the Webhook node’s output data for City. You can do this by going to Nodes > Webhook > Output Data > JSON > body > city.

Now, click on the Execute Node button and you should see the weather information retrieved from OpenWeatherMap like this:

Image for post
Image for post
Getting weather data using the OpenWeatherMap node

Finally, add a Set node and connect it to the OpenWeatherMap node. Execute the OpenWeatherMap node to load the data into n8n. Set the Keep Only Set toggle to ‘true’. This tells n8n to only return the data specified in the Set node. Select ‘String’ from the Add Value dropdown list and enter ‘humidity’ as the name; use the variable selector to select the humidity value from the OpenWeatherMap node’s output data for value. Similarly, add entries for temperature, wind speed, description and city.

Now, click on the Execute Node button and you should see the data from the Set node like this:

Image for post
Image for post
Using the Set node to return data

When you’re done, your workflow should look like this:

Image for post
Image for post
A workflow to get information about the weather for any city

Now click on Execute Workflow and test it by sending a cURL request similar to the one above, you should get a response like this:

Here’s a video of me building this workflow, for reference:

Building a workflow to get information about the weather for any city

Workflow 2 — Storing and sending information about the weather for any city via SMS

For this example, we will be receiving a query, saving the details of the query in Airtable, retrieving the information about the weather in a city using the OpenWeatherMap node and sending an SMS of the weather information.

You can also follow along this example by copying the workflow from n8n.io and making appropriate changes at each step.

First, click on the + button on your n8n dashboard, select the Trigger tab and type “Webhook” into the search box and select the Webhook node. Select Header Auth from the Authentication dropdown list. Select/Create new Header Auth credentials in the Credentials section by entering and saving a name and value. Then, select the POST option for HTTP Method. This tells n8n to listen for a HTTP POST request instead of the default GET request. Then, select On Received for Response Mode. Select ‘Raw Body’ from the Add Option dropdown list and set the toggle to ‘true’. This lets n8n know that we are sending RAW data.

Now let’s save our workflow to register the webhooks, then click the Execute Node button and send your cURL request.

Make sure it works by sending it some data using a cURL request, remember to change the URL to your webhook test URL from n8n:

You should see the data in the Webhook node, similar to this:

Image for post
Image for post
Receiving data using the Webhook node

Next, add a Set node and connect it to the Webhook node. Set the Keep Only Set toggle to ‘true’. This will keep only the values you specify in this node, helping save them in an Airtable in the next step. Select ‘String’ from the Add Value dropdown list and enter ‘Name’ as the name; use the variable selector to select the name value from the Webhook node’s output data for value. Similarly, add entries for number, and city.

Now, click on the Execute Node button and you should see the data from the Set node like this:

Image for post
Image for post
Using the Set node to extract only the necessary data

Copy this table to your Airtable account. Add an Airtable node and connect it to the Set node. Configure your Airtable credentials by following the instructions here. Set Append as the Operation. Enter your Airtable Application ID (you can find the app ID by going to https://airtable.com/api and selecting the base) in the Application ID field. Enter “Table 1” in the Table field.

Now, click on the Execute Node button and you should see that the data is saved in Airtable like this:

Image for post
Image for post
Saving the data using the Airtable node

Here’s a picture of the table I used for this workflow:

Image for post
Image for post
The data is saved in Airtable

Next, add an OpenWeatherMap node and connect it to the Airtable node. Configure your OpenWeatherMap credentials by following the instructions here. Click on the gears icon next to the City field and use the variable selector to select the city from the Webhook node’s output data for City.

Now, click on the Execute Node button and you should see the weather information retrieved from OpenWeatherMap like this:

Image for post
Image for post
Getting information about the weather using the OpenWeatherMap node

Finally, add a Twilio node and connect it to the OpenWeatherMap node. Configure your Twilio credentials by following the instructions here. Use the variable selector to select the number field from the Webhook node’s output data for the To field. Similarly, use the variable selector to write a short message about the weather using the information from the OpenWeatherMap node for the Message field.

Now, click on the Execute Node button and you should see that a message is sent using the Twilio node, like this:

Image for post
Image for post
Sending an SMS using the Twilio node

When you’re done, your workflow should look like this:

Image for post
Image for post
Store and send information about the weather for any city via SMS using a Webhook based workflow

Now click on Execute Workflow and test it by sending a cURL request similar to the one above, you should see the data from your request get added to Airtable, and you should receive an SMS on your phone like this:

Image for post
Image for post
SMS received after workflow is executed

Here’s a video of me building this workflow, for reference:

Building a workflow to store and send information about the weather for any city via SMS

Workflow 3 — Detect and store the information about a purchase using the image of a receipt

For this example, we will be receiving an image via the Webhook, and then using the Mindee node to read and extract data about the purchase, saving the data in Airtable and also returning the data.

You can also follow along this example by copying the workflow from n8n.io and making appropriate changes at each step.

First, click on the + button on your n8n dashboard, select the Trigger tab and type “Webhook” into the search box and select the Webhook node. Select Header Auth from the Authentication dropdown list. Select/Create new Header Auth credentials in the Credentials section by entering and saving a name and value. Then, select the POST option for HTTP Method. Then, select Last Node for Response Mode. Select All Entries for Response Data. Select ‘Binary Data’ from the Add Option dropdown list and set the toggle to ‘true’. This lets n8n know that we are sending a file.

Now let’s save our workflow to register the webhook, then click the Execute Node button and send your cURL request.

Make sure it works by sending it some data using a cURL request, remember to change the file path to your image file and the URL to your webhook test URL from n8n:

Notice that this cURL request contains a path to an image file that is our receipt. This is the receipt that I used to test my workflow, although you can use one of your own!

Image for post
Image for post
A sample receipt I used for testing the workflow.

You should see the data in the Webhook node, similar to this:

Image for post
Image for post
Receiving data using the Webhook node

Next, add a Mindee node and connect it to the Webhook node. Configure your Mindee credentials by following the instructions here. Enter ‘receipt’ in the Binary Property field. This tells the Mindee node which field of the Webhook node’s output contains the image file that needs to be processed.

Now, click on the Execute Node button and you should see the extracted information retrieved from Mindee like this:

Image for post
Image for post
Extracting transaction data from the receipt using the Mindee node

Copy this table to your Airtable account. Add an Airtable node and connect it to the Mindee node. Configure your Airtable credentials by following the instructions here. Set Append as the Operation. Enter your Airtable Application ID in the Application ID field. Enter “Receipt” in the Table field. Set the Add All Fields toggle to ‘Off’. Then, click the Add Field button and enter ‘category’ in the newly added Name field. Similarly, create entries for ‘date’, ‘currency’, ‘locale’, ‘merchant’, ‘time’, and ‘total’ under the Fields section.

Now, click on the Execute Node button and you should see that the data is saved in Airtable like this:

Image for post
Image for post
Saving the extracted data using the Airtable node

Here is a picture of the table I used in this workflow:

Image for post
Image for post
The extracted data is saved in Airtable

Finally, add a Set node and connect it to the Airtable node. Set the Keep Only Set toggle to ‘true’. Select ‘String’ from the Add Value dropdown list and enter ‘data’ as the name; use the variable selector to select the fields returned from the Airtable node’s output data for value. Similarly, add a string field with the name ‘message’ and use the variable selector to write a short message about the transaction using the information from the Mindee node.

Now, click on the Execute Node button and you should see the data from the Set node like this:

Image for post
Image for post
Using the Set node to extract the data to be returned and include it in a message

When you’re done, your workflow should look like this:

Image for post
Image for post
Detect and store the information about a purchase using the image of a receipt using a Webhook based workflow

Now click on Execute Workflow and test it by sending a cURL request similar to the one above, you should get a response like this:

Here’s a video of me building this workflow, for reference:

Building a workflow to detect and store the information about a purchase using the image of a receipt

Further exploration

Many online services support Webhooks, and you can use n8n to integrate with any of these platforms using a similar set of steps.

With the Webhook node, you can also use n8n to trigger event-based workflows. This opens up a lot of possibilities as n8n’s open, cross-platform nature allows integration of multiple platforms into a single workflow.

For example, the data extracted from the receipt could be added to Zoho Books or Xero for accounting by adding a node to the workflow. The weather data obtained from OpenWeatherMap could be connected to Twitter to respond to tweets, or Philips Hue, to change the colour of a light according to the outside temperature!

For more cool ideas on how n8n can be used, check out our Medium publication.

Conclusion

In this tutorial, we learned to leverage the Webhook node to get weather data, send weather updates over SMS and read data from the image of a receipt. We also discussed the potential ways the Webhook node can be used in other workflows and how powerful the Webhook node can be in event-based workflows.

Every person has their own set of preferred tools and processes. n8n helps you automate your workflows, regardless of how many different tools you use. We connect every tool with every other tool, making n8n work for you, rather than the other way around, like some others.

What will you automate?

In case you’ve run into an issue while following this tutorial, feel free to reach out to me on Twitter or ask for help on our forum 💙

n8n.io

Turn ideas into reality

Sign up for n8n Newsletter

By n8n.io

🚀 Your monthly update: New integrations | Fresh Tutorials Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store