Créer un workflow dans n8n via Webhook

Intermédiaire

Ceci est unEngineeringworkflow d'automatisation du domainecontenant 11 nœuds.Utilise principalement des nœuds comme If, Set, Code, Webhook, HttpRequest. Créer des workflows dynamiques par programmation avec Webhook et n8n API

Prérequis
  • Point de terminaison HTTP Webhook (généré automatiquement par n8n)
  • Peut nécessiter les informations d'identification d'authentification de l'API cible

Catégorie

Aperçu du workflow
Visualisation des connexions entre les nœuds, avec support du zoom et du déplacement
Exporter le workflow
Copiez la configuration JSON suivante dans n8n pour importer et utiliser ce workflow
{
  "id": "mchujUnGa9RKf7dO",
  "meta": {
    "instanceId": "6f30db2c3fce1d1703bcd68e01088879e38bc7981b202c52c370fb8fb136ded4",
    "templateCredsSetupCompleted": true
  },
  "name": "Create Workflow via Webhook in n8n",
  "tags": [],
  "nodes": [
    {
      "id": "41cfbc58-c8d5-4a22-aec1-72a4b7db3d63",
      "name": "Note adhésive4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2360,
        -2100
      ],
      "parameters": {
        "width": 720,
        "height": 500,
        "content": "1. **Webhook**: it receives a POST at `/webhook/create-workflow` with the workflow JSON.\n2. **Validate JSON**: it runs code that checks the JSON has the fields `name` and `nodes`, and that each node has `id`, `name`, `type`, and `position`. It returns `{ success: true }` if everything is valid, or `{ success: false, message: \"…\" }` if there’s an error.\n3. **Validation Successful?**: it looks at the result from “Validate JSON” and, depending on whether `success` is `true` or `false`, decides to continue creating the workflow or return the error.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "62c4a402-1397-4aad-82de-c38672a382a7",
      "name": "Note adhésive6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1600,
        -1740
      ],
      "parameters": {
        "color": 3,
        "width": 820,
        "height": 360,
        "content": "1. **Validation Error**: Sets a JSON response with `success: false` and uses the incoming `$json.message` as the error message when the initial payload validation fails.\n\n2. **API Error**: Sets a JSON response with `success: false`, a fixed `message: \"Error creating workflow\"`, includes the full error payload (`error: JSON.stringify($json)`), and the HTTP `statusCode` from the failed API request.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "df771acf-ef37-49f9-8a9f-ec60fd955d86",
      "name": "Note adhésive7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1520,
        -2320
      ],
      "parameters": {
        "width": 720,
        "height": 500,
        "content": "1. **Create Workflow**: Sends a POST to `/api/v1/workflows` with the validated workflow payload, using header-based authentication.\n2. **API Successful?**: Checks if the HTTP response status code is ≤ 299.\n3. **Success Response**: If the API call succeeded, constructs the final JSON with `success: true`, `workflowId`, `workflowName`, `createdAt`, and the workflow `url`.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "c798d2a9-577d-4f46-b269-ca2b4447560c",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -2280,
        -1880
      ],
      "webhookId": "baad3add-f74a-447a-9764-75967674fa9f",
      "parameters": {
        "path": "create-workflow",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "lastNode"
      },
      "notesInFlow": false,
      "typeVersion": 1
    },
    {
      "id": "3ca95b00-83b2-4f75-874f-43a27e298bf1",
      "name": "Valider JSON",
      "type": "n8n-nodes-base.code",
      "position": [
        -2060,
        -1880
      ],
      "parameters": {
        "jsCode": "// Validate the received JSON\nconst payload = $input.item.json.body;\n\n// Check that required fields exist\nif (!payload) {\n  return {\n    json: {\n      success: false,\n      message: \"No payload received in the request\"\n    }\n  };\n}\n\n// Ensure correct format for a workflow\nconst requiredFields = ['name', 'nodes'];\nfor (const field of requiredFields) {\n  if (!payload[field]) {\n    return {\n      json: {\n        success: false,\n        message: `The field '${field}' is required in the workflow`\n      }\n    };\n  }\n}\n\n// Ensure connections and settings exist\nif (!payload.connections) payload.connections = {};\nif (!payload.settings) payload.settings = {};\n\n// Verify that there is at least one node\nif (!Array.isArray(payload.nodes) || payload.nodes.length === 0) {\n  return {\n    json: {\n      success: false,\n      message: \"The workflow must have at least one node\"\n    }\n  };\n}\n\n// Validate each node's required fields\nfor (const node of payload.nodes) {\n  const nodeRequiredFields = ['id', 'name', 'type', 'position'];\n  for (const field of nodeRequiredFields) {\n    if (!node[field]) {\n      return {\n        json: {\n          success: false,\n          message: `Node '${node.name || 'unknown'}' is missing required field '${field}'`\n        }\n      };\n    }\n  }\n  \n  // Ensure parameters exists\n  if (!node.parameters) node.parameters = {};\n  if (!node.typeVersion) node.typeVersion = 1;\n}\n\n// Prepare the workflow object for the API\nconst apiWorkflow = {\n  name: payload.name,\n  nodes: payload.nodes,\n  connections: payload.connections,\n  settings: payload.settings\n};\n\n// If everything is valid, proceed\nreturn {\n  json: {\n    success: true,\n    apiWorkflow: apiWorkflow\n  }\n};"
      },
      "typeVersion": 1
    },
    {
      "id": "b3894755-0583-4c00-b574-e993a6f0b08f",
      "name": "Validation réussie ?",
      "type": "n8n-nodes-base.if",
      "position": [
        -1840,
        -1880
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.success }}",
              "value2": true
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ef3a310b-7673-40cc-985a-4e87dc99de70",
      "name": "Créer un workflow",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1440,
        -2080
      ],
      "parameters": {
        "url": "=http://{{ $('Webhook').item.json.headers.host }}/api/v1/workflows",
        "options": {},
        "authentication": "headerAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "TS0ZOhjrOm9oUaWR",
          "name": "N8N"
        }
      },
      "typeVersion": 1,
      "continueOnFail": true
    },
    {
      "id": "7e2c1a11-1d4c-4075-a03c-f6ed93307d19",
      "name": "API réussi ?",
      "type": "n8n-nodes-base.if",
      "position": [
        -1220,
        -2080
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $response.statusCode }}",
              "value2": 299,
              "operation": "smallerEqual"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0371ce73-ccb6-4e87-8620-4aab8529b170",
      "name": "Réponse de succès",
      "type": "n8n-nodes-base.set",
      "position": [
        -960,
        -2180
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "success",
              "value": "true"
            },
            {
              "name": "message",
              "value": "Workflow created successfully"
            },
            {
              "name": "workflowId",
              "value": "={{ $json.data[0].id }}"
            },
            {
              "name": "workflowName",
              "value": "={{ $json.data[0].name }}"
            },
            {
              "name": "createdAt",
              "value": "={{ $json.data[0].createdAt }}"
            },
            {
              "name": "url",
              "value": "=http://localhost:5678/workflow/{{ $json.data[0].id }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "daec93d7-b9d8-4407-93ab-389b705b0c11",
      "name": "Erreur de validation",
      "type": "n8n-nodes-base.set",
      "position": [
        -1500,
        -1540
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "success",
              "value": "false"
            },
            {
              "name": "message",
              "value": "={{ $json.message }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "cd2cd403-63c2-42af-a715-9f080067f7d1",
      "name": "Erreur API",
      "type": "n8n-nodes-base.set",
      "position": [
        -920,
        -1540
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "success",
              "value": "false"
            },
            {
              "name": "message",
              "value": "Error creating workflow"
            },
            {
              "name": "error",
              "value": "={{ JSON.stringify($json) }}"
            },
            {
              "name": "statusCode",
              "value": "={{ $response.statusCode }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "pinData": {
    "Webhook": [
      {
        "json": {
          "body": {
            "name": "Workflow Created by ID mchujUnGa9RKf7dO",
            "nodes": [
              {
                "id": "start-node",
                "name": "Start",
                "type": "n8n-nodes-base.manualTrigger",
                "position": [
                  100,
                  100
                ],
                "parameters": {},
                "typeVersion": 1
              },
              {
                "id": "set-node",
                "name": "Set",
                "type": "n8n-nodes-base.set",
                "position": [
                  300,
                  100
                ],
                "parameters": {
                  "values": {
                    "string": [
                      {
                        "name": "message",
                        "value": "This workflow was created using the webhook in mchujUnGa9RKf7dO!"
                      }
                    ]
                  }
                },
                "typeVersion": 1
              }
            ],
            "settings": {},
            "connections": {
              "Start": {
                "main": [
                  [
                    {
                      "node": "Set",
                      "type": "main",
                      "index": 0
                    }
                  ]
                ]
              }
            }
          },
          "query": {},
          "params": {},
          "headers": {
            "host": "127.0.0.1:5678",
            "connection": "keep-alive",
            "content-type": "application/json",
            "transfer-encoding": "chunked"
          },
          "webhookUrl": "http://localhost:5678/webhook/create-workflow",
          "executionMode": "production"
        }
      }
    ]
  },
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "849462be-761a-455e-a99b-ffe52b2249d7",
  "connections": {
    "c798d2a9-577d-4f46-b269-ca2b4447560c": {
      "main": [
        [
          {
            "node": "3ca95b00-83b2-4f75-874f-43a27e298bf1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "3ca95b00-83b2-4f75-874f-43a27e298bf1": {
      "main": [
        [
          {
            "node": "b3894755-0583-4c00-b574-e993a6f0b08f",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "7e2c1a11-1d4c-4075-a03c-f6ed93307d19": {
      "main": [
        [
          {
            "node": "0371ce73-ccb6-4e87-8620-4aab8529b170",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "cd2cd403-63c2-42af-a715-9f080067f7d1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ef3a310b-7673-40cc-985a-4e87dc99de70": {
      "main": [
        [
          {
            "node": "7e2c1a11-1d4c-4075-a03c-f6ed93307d19",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "b3894755-0583-4c00-b574-e993a6f0b08f": {
      "main": [
        [
          {
            "node": "ef3a310b-7673-40cc-985a-4e87dc99de70",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "daec93d7-b9d8-4407-93ab-389b705b0c11",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
Foire aux questions

Comment utiliser ce workflow ?

Copiez le code de configuration JSON ci-dessus, créez un nouveau workflow dans votre instance n8n et sélectionnez "Importer depuis le JSON", collez la configuration et modifiez les paramètres d'authentification selon vos besoins.

Dans quelles scénarios ce workflow est-il adapté ?

Intermédiaire - Ingénierie

Est-ce payant ?

Ce workflow est entièrement gratuit et peut être utilisé directement. Veuillez noter que les services tiers utilisés dans le workflow (comme l'API OpenAI) peuvent nécessiter un paiement de votre part.

Informations sur le workflow
Niveau de difficulté
Intermédiaire
Nombre de nœuds11
Catégorie1
Types de nœuds6
Description de la difficulté

Adapté aux utilisateurs expérimentés, avec des workflows de complexité moyenne contenant 6-15 nœuds

Auteur
Mauricio Perera

Mauricio Perera

@rckflr

Automation consultant with over 10 years of experience specializing in AI, no-code, and workflow optimization. I’ve delivered tailored AI and NLP solutions across real estate, healthcare, and more, enhancing efficiency and customer experiences. Proficient in tools like Make, Airtable, and Zapier, I also integrate GPT models to create scalable, innovative automations. Contact me to discuss custom n8n workflows or advanced automations to streamline your processes.

Liens externes
Voir sur n8n.io

Partager ce workflow

Catégories

Catégories: 34