Advanced n8n Workflow Engineering
Advanced HTTP API Mastery
Beyond Simple GET Requests
You're already familiar with making GET requests to fetch data. But to build powerful workflows, you need to manipulate data, not just read it. This involves using other HTTP methods.
POST creates new resources, while PUT and PATCH are for updates. The main difference is that PUT typically replaces an entire resource, whereas PATCH applies a partial update. This makes PATCH more efficient when you only need to change a few fields.
The DELETE method does exactly what its name implies: it removes a specified resource. Using these methods allows your workflows to perform a full range of Create, Read, Update, and Delete (CRUD) operations.
/*
Example of a PATCH request body to update a user's email.
This only sends the field that needs changing, not the whole user object.
*/
{
"email": "new.email@example.com"
}
Mastering Authentication
Most useful APIs require authentication. While simple API keys are common, professional integrations often use more complex schemes like Bearer tokens or OAuth2.
Bearer Tokens are security tokens sent in an HTTP header. The server receives the request, checks the token for validity, and grants access. You can add one by creating an Authorization header with the value Bearer YOUR_TOKEN.
The HTTP Request node often has built-in authentication helpers, but understanding how to set the headers manually is crucial for APIs with non-standard requirements.
OAuth2 is a more involved authorization framework. It allows a user to grant your application limited access to their data on another service without sharing their credentials. The process involves a multi-step handshake of redirects and token exchanges. While many automation tools have dedicated credential types for OAuth2, you might occasionally need to perform this flow manually using a series of HTTP Request nodes to fetch the access token before making your final API call.
Dynamic and Complex Requests
Hardcoding values is fine for testing, but real workflows need to handle dynamic data. You can use expressions to pull data from previous steps into your request's URL, headers, query parameters, or body.
For POST or PATCH requests, you'll often send data in the request body, usually as JSON. You can construct this JSON object dynamically.
// Constructing a dynamic JSON body using expressions
{
"name": "{{ $json.customerName }}",
"order_id": "{{ $json.orderId }}",
"items": [
{
"product_id": "{{ $json.productId }}",
"quantity": 2
}
]
}
A huge time-saver is the Import cURL feature. Most API documentation provides example requests as cURL commands. Instead of manually configuring every header, parameter, and option, you can paste the entire cURL command, and the node will parse it and set everything up for you. This is the fastest way to go from API docs to a working request.
# A typical cURL command from API documentation
curl -X POST 'https://api.example.com/v1/users' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"name": "Ada Lovelace", "email": "ada@example.com"}'
Handling All Kinds of Responses
APIs don't always return neat JSON objects. Sometimes you'll get a PDF, an image, or just plain text. You can configure the Response Format option to handle these different content types. Setting it to 'File' will let you process binary data, while 'String' will handle raw text.
By default, a failed request (any 4xx or 5xx status code) will stop your workflow. However, you can toggle an option like 'Continue On Fail' to allow the workflow to proceed. This is essential for building robust logic that can handle expected errors, like a 404 Not Found response when checking if a resource exists.
To properly handle these cases, you need more than just the response body. By enabling the Full Response option, the node's output will include the status code, status message, and all response headers. This gives you the context needed to build conditional logic. For example, you can use an IF node to check if statusCode is 200 before proceeding.
When updating a resource via an API, what is the main difference between using the PUT and PATCH methods?
How do you typically include a Bearer Token for authentication in an HTTP request?
With these advanced techniques, you can reliably connect to almost any API and build workflows that are both powerful and resilient.