Spaces:
Sleeping
Sleeping
File size: 16,130 Bytes
da2e594 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | # Workflow Diff Examples
This guide demonstrates how to use the `n8n_update_partial_workflow` tool for efficient workflow editing.
## Overview
The `n8n_update_partial_workflow` tool allows you to make targeted changes to workflows without sending the entire workflow JSON. This results in:
- 80-90% reduction in token usage
- More precise edits
- Clearer intent
- Reduced risk of accidentally modifying unrelated parts
## Basic Usage
```json
{
"id": "workflow-id-here",
"operations": [
{
"type": "operation-type",
"...operation-specific-fields..."
}
]
}
```
## Operation Types
### 1. Node Operations
#### Add Node
```json
{
"type": "addNode",
"description": "Add HTTP Request node to fetch data",
"node": {
"name": "Fetch User Data",
"type": "n8n-nodes-base.httpRequest",
"position": [600, 300],
"parameters": {
"url": "https://api.example.com/users",
"method": "GET",
"authentication": "none"
}
}
}
```
#### Remove Node
```json
{
"type": "removeNode",
"nodeName": "Old Node Name",
"description": "Remove deprecated node"
}
```
#### Update Node
```json
{
"type": "updateNode",
"nodeName": "HTTP Request",
"changes": {
"parameters.url": "https://new-api.example.com/v2/users",
"parameters.headers.parameters": [
{
"name": "Authorization",
"value": "Bearer {{$credentials.apiKey}}"
}
]
},
"description": "Update API endpoint to v2"
}
```
#### Move Node
```json
{
"type": "moveNode",
"nodeName": "Set Variable",
"position": [800, 400],
"description": "Reposition for better layout"
}
```
#### Enable/Disable Node
```json
{
"type": "disableNode",
"nodeName": "Debug Node",
"description": "Disable debug output for production"
}
```
### 2. Connection Operations
#### Add Connection
```json
{
"type": "addConnection",
"source": "Webhook",
"target": "Process Data",
"sourceOutput": "main",
"targetInput": "main",
"description": "Connect webhook to processor"
}
```
#### Remove Connection
```json
{
"type": "removeConnection",
"source": "Old Source",
"target": "Old Target",
"description": "Remove unused connection"
}
```
#### Rewire Connection
```json
{
"type": "rewireConnection",
"source": "Webhook",
"from": "Old Handler",
"to": "New Handler",
"description": "Rewire connection to new handler"
}
```
#### Smart Parameters for IF Nodes
```json
{
"type": "addConnection",
"source": "IF",
"target": "Success Handler",
"branch": "true", // Semantic parameter instead of sourceIndex
"description": "Route true branch to success handler"
}
```
```json
{
"type": "addConnection",
"source": "IF",
"target": "Error Handler",
"branch": "false", // Routes to false branch (sourceIndex=1)
"description": "Route false branch to error handler"
}
```
#### Smart Parameters for Switch Nodes
```json
{
"type": "addConnection",
"source": "Switch",
"target": "Handler A",
"case": 0, // First output
"description": "Route case 0 to Handler A"
}
```
### 3. Workflow Metadata Operations
#### Update Workflow Name
```json
{
"type": "updateName",
"name": "Production User Sync v2",
"description": "Update workflow name for versioning"
}
```
#### Update Settings
```json
{
"type": "updateSettings",
"settings": {
"executionTimeout": 300,
"saveDataErrorExecution": "all",
"timezone": "America/New_York"
},
"description": "Configure production settings"
}
```
#### Manage Tags
```json
{
"type": "addTag",
"tag": "production",
"description": "Mark as production workflow"
}
```
## Complete Examples
### Example 1: Add Slack Notification to Workflow
```json
{
"id": "workflow-123",
"operations": [
{
"type": "addNode",
"node": {
"name": "Send Slack Alert",
"type": "n8n-nodes-base.slack",
"position": [1000, 300],
"parameters": {
"resource": "message",
"operation": "post",
"channel": "#alerts",
"text": "Workflow completed successfully!"
}
}
},
{
"type": "addConnection",
"source": "Process Data",
"target": "Send Slack Alert"
}
]
}
```
### Example 2: Update Multiple Webhook Paths
```json
{
"id": "workflow-456",
"operations": [
{
"type": "updateNode",
"nodeName": "Webhook 1",
"changes": {
"parameters.path": "v2/webhook1"
}
},
{
"type": "updateNode",
"nodeName": "Webhook 2",
"changes": {
"parameters.path": "v2/webhook2"
}
},
{
"type": "updateName",
"name": "API v2 Webhooks"
}
]
}
```
### Example 3: Refactor Workflow Structure
```json
{
"id": "workflow-789",
"operations": [
{
"type": "removeNode",
"nodeName": "Legacy Processor"
},
{
"type": "addNode",
"node": {
"name": "Modern Processor",
"type": "n8n-nodes-base.code",
"position": [600, 300],
"parameters": {
"mode": "runOnceForEachItem",
"jsCode": "// Process items\nreturn item;"
}
}
},
{
"type": "addConnection",
"source": "HTTP Request",
"target": "Modern Processor"
},
{
"type": "addConnection",
"source": "Modern Processor",
"target": "Save to Database"
}
]
}
```
### Example 4: Add Error Handling
```json
{
"id": "workflow-999",
"operations": [
{
"type": "addNode",
"node": {
"name": "Error Handler",
"type": "n8n-nodes-base.errorTrigger",
"position": [200, 500]
}
},
{
"type": "addNode",
"node": {
"name": "Send Error Email",
"type": "n8n-nodes-base.emailSend",
"position": [400, 500],
"parameters": {
"toEmail": "admin@example.com",
"subject": "Workflow Error: {{$node['Error Handler'].json.error.message}}",
"text": "Error details: {{$json}}"
}
}
},
{
"type": "addConnection",
"source": "Error Handler",
"target": "Send Error Email"
},
{
"type": "updateSettings",
"settings": {
"errorWorkflow": "workflow-999"
}
}
]
}
```
### Example 5: Large Batch Workflow Refactoring
Demonstrates handling many operations in a single request - no longer limited to 5 operations!
```json
{
"id": "workflow-batch",
"operations": [
// Add 10 processing nodes
{
"type": "addNode",
"node": {
"name": "Filter Active Users",
"type": "n8n-nodes-base.filter",
"position": [400, 200],
"parameters": { "conditions": { "boolean": [{ "value1": "={{$json.active}}", "value2": true }] } }
}
},
{
"type": "addNode",
"node": {
"name": "Transform User Data",
"type": "n8n-nodes-base.set",
"position": [600, 200],
"parameters": { "values": { "string": [{ "name": "formatted_name", "value": "={{$json.firstName}} {{$json.lastName}}" }] } }
}
},
{
"type": "addNode",
"node": {
"name": "Validate Email",
"type": "n8n-nodes-base.if",
"position": [800, 200],
"parameters": { "conditions": { "string": [{ "value1": "={{$json.email}}", "operation": "contains", "value2": "@" }] } }
}
},
{
"type": "addNode",
"node": {
"name": "Enrich with API",
"type": "n8n-nodes-base.httpRequest",
"position": [1000, 150],
"parameters": { "url": "https://api.example.com/enrich", "method": "POST" }
}
},
{
"type": "addNode",
"node": {
"name": "Log Invalid Emails",
"type": "n8n-nodes-base.code",
"position": [1000, 350],
"parameters": { "jsCode": "console.log('Invalid email:', $json.email);\nreturn $json;" }
}
},
{
"type": "addNode",
"node": {
"name": "Merge Results",
"type": "n8n-nodes-base.merge",
"position": [1200, 250]
}
},
{
"type": "addNode",
"node": {
"name": "Deduplicate",
"type": "n8n-nodes-base.removeDuplicates",
"position": [1400, 250],
"parameters": { "propertyName": "id" }
}
},
{
"type": "addNode",
"node": {
"name": "Sort by Date",
"type": "n8n-nodes-base.sort",
"position": [1600, 250],
"parameters": { "sortFieldsUi": { "sortField": [{ "fieldName": "created_at", "order": "descending" }] } }
}
},
{
"type": "addNode",
"node": {
"name": "Batch for DB",
"type": "n8n-nodes-base.splitInBatches",
"position": [1800, 250],
"parameters": { "batchSize": 100 }
}
},
{
"type": "addNode",
"node": {
"name": "Save to Database",
"type": "n8n-nodes-base.postgres",
"position": [2000, 250],
"parameters": { "operation": "insert", "table": "processed_users" }
}
},
// Connect all the nodes
{
"type": "addConnection",
"source": "Get Users",
"target": "Filter Active Users"
},
{
"type": "addConnection",
"source": "Filter Active Users",
"target": "Transform User Data"
},
{
"type": "addConnection",
"source": "Transform User Data",
"target": "Validate Email"
},
{
"type": "addConnection",
"source": "Validate Email",
"sourceOutput": "true",
"target": "Enrich with API"
},
{
"type": "addConnection",
"source": "Validate Email",
"sourceOutput": "false",
"target": "Log Invalid Emails"
},
{
"type": "addConnection",
"source": "Enrich with API",
"target": "Merge Results"
},
{
"type": "addConnection",
"source": "Log Invalid Emails",
"target": "Merge Results",
"targetInput": "input2"
},
{
"type": "addConnection",
"source": "Merge Results",
"target": "Deduplicate"
},
{
"type": "addConnection",
"source": "Deduplicate",
"target": "Sort by Date"
},
{
"type": "addConnection",
"source": "Sort by Date",
"target": "Batch for DB"
},
{
"type": "addConnection",
"source": "Batch for DB",
"target": "Save to Database"
},
// Update workflow metadata
{
"type": "updateName",
"name": "User Processing Pipeline v2"
},
{
"type": "updateSettings",
"settings": {
"executionOrder": "v1",
"timezone": "UTC",
"saveDataSuccessExecution": "all"
}
},
{
"type": "addTag",
"tag": "production"
},
{
"type": "addTag",
"tag": "user-processing"
},
{
"type": "addTag",
"tag": "v2"
}
]
}
```
This example shows 26 operations in a single request, creating a complete data processing pipeline with proper error handling, validation, and batch processing.
## Best Practices
1. **Use Descriptive Names**: Always provide clear node names and descriptions for operations
2. **Batch Related Changes**: Group related operations in a single request
3. **Validate First**: Use `validateOnly: true` to test your operations before applying
4. **Reference by Name**: Prefer node names over IDs for better readability
5. **Small, Focused Changes**: Make targeted edits rather than large structural changes
## Common Patterns
### Add Processing Step
```json
{
"operations": [
{
"type": "removeConnection",
"source": "Source Node",
"target": "Target Node"
},
{
"type": "addNode",
"node": {
"name": "Process Step",
"type": "n8n-nodes-base.set",
"position": [600, 300],
"parameters": { /* ... */ }
}
},
{
"type": "addConnection",
"source": "Source Node",
"target": "Process Step"
},
{
"type": "addConnection",
"source": "Process Step",
"target": "Target Node"
}
]
}
```
### Replace Node
```json
{
"operations": [
{
"type": "addNode",
"node": {
"name": "New Implementation",
"type": "n8n-nodes-base.httpRequest",
"position": [600, 300],
"parameters": { /* ... */ }
}
},
{
"type": "removeConnection",
"source": "Previous Node",
"target": "Old Implementation"
},
{
"type": "removeConnection",
"source": "Old Implementation",
"target": "Next Node"
},
{
"type": "addConnection",
"source": "Previous Node",
"target": "New Implementation"
},
{
"type": "addConnection",
"source": "New Implementation",
"target": "Next Node"
},
{
"type": "removeNode",
"nodeName": "Old Implementation"
}
]
}
```
## Error Handling
The tool validates all operations before applying any changes. Common errors include:
- **Duplicate node names**: Each node must have a unique name
- **Invalid node types**: Use full package prefixes (e.g., `n8n-nodes-base.webhook`)
- **Missing connections**: Referenced nodes must exist
- **Circular dependencies**: Connections cannot create loops
Always check the response for validation errors and adjust your operations accordingly.
## Transactional Updates
The diff engine now supports transactional updates using a **two-pass processing** approach:
### How It Works
1. **No Operation Limit**: Process unlimited operations in a single request
2. **Two-Pass Processing**:
- **Pass 1**: All node operations (add, remove, update, move, enable, disable)
- **Pass 2**: All other operations (connections, settings, metadata)
This allows you to add nodes and connect them in the same request:
```json
{
"id": "workflow-id",
"operations": [
// These will be processed in Pass 2 (but work because nodes are added first)
{
"type": "addConnection",
"source": "Webhook",
"target": "Process Data"
},
{
"type": "addConnection",
"source": "Process Data",
"target": "Send Email"
},
// These will be processed in Pass 1
{
"type": "addNode",
"node": {
"name": "Process Data",
"type": "n8n-nodes-base.set",
"position": [400, 300],
"parameters": {}
}
},
{
"type": "addNode",
"node": {
"name": "Send Email",
"type": "n8n-nodes-base.emailSend",
"position": [600, 300],
"parameters": {
"to": "user@example.com"
}
}
}
]
}
```
### Benefits
- **Order Independence**: You don't need to worry about operation order
- **Atomic Updates**: All operations succeed or all fail (unless continueOnError is enabled)
- **Intuitive Usage**: Add complex workflow structures in one call
- **No Hard Limits**: Process unlimited operations efficiently
### Example: Complete Workflow Addition
```json
{
"id": "workflow-id",
"operations": [
// Add three nodes
{
"type": "addNode",
"node": {
"name": "Schedule",
"type": "n8n-nodes-base.schedule",
"position": [200, 300],
"parameters": {
"rule": {
"interval": [{ "field": "hours", "intervalValue": 1 }]
}
}
}
},
{
"type": "addNode",
"node": {
"name": "Get Data",
"type": "n8n-nodes-base.httpRequest",
"position": [400, 300],
"parameters": {
"url": "https://api.example.com/data"
}
}
},
{
"type": "addNode",
"node": {
"name": "Save to Database",
"type": "n8n-nodes-base.postgres",
"position": [600, 300],
"parameters": {
"operation": "insert"
}
}
},
// Connect them all
{
"type": "addConnection",
"source": "Schedule",
"target": "Get Data"
},
{
"type": "addConnection",
"source": "Get Data",
"target": "Save to Database"
}
]
}
```
All operations will be processed correctly regardless of order! |