Skip to main content
Create your own
Lesson illustration

Interacting with n8n via the API Node

Hello! Welcome to your next lesson in the "Modular and Reusable Workflows" module.

In our last session, we focused on building idempotent workflows, a crucial practice for creating robust, production-ready automations that can safely handle duplicate events. You learned the "check-then-act-then-log" pattern and implemented it using both n8n's Data Tables and the Remove Duplicates node.

Today, we're taking the concept of robust automation a step further. Instead of just controlling the logic within a workflow, we're going to learn how to control the workflows themselves. This is a form of "meta-automation"—using n8n to automate n8n.

Your goal of achieving full-stack mastery of n8n requires not just building workflows, but also managing the platform programmatically. This lesson is a significant step in that direction. By the end of our 60 minutes, you will be able to use the n8n API node to programmatically interact with your n8n instance.

1. The n8n API: Your Control Panel

Just like many modern software applications, your n8n instance has its own REST API. This API allows you to perform nearly any action you can do through the user interface, but programmatically. You can create, update, delete, activate, and execute workflows, manage credentials, and fetch execution data, all from within another workflow.

There are two primary ways to interact with this API in n8n:

  1. The HTTP Request node: The general-purpose tool you're familiar with for calling any external API.
  2. The n8n API node: A specialized, user-friendly wrapper built specifically for interacting with the n8n API. It simplifies authentication and provides pre-configured operations.

We will focus on the n8n API node, as it's the most direct way to achieve today's goal, but we'll also look under the hood to see how it relates to the standard HTTP Request node.

2. Authentication: Getting the Keys to the Kingdom

To use the n8n API, you first need to prove you have permission. This is done with an API key.

How to generate your n8n API Key:

  1. In your n8n instance (Cloud or self-hosted), click on the user icon in the top-left corner and go to My settings.
  2. Navigate to the API section.
  3. If you don't have a key, click Add API key. Give it a descriptive name (e.g., "WorkflowManagerKey") and copy the generated key. Treat this key like a password.

Now, let's create a credential in n8n to securely store this key.

  1. In the n8n main menu, go to Credentials and click Add credential.
  2. Search for n8n API and select it.
  3. Give the credential a name, paste your API key into the API Key field, and save it.

You're now ready to use the n8n API node.

3. Core Operations with the n8n API Node

Let's build a simple workflow to explore what the n8n API node can do.

  1. Create a new workflow.
  2. Add an n8n API node.
  3. In the node's parameters, select the credential you just created.

You'll see two main dropdowns: Resource and Operation. Let's try a simple read operation.

  • Set Resource to Workflow.
  • Set Operation to Get All.

Execute the node. The output will be a JSON array containing data for every workflow in your n8n instance. This is a powerful way to audit or manage your workflows programmatically.

The real power, however, comes from write operations. You can create, update, and manage workflows dynamically. The most advanced operation is creating a workflow from scratch, which requires a precise JSON structure.

The following resources from the n8n community provide excellent examples of how this is done.

An automated process...to create process

The community post 'An automated process...to create process' details how to use the n8n API to dynamically build workflows. It clearly outlines the necessary API endpoints and the JSON body structure.

Please read 'Step 1: Enable N8N API Access' and 'Step 4: Use the N8N API to Create and Configure the Workflow'. In Step 4, focus on the JSON structure in the Body. Notice how it defines the workflow's name, its nodes (with their type and parameters), and the connections between them. You don't need to worry about the GPT parts for now.

This second resource is a complete, importable workflow that does exactly what the first article describes.

Create Dynamic Workflows Programmatically via Webhooks & n8n API

The workflow 'Create Dynamic Workflows Programmatically via Webhooks & n8n API' is a practical implementation of the concept. It shows how the nodes are connected and configured.

Review the 'Step-by-Step Details' and the 'Example Webhook Request'. Pay close attention to '4. Create Workflow', which is the n8n API call, and the data-raw section of the cURL command, which shows the full JSON payload needed to define a simple two-node workflow.

As you can see from these resources, defining a workflow programmatically is verbose but gives you ultimate control. For your work in product and team integrations, this could be used to create boilerplate workflows for new clients or projects automatically.

Test your understanding!

You are using the n8n API node with the Workflow resource and Create operation. Your input JSON for the new workflow is missing the connections property, which defines how nodes are linked. What will happen when you execute the node?

Show answer

The API call will succeed, and n8n will create a workflow containing the specified nodes. However, on the canvas, the nodes will appear but will not be connected to each other. The workflow will not be executable as a coherent sequence because the data flow path hasn't been defined. The connections object is crucial for defining the directed graph that makes up a workflow.

4. Practical Example: A Remote "On/Off" Switch for a Workflow

Let's build something practical. We'll create a workflow that acts as a remote control to activate or deactivate another workflow via a simple API call (a webhook).

Part 1: The Target Workflow

First, create a simple workflow that we can turn on and off.

  1. Create a new workflow and name it "Target Workflow".
  2. Add a Schedule trigger and set it to run every minute.
  3. Add a NoOp node after it (this is just a placeholder).
  4. Save and activate this workflow. Note its URL; the string of characters at the end is its unique ID (e.g., .../workflow/3).

Part 2: The Manager Workflow

Now, let's build the controller.

  1. Create a new workflow and name it "Workflow Manager".
  2. Add a Webhook node. This will be our API endpoint. It automatically provides you with a test and production URL.
  3. Add an If node connected to the Webhook. We'll use this to decide whether to activate or deactivate the target.
    • Set the first condition to: String -> {{ $json.query.action }} -> Equals -> activate.
  4. Add an n8n API node and connect it to the true output of the If node.
    • Credential: Select your n8n API credential.
    • Resource: Workflow
    • Operation: Activate
    • Workflow ID: Use an expression to get the ID from the webhook's query parameters: {{ $json.query.id }}
  5. Add another n8n API node and connect it to the false output of the If node.
    • Configure it identically, but set the Operation to Deactivate.
  6. Save the "Workflow Manager" workflow and activate it.

Testing it out:

  1. Copy the Test URL from your Webhook node.
  2. Open a new browser tab and paste the URL. Append query parameters to it to specify the target workflow ID and the action.
    • To deactivate: YOUR_WEBHOOK_URL?id=TARGET_ID&action=deactivate (replace TARGET_ID with the ID from Part 1).
    • You should get a {"message":"Workflow execution started successfully"} response.
    • Go back to your n8n workflows list; the "Target Workflow" should now be inactive.
  3. Now, change the action in the URL to activate.
    • To activate: YOUR_WEBHOOK_URL?id=TARGET_ID&action=activate
    • The "Target Workflow" should now be active again.

This simple pattern is incredibly powerful. You could use it to build a dashboard for business users to toggle automations, or have a master workflow that deactivates other workflows during a system maintenance window.

5. Under the Hood with the HTTP Request Node

The n8n API node is a convenient abstraction. Underneath, it's simply a pre-configured HTTP Request node. Understanding this is key for a developer, as it allows you to call any part of the n8n API, even endpoints not yet implemented in the dedicated node.

Let's replicate our "Activate" operation using the HTTP Request node.

n8n HTTP Request Node Configuration
This is the configuration panel for the standard HTTP Request node. We will use it to manually construct the same API call that the n8n API node performs automatically.
  1. Add an HTTP Request node to your "Workflow Manager" workflow.
  2. Configure it as follows:
    • Method: POST
    • URL: https://YOUR_INSTANCE.n8n.cloud/api/v1/workflows/{{ $json.query.id }}/activate (Replace with your n8n instance URL. The base path for the API is /api/v1).
    • Authentication: Header Auth
    • Name: X-N8N-API-KEY
    • Value: Paste your n8n API key directly here. (In a real scenario, you'd use a credential expression for better security).

This configuration achieves the exact same result as the n8n API node configured to activate a workflow. Seeing this connection empowers you to read the n8n API documentation and interact with any part of the system.

Conclusion

Today you've unlocked a new level of control over n8n. By learning to use the n8n API, you've moved from being a user of the platform to a programmatic manager of it. This is a fundamental skill for building complex, scalable, and manageable automation solutions.

Key Takeaways:

  • n8n has a full-featured REST API that allows for programmatic control over workflows, executions, and more.
  • The n8n API node is a convenient, purpose-built tool for interacting with this API, handling authentication and operations through a simple UI.
  • You can perform CRUD (Create, Read, Update, Delete) operations on workflows, as well as Activate and Deactivate them.
  • Programmatically creating workflows requires a detailed JSON object defining nodes and their connections.
  • The n8n API node is a wrapper around the standard HTTP Request node, and understanding the underlying API calls gives you maximum flexibility.

Preview of the Next Lesson:

In this lesson, you learned how to tell n8n what to do (e.g., "activate this workflow"). In the next lesson, we'll explore the other side of the coin: having n8n tell you when something happens. You will use the n8n Trigger node to react to internal events like workflow executions, creating automations that respond to the lifecycle of other automations. This is the event-driven counterpart to the command-driven approach you learned today.

Can't find a good explanation? Sign up and we'll make it for you

Sign up