{
  "id": "vtkfYX48kuD3WPPJ",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "API Failure Recovery System",
  "tags": [],
  "nodes": [
    {
      "id": "b71e2989-1a10-4889-8604-24c821ec4761",
      "name": "API Recovery Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -3216,
        4176
      ],
      "parameters": {
        "path": "api-recovery",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 1
    },
    {
      "id": "106e445d-a2dc-4da9-91e3-03d2835de513",
      "name": "Extract Request Configuration",
      "type": "n8n-nodes-base.set",
      "position": [
        -2960,
        4176
      ],
      "parameters": {
        "values": {
          "number": [
            {
              "name": "retryCount",
              "value": "={{$json.body.retryCount || 2}}"
            }
          ],
          "string": [
            {
              "name": "primaryApi",
              "value": "={{$json.body.primaryApi}}"
            },
            {
              "name": "backupApi",
              "value": "={{$json.body.backupApi}}"
            },
            {
              "name": "method",
              "value": "={{$json.body.method || 'GET'}}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "d4e498d4-84b5-4ac6-8d19-1d485bd8e064",
      "name": "Execute Primary API Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -2608,
        4176
      ],
      "parameters": {
        "url": "={{ $json.body.primaryApi }}",
        "method": "={{ $json.body.method }}",
        "options": {
          "timeout": 10000,
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        }
      },
      "typeVersion": 4,
      "continueOnFail": true
    },
    {
      "id": "09489481-c09d-404b-b4f6-4e9d69ecee5e",
      "name": "Check Primary API Success",
      "type": "n8n-nodes-base.if",
      "position": [
        -2384,
        4176
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json.statusCode || $json.error.status}}",
              "value2": 200,
              "operation": "equal"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "212091aa-32e3-42cb-a464-67fbea72f608",
      "name": "Return Primary API Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        -2128,
        4064
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"provider\": \"primary\",\n  \"status\": {{$json.statusCode}},\n  \"total-data\": {{ $json.body.length }}\n}"
      },
      "typeVersion": 1
    },
    {
      "id": "e24a0ff9-cb2b-452c-8c9e-f7b4e98bde27",
      "name": "Wait Before Retry Attempt",
      "type": "n8n-nodes-base.wait",
      "position": [
        -1760,
        4384
      ],
      "parameters": {
        "unit": "seconds",
        "amount": 5
      },
      "typeVersion": 1
    },
    {
      "id": "ebed15e2-f034-47b7-b188-dd0b5767da82",
      "name": "Retry Primary API Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1552,
        4384
      ],
      "parameters": {
        "url": "={{ $('Extract Request Configuration').item.json.body.primaryApi }}",
        "method": "={{ $('Extract Request Configuration').item.json.body.method }}",
        "options": {
          "timeout": 10000,
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        }
      },
      "typeVersion": 4,
      "continueOnFail": true
    },
    {
      "id": "c2873dca-16a6-4579-998b-edd13db117ea",
      "name": "Check Retry API Success",
      "type": "n8n-nodes-base.if",
      "position": [
        -1344,
        4384
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json.statusCode || $json.error.status }}",
              "value2": 200,
              "operation": "equal"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "23352118-a3e7-4277-bd06-6d4ad7fd8191",
      "name": "Return Retry API Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        -1104,
        4256
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"provider\": \"primary-retry\",\n  \"status\": {{$json.statusCode}},\n  \"total-data\": {{$json.body.length}}\n}"
      },
      "typeVersion": 1
    },
    {
      "id": "c9360bf6-7bea-40cb-9fd7-7b8179107f33",
      "name": "Execute Backup API Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -752,
        4656
      ],
      "parameters": {
        "url": "={{ $('Extract Request Configuration').item.json.body.backupApi }}",
        "method": "={{ $('Extract Request Configuration').item.json.body.method }}",
        "options": {
          "timeout": 10000,
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        }
      },
      "typeVersion": 4,
      "continueOnFail": true
    },
    {
      "id": "f9a00ed5-c8b9-4de6-8b9d-ebea17bc3cbb",
      "name": "Check Backup API Success",
      "type": "n8n-nodes-base.if",
      "position": [
        -528,
        4656
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json.statusCode}}",
              "value2": 200,
              "operation": "equal"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ec201042-6967-4335-a7d7-afb7f5e75a8c",
      "name": "Return Backup API Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        -304,
        4544
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"provider\": \"backup\",\n  \"status\": {{ $json.statusCode }},\n  \"total-data\": {{ $json.body.length }}\n}"
      },
      "typeVersion": 1
    },
    {
      "id": "7d21e038-a84e-4454-83da-2314b735e3cc",
      "name": "Prepare Failure Payload",
      "type": "n8n-nodes-base.set",
      "position": [
        96,
        4832
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "errorMessage",
              "value": "Both primary and backup APIs failed"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "995cf5e6-e965-4be9-92b9-a6bf2cc620d3",
      "name": "Return Final Failure Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        320,
        4832
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={\n  \"success\": false,\n  \"message\": \"{{$json.errorMessage}}\",\n  \"timestamp\": \"{{new Date().toISOString()}}\"\n}"
      },
      "typeVersion": 1
    },
    {
      "id": "d0db855d-b7d3-479f-aa1f-80c23dd6a2b0",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3328,
        3904
      ],
      "parameters": {
        "color": 7,
        "width": 592,
        "height": 512,
        "content": "## Incoming Request Handler\n\nThis section receives incoming API recovery requests through a webhook and extracts the required configuration such as primary API URL, backup API URL, request method, and retry count. It prepares the workflow for dynamic and reusable API recovery handling."
      },
      "typeVersion": 1
    },
    {
      "id": "daf4a667-20da-41e4-bc33-e620e8187330",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2720,
        3904
      ],
      "parameters": {
        "color": 7,
        "width": 848,
        "height": 512,
        "content": "## Primary API Execution\n\nThis section handles the first API execution attempt using the primary provider. The workflow checks whether the API request succeeds or fails and immediately returns the successful response if the primary API is available and working correctly."
      },
      "typeVersion": 1
    },
    {
      "id": "50637029-747e-4dfa-a4d6-192a2bbc6891",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1856,
        4112
      ],
      "parameters": {
        "color": 7,
        "width": 1008,
        "height": 464,
        "content": "## Retry Recovery Mechanism\n\nIf the primary API fails, this section automatically waits for a few seconds and retries the request. The retry mechanism helps recover from temporary issues such as rate limits, server overload, or short network interruptions before switching to the backup provider."
      },
      "typeVersion": 1
    },
    {
      "id": "a24d1f8a-5f3e-4ca8-82ad-c63a6359b13f",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -832,
        4400
      ],
      "parameters": {
        "color": 7,
        "width": 784,
        "height": 464,
        "content": "## Backup API Failover\n\nWhen both the primary request and retry attempt fail, the workflow switches to the backup API provider. This failover system improves reliability and minimizes downtime by ensuring requests can still succeed using an alternative service provider."
      },
      "typeVersion": 1
    },
    {
      "id": "c3e06c63-11c7-4a0c-af65-98b1f8a3ff5e",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -32,
        4624
      ],
      "parameters": {
        "color": 7,
        "width": 608,
        "height": 400,
        "content": "## Failure Handling & Error Response\n\nThis section handles complete API failure scenarios when both primary and backup providers are unavailable. It prepares a structured failure response containing error details and timestamps so client applications can properly detect and manage failed requests."
      },
      "typeVersion": 1
    },
    {
      "id": "0199d971-fc75-4f1e-bd5c-3a35aed612f4",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3328,
        3536
      ],
      "parameters": {
        "width": 640,
        "height": 352,
        "content": "## Workflow Overview & Setup Guide\n\nThis workflow acts as a universal API recovery middleware for handling failed API requests automatically. It receives API configuration dynamically through a webhook, sends the request to the primary API, and checks the response status. If the request fails, the workflow waits briefly and retries the request. If the retry also fails, it automatically switches to the backup API provider. Successful responses are returned immediately using webhook response nodes, while complete failures generate structured error responses. This architecture improves reliability, reduces downtime, and allows the workflow to work with any API dynamically.\n\n**Setup steps**\n\nConfigure the webhook URL and activate the workflow. Pass the primary API URL, backup API URL, HTTP method, and retry count in the webhook request body. Update timeout settings if required and optionally add headers or request body forwarding for POST APIs. Test the workflow using success, failure, timeout, and backup scenarios to verify recovery behavior. This workflow can be extended further with Slack alerts, logging, AI-based root cause analysis, and database monitoring for production systems."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "a134311d-8da9-4f62-b519-817cf875dd55",
  "connections": {
    "API Recovery Webhook": {
      "main": [
        [
          {
            "node": "Extract Request Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Retry API Success": {
      "main": [
        [
          {
            "node": "Return Retry API Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Execute Backup API Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Failure Payload": {
      "main": [
        [
          {
            "node": "Return Final Failure Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Backup API Success": {
      "main": [
        [
          {
            "node": "Return Backup API Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prepare Failure Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Primary API Success": {
      "main": [
        [
          {
            "node": "Return Primary API Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait Before Retry Attempt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retry Primary API Request": {
      "main": [
        [
          {
            "node": "Check Retry API Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait Before Retry Attempt": {
      "main": [
        [
          {
            "node": "Retry Primary API Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Backup API Request": {
      "main": [
        [
          {
            "node": "Check Backup API Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Primary API Request": {
      "main": [
        [
          {
            "node": "Check Primary API Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Request Configuration": {
      "main": [
        [
          {
            "node": "Execute Primary API Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}