Files
nusquama dc2ce7454d creation
2025-11-12 16:36:26 +01:00

280 lines
16 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Create Dynamic Workflows Programmatically via Webhooks & n8n API
https://n8nworkflows.xyz/workflows/create-dynamic-workflows-programmatically-via-webhooks---n8n-api-4544
# Create Dynamic Workflows Programmatically via Webhooks & n8n API
### 1. Workflow Overview
This workflow enables dynamic creation of new n8n workflows programmatically via an HTTP POST request to a webhook. It is designed for use cases where external systems or users want to automate the deployment of workflows by submitting their JSON definitions to n8n. The workflow performs validation on the submitted JSON, attempts to create the workflow through the n8n API, and returns a structured JSON response indicating success or failure.
The logic is organized into three main blocks:
- **1.1 Input Reception:** Receives the incoming HTTP POST request containing the workflow JSON.
- **1.2 Payload Validation:** Validates the structure and required fields of the workflow JSON to ensure it can be processed.
- **1.3 Workflow Creation & Response:** Submits the validated workflow JSON to the n8n API to create the workflow and returns success or error response accordingly.
---
### 2. Block-by-Block Analysis
#### 1.1 Input Reception
**Overview:**
This block receives the HTTP POST request at a webhook endpoint `/webhook/create-workflow`, extracting the submitted workflow JSON payload.
**Nodes Involved:**
- Webhook
- Sticky Note4 (comment)
**Node Details:**
- **Webhook**
- Type: Webhook node
- Role: Entry point receiving POST requests at path `/create-workflow`.
- Configuration: HTTP Method POST, response mode set to wait for the last node to respond.
- Inputs: None (trigger node)
- Outputs: Passes the full request including headers and body to the next node.
- Edge cases: Invalid HTTP method or missing payload could cause failures downstream.
- Notes: This node listens on `/webhook/create-workflow`.
- **Sticky Note4**
- Type: Sticky Note
- Role: Documentation for this block detailing the webhooks purpose and initial validation step.
---
#### 1.2 Payload Validation
**Overview:**
This block validates the incoming JSON payload to ensure it contains all required workflow fields (`name`, `nodes`, etc.) and that each node has mandatory properties (`id`, `name`, `type`, `position`). It prepares the payload for the API call or returns validation errors.
**Nodes Involved:**
- Validate JSON (Code node)
- Validation Successful? (If node)
- Validation Error (Set node)
- Sticky Note4 (comment)
**Node Details:**
- **Validate JSON**
- Type: Code node (JavaScript)
- Role: Validates the incoming workflow JSON structure and fields.
- Configuration details:
- Checks presence of `name` and `nodes` in the payload.
- Verifies `connections` and `settings` objects exist or sets defaults.
- Ensures at least one node is present.
- Validates required fields on each node (`id`, `name`, `type`, `position`).
- Sets default empty parameters and version 1 for nodes missing these.
- Returns `{ success: true, apiWorkflow: <validated workflow> }` on valid payload or `{ success: false, message: <error> }` on failure.
- Inputs: Receives entire request JSON from webhook.
- Outputs: JSON object indicating validation result.
- Edge cases: Missing fields, empty nodes array, malformed node objects, missing parameters or typeVersion handled gracefully with defaults or error messages.
- Version note: Assumes n8n API v1 workflow structure.
- **Validation Successful?**
- Type: If node
- Role: Branch logic based on validation results `success` boolean.
- Configuration: Checks if `$json.success` equals `true`.
- Inputs: Output from Validate JSON node.
- Outputs: Two branches — continue to workflow creation if true; else go to validation error response.
- Edge cases: Unexpected non-boolean or missing success flag could cause logic errors.
- **Validation Error**
- Type: Set node
- Role: Prepares JSON response with validation failure message.
- Configuration: Sets `success: false` and copies the error message from the validation node (`$json.message`).
- Inputs: Validation failure branch from “Validation Successful?” node.
- Outputs: Final response to requester on validation failure.
- **Sticky Note4** (text content covers this block as well)
- Explains the validation logic steps.
---
#### 1.3 Workflow Creation & Response
**Overview:**
This block sends the validated workflow JSON to the n8n API endpoint to create the workflow, checks if the API call succeeded, and returns a success or error JSON response accordingly.
**Nodes Involved:**
- Create Workflow (HTTP Request)
- API Successful? (If node)
- Success Response (Set node)
- API Error (Set node)
- Sticky Note6 (comment)
- Sticky Note7 (comment)
**Node Details:**
- **Create Workflow**
- Type: HTTP Request node
- Role: Sends POST request to n8n internal API `/api/v1/workflows` with validated workflow JSON.
- Configuration:
- URL dynamically constructed using the webhooks incoming request `host` header to target the correct server instance.
- Authentication via header-based credentials (API token).
- Sends the entire validated workflow object as the POST payload.
- Continue on fail set to true to allow catching errors downstream.
- Inputs: Validated workflow JSON from “Validation Successful?” node.
- Outputs: HTTP response with status code and response body from API.
- Edge cases: Network failure, authentication errors, API validation errors, server downtime.
- **API Successful?**
- Type: If node
- Role: Checks if the API response status code indicates success (≤ 299).
- Configuration: Compares `$response.statusCode` ≤ 299.
- Inputs: Response from “Create Workflow” node.
- Outputs: Branches to success response or API error handling.
- **Success Response**
- Type: Set node
- Role: Constructs success JSON response for the client.
- Configuration: Sets:
- `success: true`
- `message: Workflow created successfully`
- `workflowId`, `workflowName`, `createdAt` extracted from API response data.
- `url` constructed as `http://localhost:5678/workflow/<workflowId>` for direct access.
- Inputs: Success branch from “API Successful?” node.
- **API Error**
- Type: Set node
- Role: Constructs failure JSON response for the client on API error.
- Configuration: Sets:
- `success: false`
- `message: Error creating workflow`
- `error` containing stringified full error JSON from API response.
- `statusCode` from API response.
- Inputs: Failure branch from “API Successful?” node.
- **Sticky Note6**
- Describes the error handling responses for validation and API errors.
- **Sticky Note7**
- Describes the workflow creation API call, status check, and success response construction.
---
### 3. Summary Table
| Node Name | Node Type | Functional Role | Input Node(s) | Output Node(s) | Sticky Note |
|---------------------|--------------------|----------------------------------------|--------------------|------------------------------|---------------------------------------------------------------|
| Webhook | Webhook | Receives POST at `/create-workflow` | None | Validate JSON | 1. Webhook receives POST with workflow JSON |
| Validate JSON | Code | Validates workflow JSON structure | Webhook | Validation Successful? | 2. Validates `name`, `nodes`, each node's required fields |
| Validation Successful? | If | Branches on validation success | Validate JSON | Create Workflow, Validation Error | See Sticky Note4 content |
| Validation Error | Set | Sets failure response on validation error | Validation Successful? (fail branch) | None | 1. Sets JSON response with validation failure message |
| Create Workflow | HTTP Request | Sends POST to n8n API to create workflow | Validation Successful? (success branch) | API Successful? | 1. Sends POST to `/api/v1/workflows` with API header auth |
| API Successful? | If | Checks API response status success | Create Workflow | Success Response, API Error | 2. Checks if status code ≤ 299 |
| Success Response | Set | Constructs success JSON response | API Successful? (success branch) | None | 3. Returns JSON with workflow details and success flag |
| API Error | Set | Constructs error JSON response | API Successful? (fail branch) | None | 2. Returns JSON error with API error details |
| Sticky Note4 | Sticky Note | Documentation for webhook and validation | None | None | See content in block 1.1 and 1.2 |
| Sticky Note6 | Sticky Note | Documentation for error response handling | None | None | Describes Validation Error and API Error JSON responses |
| Sticky Note7 | Sticky Note | Documentation for workflow creation steps | None | None | Describes API request, success check, and success response |
---
### 4. Reproducing the Workflow from Scratch
1. **Create Webhook Node**
- Type: Webhook
- Name: `Webhook`
- HTTP Method: POST
- Path: `create-workflow`
- Response Mode: `lastNode` (wait for final node response)
- Position: (e.g., x = -2280, y = -1880)
2. **Create Code Node for Validation**
- Type: Code
- Name: `Validate JSON`
- JavaScript code:
- Extract the incoming payload from `$input.item.json.body`.
- Check if payload exists.
- Validate required fields: `name`, `nodes`.
- Default `connections` and `settings` if missing.
- Check `nodes` is a non-empty array.
- For each node, verify `id`, `name`, `type`, `position`.
- Default `parameters` to `{}` and `typeVersion` to `1` if missing.
- Return `{ success: true, apiWorkflow: <prepared payload> }` or failure JSON with message.
- Connect Webhook node output to this node input.
3. **Create If Node for Validation Result Branching**
- Type: If
- Name: `Validation Successful?`
- Condition: Boolean check `$json.success === true`
- Connect `Validate JSON` node output to this node input.
4. **Create Set Node for Validation Error**
- Type: Set
- Name: `Validation Error`
- Values:
- `success` = `"false"`
- `message` = `{{$json.message}}` (from validation code node)
- Connect the false branch of `Validation Successful?` node to this node.
5. **Create HTTP Request Node to Create Workflow**
- Type: HTTP Request
- Name: `Create Workflow`
- HTTP Method: POST
- URL: `=http://{{ $('Webhook').item.json.headers.host }}/api/v1/workflows`
- Authentication: Header Auth using n8n API token credential
- Payload: Use the `apiWorkflow` object from the validation code node output.
- Continue On Fail: Enabled (true)
- Connect the true branch of `Validation Successful?` node to this node.
6. **Create If Node to Check API Success**
- Type: If
- Name: `API Successful?`
- Condition: Number check `$response.statusCode <= 299`
- Connect `Create Workflow` node output to this node.
7. **Create Set Node for Success Response**
- Type: Set
- Name: `Success Response`
- Values:
- `success` = `"true"`
- `message` = `"Workflow created successfully"`
- `workflowId` = `{{$json.data[0].id}}`
- `workflowName` = `{{$json.data[0].name}}`
- `createdAt` = `{{$json.data[0].createdAt}}`
- `url` = `"http://localhost:5678/workflow/{{$json.data[0].id}}"`
- Connect the true branch of `API Successful?` node to this node.
8. **Create Set Node for API Error Response**
- Type: Set
- Name: `API Error`
- Values:
- `success` = `"false"`
- `message` = `"Error creating workflow"`
- `error` = `{{ JSON.stringify($json) }}` (full API response)
- `statusCode` = `{{$response.statusCode}}`
- Connect the false branch of `API Successful?` node to this node.
9. **Connect the outputs for final HTTP response**
- The webhooks HTTP response mode is set to wait for the last node, so make sure the terminal nodes for success, validation error, and API error are endpoints returning the JSON.
10. **Create Sticky Notes (optional for documentation)**
- Add sticky notes near relevant nodes with the content as per the workflows notes for clarity.
11. **Set Credentials**
- Configure HTTP Header Auth credentials with a valid n8n API token for the `Create Workflow` node.
12. **Test the Workflow**
- POST a valid workflow JSON to `http://<your-n8n-host>:5678/webhook/create-workflow`
- Verify success and error responses as expected.
---
### 5. General Notes & Resources
| Note Content | Context or Link |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------|
| The workflow dynamically constructs the API URL using the incoming requests `host` header to support deployment flexibility across environments. | Node “Create Workflow” URL configuration |
| This workflow requires a valid n8n API token set in credentials for secure creation of workflows via the REST API. | Authentication setup for “Create Workflow” node |
| On successful workflow creation, the returned URL assumes the n8n instance is accessible at `localhost:5678`; update this if hosted elsewhere. | URL construction in “Success Response” node |
| The validation code node ensures minimal viable workflow JSON structure to prevent API errors and unexpected failures. | “Validate JSON” node details |
| For further details on n8n API workflow creation, visit: https://docs.n8n.io/reference/api/workflows/ | Official n8n API documentation |
| This workflow demonstrates a pattern for receiving arbitrary workflows over HTTP and deploying them programmatically, useful for multi-tenant or automated CI/CD pipeline integrations. | Use case context |
---
**Disclaimer:** This document is generated from an n8n workflow JSON export and respects all content and usage policies. The workflow handles only valid, legal, and authorized data.