n8n Tutorial #4: Understanding Webhooks & API Calls

Ready to learn about webhooks and API calls?

In video 4 of the n8n tutorial series, Ben Young teaches a crucial lesson about webhooks and API calls using the OpenWeatherMap API as an example.

Watch the video, then for more details, see the steps explained below.

n8n Tutorial #4 Explained

In this expanded article, we dive deeper into creating a customized API with n8n. Our goal is to make a unique message based on the weather in any city. We’ll use tools like webhooks, the OpenWeatherMap API, and If or Switch nodes. n8n makes this process easy, even for beginners in automation. Note: n8n requires Node.js 16 or higher and can be hosted via Docker or on-prem, providing flexibility for different setups.

We have extensive experience in automation, helping various industries streamline their workflows. This guide builds on previous lessons, introducing the n8n interface and basic components. By the end, you can automate tasks and create APIs for your projects.

Key Takeaways

  • We will create a customized API that outputs weather updates for any city.
  • We will demonstrate the process of setting up nodes and webhooks in n8n.
  • We will walk through troubleshooting methods to ensure everything runs without issues.
  • n8n supports flexible hosting options, including local, Docker, and various cloud providers.

Getting Started with n8n

Using an n8n Template

We’ll use an n8n template to make creating workflows easier. This is great for beginners or if you want to implement a specific use case quickly. Start by opening the templates section in n8n. Search for “City weather” to find relevant workflows. Choose the template for gathering weather information for any city.

After selecting the template, choose to import and use it in your n8n instance. This template is also compatible with n8n’s Docker-based deployments, allowing a quick start in most environments.

This template uses the OpenWeatherMap API, which needs an API key. Each OpenWeatherMap account gets up to 1,000 API calls daily for free. To get an API key, go to openweathermap.org, sign up, and log in. Then, find the “API keys” section and create a new key.

Return to your workflow in n8n, click “create new OpenWeatherMap credential,” paste your key, and save your configuration.

Setting Up OpenWeatherMap API

In this workflow, three central nodes are used. The first is the webhook, which starts our workflow when a unique web URL is accessed. Double-clicking the webhook node shows the URL you can use to trigger the workflow. This URL has two versions: a test URL and a production URL.

The next node is the OpenWeatherMap node. Make sure the correct API key is selected before proceeding. You can adjust parameters in this node, like getting the current weather or a 5-day forecast. This tutorial focuses on the current weather information. Set the format to metric units and configure the node to accept a dynamic city name.

After verifying the test webhook is operational, click the “test workflow” button to run the entire workflow. This action checks if each connected node is behaving as anticipated.

Common Issues with OpenWeatherMap Node

If you encounter problems, check the details you provide to the OpenWeatherMap node. Forgetting to include the city name often leads to a bad request error. Ensure all necessary fields, like the city name, are correct to get the weather data you need.

Working with n8n Webhooks

Test and Production URLs

n8n webhooks start workflows when a specific URL is visited. Each webhook has its own URL. Knowing the difference between test and production URLs is key when setting up your workflow.

The test URL ends with “-test.” It’s for testing, letting you tweak your workflow without affecting the live version. Once everything works as expected, switch to the production URL. This URL is for live use and doesn’t have the “-test” suffix.

Try the test URL in a browser to see your workflow in action. This helps you test your logic. For live use, you’ll need the standard URL. This approach makes your workflow safer and more reliable.

If your webhook doesn’t work during testing, check for missing details like the city name. Tailor your inputs to meet the node’s needs to avoid common mistakes.

Configuring the OpenWeatherMap Node

Selecting an API Key

You need an API key to use the OpenWeatherMap API. OpenWeatherMap offers free accounts for thousands of daily calls, perfect for most users.

  1. Start at openweathermap.org.
  2. Click “Create an Account” if you haven’t already.
  3. Enter your details: username, email, and password.
  4. Accept the terms and confirm your age.
  5. Log in and find “API Keys” in the top menu.
  6. Create an API key and name it something you’ll remember.
  7. Copy the key and add it to your n8n workflow. Paste it into the field and save. This connects your workflow to your OpenWeatherMap account. Remember, advanced features might need a paid subscription.

Weather Options and Parameters

In your n8n workflow, you’ll see three nodes. The OpenWeatherMap node gets weather data. You can customize how you get this information:

  • API Key Selection: Make sure you’ve picked the correct API key.
  • Weather Type: You can ask for current conditions and forecasts. We’re using “current weather.”
  • Units: Choose between metric, imperial, or standard. We’re using metrics.
  • City Selection: Use a dynamic value from the webhook to specify the city.

Each n8n node has a settings tab for customizing data handling. After setting up your nodes, check the webhook node to ensure everything is correct.

When testing, give the node something to work with, like a city name. For example, use ?city=Oslo for Oslo’s weather.

Working with Data Using the Set Node

The Set Node, or “edit fields,” is great for focusing on specific data. It lets you rename or extract fields easily without digging through JSON structures.

Key Values to Extract

At a minimum, you’ll likely want the following details:

  • Temperature – Reflecting the current metric value (in Celsius if you’ve chosen metric units).
  • Humidity – Showcases the moisture level in the air.
  • Wind Speed – Useful for activity planning or providing wind-based alerts.
  • Weather Description – A human-readable output like “light rain” or “clear sky.”
  • City Name – Confirms the location for which data is retrieved.

You can also extract additional attributes, such as feels_like temperature or wind gust data if needed.

By isolating these values, you can easily forward them to the next step in your workflow. You could store them in a database, send them via email, or use them for logic checks.

Testing the Workflow

With your data extraction setup, you’ll want to run a full test to verify everything. Locate the test workflow button near the bottom of the n8n canvas. Clicking this shifts the system into listening mode, waiting for the trigger event—in this case, the webhook call.

Troubleshooting Issues

Something is missing or incorrect if your test produces a bad request or an error. Common issues include forgetting to specify the city parameter or referencing an incorrect field name. Make sure you correctly pass the city name through the webhook. Add?city=Toronto or a relevant parameter to your test URL if needed.

By iterating through these checks, you’ll become adept at using n8n to capture and manipulate data in real-time.

Testing the Workflow

Starting the Test Workflow

We’ll revisit the n8n template to get going quickly. Look for “City weather” in the templates section and select the workflow that retrieves weather data for any specified city. Once imported, you’ll note that the OpenWeatherMap node in the workflow references an API key. Secure this key from the openweathermap.org site by making an account if you haven’t already.

Copy the generated key and return it to n8n. Create a new credential for OpenWeatherMap, paste the key into the designated field, and click save. You can then direct your attention to the webhook node, the engine that starts the entire chain by capturing external requests.

Troubleshooting Errors

If you run into issues during testing, double-check the URL you’re using. The test URL is prone to misconfiguration if you have a custom domain or your n8n instance is behind a proxy. A 404 error often indicates the URL doesn’t match the domain you’ve configured. Also, confirm that you’ve toggled n8n into “Listening” mode before you try to trigger the webhook from your browser.

Sometimes, the problem lies in the OpenWeatherMap node if the city parameter is absent or incorrectly mapped. You’ll see a “bad request” error in that case, signaling that the city detail is missing. Solve this by assigning a variable that references the correct query parameter in your URL so that the workflow obtains a valid city name.

Executing and Troubleshooting the Final Workflow

When you’re satisfied with your initial setup, consider running a quick final test to ensure that all aspects—like credentials, webhook URL, and node configurations—function properly.

Webhook Node

In this workflow, the first node is a webhook. By double-clicking it, you’ll see two URLs. One is a test URL suitable for the workflow’s trial runs, and the other is a production URL for live operations. The test URL typically includes a “-test” segment in its path. If you are self-hosting and using HTTPS, ensure your n8n environment variables reflect the correct domain.

OpenWeatherMap Node

After the webhook, we find the OpenWeatherMap node. It uses your API key. You can change settings to get the weather data you need.

Change the settings if you want a daily forecast instead of current weather.

Testing the Webhook

Open a browser and enter the test URL for your webhook. Add ?city=Seoul to specify the city. Press Enter.

You should see n8n detect the request. If it works, n8n will get the weather data for Seoul.

If you get a 404 error, check your domain settings. If n8n uses a non-standard port, make sure the port is open.

Set Node

The Set Node is near the end of the workflow. It cleans and formats the weather data. For example, it can rename “temp” to “Temperature (Celsius)”.

It makes the data easy to use in other applications.

Testing the Entire Workflow

Click “Test workflow” to start n8n waiting for the webhook. Go back to your test URL to trigger it. If it works, you’ll see success in n8n.

This means the city was sent, the API call was made, and the Set Node worked.

Check your query string and the OpenWeatherMap node to see if you still get an error. Make sure everything matches.

Troubleshooting

Watch how each node works together. Misconfigurations can cause problems. Check everything carefully.

Ensure the city name is identical in the webhook, node references, and query string.

When everything matches, you’ll have a working workflow. This shows n8n’s power in automating tasks.

Tutorial Series

FAQ

1. What is the purpose of using n8n with OpenWeatherMap?

OpenWeatherMap helps explain n8n’s power. It automates API calls and gets weather data, making it easy to integrate with other services.

2. How do webhooks work in n8n?

Webhooks in n8n start workflows when a URL is accessed. They pass data through the workflow.

3. What do I do if my webhook returns a 404 error?

A 404 error means the URL is wrong or the workflow isn’t active. Check your domain settings and make sure the workflow is running.

4. How can I ensure my API key is secure in n8n?

Use n8n’s credential manager to store API keys safely. Don’t put them in workflow nodes.

5. How do I schedule weather data retrieval?

Use the Cron node in n8n to schedule requests. You can set it to run every hour or daily.

6. Can I customize the weather data output?

Yes, you can use Set nodes to filter and format data. If/Switch nodes can also trigger actions based on weather conditions.

7. What should I check if my OpenWeatherMap node shows a ‘Bad Request’ error?

Check that all required parameters, like the city name, are correctly set in the node and passed through the webhook.

8. Can I use n8n for other API integrations?

Yes, n8n supports hundreds of integrations. You can connect APIs like Slack, Google Sheets, and email services, including OpenWeatherMap.

9. How can I test my workflow before deploying it?

Use the test webhook URL to check your workflow. Before you go live, manually trigger it with test parameters to ensure all nodes work.

10. Where can I find more learning resources for n8n?

Check out the official n8n documentation and community forums. You can also find video tutorials. They’re great for learning advanced automation and best practices.

Leave a Reply