⚙️ Create Dynamic Workflows Programmatically via Webhooks & n8n API

1,569 views · ⚙️ DevOps & CI/CD

Description

Overview

This workflow exposes an HTTP endpoint (webhook) that accepts a JSON definition of an n8n workflow, validates it, and—if everything is correct—dynamically creates that workflow in the n8n instance via its internal API. If any validation fails or the API call encounters an error, an explanatory message with details is returned.

Workflow Diagram

Webhook


Validate JSON ── fails validation ──► Validation Error

   └─ passes ─► Validation Successful?

                           ├─ true ─► Create Workflow ──► API Successful? ──► Success Response
                           │                                 │
                           │                                 └─ false ─► API Error
                           └─ false ─► Validation Error

Step-by-Step Details

1. Webhook

2. Validate JSON

3. Validation Successful?

4. Create Workflow

5. API Successful?

6. Success Response

7. API Error

8. Validation Error

Example Webhook Request

curl --location --request POST 'http://localhost:5678/webhook/create-workflow' \
--header 'Content-Type: application/json' \
--data-raw '{
  "name": "My Dynamic Workflow",
  "nodes": [
    {
      "id": "start-node",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [100, 100],
      "parameters": {}
    },
    {
      "id": "set-node",
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [300, 100],
      "parameters": {
        "values": {
          "string": [
            { "name": "message", "value": "Hello from a webhook-created workflow!" }
          ]
        }
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [ { "node": "Set", "type": "main", "index": 0 } ]
      ]
    }
  },
  "settings": {}
}'

Expected Success Response

{
  "success": "true",
  "message": "Workflow created successfully",
  "workflowId": "abcdef1234567890",
  "workflowName": "My Dynamic Workflow",
  "createdAt": "2025-05-31T12:34:56.789Z",
  "url": "http://localhost:5678/workflow/abcdef1234567890"
}

Validation Error Response

{
  "success": false,
  "message": "The 'name' field is required in the workflow"
}

API Error Response

{
  "success": "false",
  "message": "Error creating workflow",
  "error": "{ ...full API response details... }",
  "statusCode": 401
}

🔗 Nodes Used

HTTP Request, Webhook

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup