Connect n8n With APIs: A Beginner-Friendly Example

Posted November 23, 2025 by Karol Polakowski

n8n is a powerful, open-source workflow automation tool that lets developers orchestrate API calls, transform data, and build integrations without writing full-blown integration services. This tutorial walks through a simple, practical example: accept an incoming webhook, call a third-party REST API, and return a combined result — all in n8n.

What you’ll build

We’ll create a workflow that:

  • Exposes a webhook to receive a JSON payload (for example, {“city”:”London”}).
  • Calls a public weather API using the city name.
  • Transforms and returns a compact JSON response to the caller.

This pattern generalizes to many use cases: form processing, webhook enrichment, service orchestration, or building lightweight API facades.

Prerequisites

  • n8n Cloud account or a self-hosted n8n instance (Docker or binary).
  • A public webhook target (n8n cloud provides one; for local dev, use ngrok).
  • An API key for the target API (optional — some public APIs allow free requests).

Step-by-step: Create the workflow

  1. Create a new workflow in n8n.
  2. Add a Webhook node. Set HTTP Method to POST and note the generated URL (or expose your instance via ngrok).
  3. Add an HTTP Request node to call the external API.
  4. Add a Function node to shape the final response.
  5. Connect nodes: Webhook -> HTTP Request -> Function -> Webhook (respond).

Webhook node configuration

  • Resource: Webhook
  • HTTP Method: POST
  • Response Mode: On Received (or ‘Last Node’ if you want to return the Function node output)

HTTP Request node example

Use the HTTP Request node to call a weather API like OpenWeatherMap (or any API). Use expressions to insert the incoming city value: {{ $json[“body”][“city”] }}. Set authentication as needed (API Key header or query string).

Function node (transform the API response)

Below is a concise Function node script that expects the HTTP Request node output (first item) and the original webhook body to produce a compact result.

// Function node
const incoming = $json["body"] || {};
const apiData = items[0].json || {};
// Example fields depend on API; adjust accordingly
const temp = apiData.main && apiData.main.temp;
const desc = apiData.weather && apiData.weather[0] && apiData.weather[0].description;
return [{ json: {
  city: incoming.city,
  temperature: temp,
  description: desc,
  fetchedAt: new Date().toISOString()
}}];

Test with curl (trigger the webhook)

Replace with the Webhook node URL.

curl -X POST '<WEBHOOK_URL>' \
  -H 'Content-Type: application/json' \
  -d '{"city":"London"}'

You should receive a JSON response with the combined data.

Authentication patterns

  • API Key: store keys in n8n Credentials (HTTP Basic Auth, API Key, or Header). Never hard-code secrets in Function nodes.
  • OAuth2: n8n has built-in OAuth2 support for many services. Use the OAuth2 credential type and authorize once via the UI.
  • JWT / Bearer tokens: use the HTTP Request node headers with expressions and credential variables.

Handling pagination and rate limits

  • Pagination: loop with the HTTP Request node using the ‘Continue on Fail’ + ‘SplitInBatches’ or use the ‘Enable Pagination’ option if supported by the node.
  • Rate limits: implement retries with exponential backoff using the ‘Wait’ node and conditional logic; use ‘Continue on Fail’ carefully to avoid silent failures.

Error handling and observability

  • Add an Error Trigger node to capture failed executions and route them (email, Slack, or database).
  • Use ‘Set’ nodes to add metadata (request id, timestamps) to payloads for tracing.
  • Enable execution logs (self-hosted) or rely on n8n Cloud’s execution history for debugging.

Best practices

  • Use credentials to store secrets and restrict UI access in production.
  • Keep Function node code minimal; prefer native nodes for transformations (Set, Merge, Item Lists) where possible.
  • Validate incoming webhook payloads at the earliest node; respond with clear error messages and HTTP status codes.
  • Use environment variables for non-sensitive configuration and credentials for secrets.
  • Version control workflows by exporting JSON or using n8n’s workflows GitHub integration where available.

When to move beyond n8n

n8n is excellent for orchestration and integrations, but consider a dedicated service or microservice when you need:

  • High-frequency, low-latency workloads where single-threaded flow could be a bottleneck.
  • Complex long-running transactions with ACID requirements.
  • Heavy custom business logic that’s better expressed in a typed language and deployed in a CI/CD pipeline.

Summary

n8n lets developers rapidly prototype API-driven workflows. With a webhook trigger, an HTTP Request node, and a bit of JavaScript transformation, you can create a secure, maintainable integration in minutes. Follow credential best practices, handle pagination and rate limits, and use n8n’s execution logging to keep workflows reliable as you scale.