If the service you are connecting to n8n does not have a pre-built connection or doesn’t quite do what you want it to do, you have the option to send data to your n8n instance using something called a webhook.
What is a Webhook?
A webhook is simply a URL that an service can post data to. Many online services that you subscribe to will have this option. They normally ask for a URL to post to, and once you provide that, every time something happens on that service, it will post the information to the URL you provided.
How to Setup a Webhook in n8n
Webhooks are triggers in an n8n workflow. To get started, you will need to select the ‘On a Webhook Call’ in the triggers section of your workflow.
Once you have the Webhook trigger node in your flow, you then need to set some parameters.
The HTTP Method will need to be selected. The service you are adding your webhook to will tell you the right method. This may be Post, Put, Get etc. In most cases, this will be Post. If they don’t provide it, it will definitely be post. The option in the dropdown is telling n8n what type of request to expect.
The path option gives the webhook it’s final URL (like an identity). You can use the one that is pre-created, or you can change it to something more memorable. A word of caution here – the url needs to be unique, so make sure that you are not using a path that you have used already in another workflow.
Once you have set the path, you can click onto either the test or production URL to copy it and add that the post or webhook url in your service.
Now, when that URL is called, the data the service sends will be posted to your webhook URL.
Securing Your Webhooks
If you leave the Authentication dropdown set to none – anyone who has your webhook URL can post data to it. You can stop that from happening by adding Authentication to your webhook.
n8n provides 3 different methods of authentication:
- Basic – here you will create a username and password
- Header – here you will add a parameter and value to a webhook header
- JWT Auth – here you will create an encrypted token
The video above shows an example of how each of these work and how to use them.
The video shows what to do if you want to use basic auth, but your service provider only accepts header auth. You can click copy below to paste the Code Node into your workflow. Click Copy, head to your n8n workflow canvas then ctrl or cmd and V to paste into your canvas.
Replace <USERNAME> with your username and <PASSWORD> with your password once you have the node pasted into your canvas.
{
"nodes": [
{
"parameters": {
"jsCode": "const username = '<USERNAME>';\nconst password = '<PASSWORD>';\n\n// Combine username and password with a colon\nconst authString = `${username}:${password}`;\n\n// Encode the string in Base64\nconst base64Encoded = Buffer.from(authString).toString('base64');\n\n// Return the Base64-encoded string for the Authorization header\nreturn [\n {\n json: {\n authorizationHeader: `Basic ${base64Encoded}`,\n },\n },\n];\n"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-60,
-40
],
"id": "6802b7b7-2e5e-4fcc-a5cf-149c34e58482",
"name": "Code"
}
],
"connections": {},
"pinData": {}
}
Testing Your Webhook
It’s always best to use the test option and run a number of tests on your webhook so you can see how the data is received. Once you understand the structure, you can then begin to use the data you received in other nodes inside of your workflow.
Set to Active
Once you are happy with your workflow, make sure you use the production URL in your service and and also set the node to active.