{
  "name": "Retry only 5xx and 429 (honours Retry-After) - TestedFlows",
  "nodes": [
    {
      "parameters": {},
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://api.example.com/items",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true
            }
          }
        }
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        240,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const r = $input.first().json;\nconst attempt = $runIndex + 1;\nconst status = r.statusCode;\nconst retryAfter = Number((r.headers || {})['retry-after']);\nconst retryable = status === 429 || status >= 500;\nreturn [{ json: {\n  status,\n  attempt,\n  ok: status >= 200 && status < 300,\n  retry: retryable && attempt < 5,\n  waitSeconds: retryAfter > 0 ? retryAfter : Math.min(2 ** attempt, 60),\n  body: r.body,\n} }];"
      },
      "name": "Check response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "59c8a2780c084c94ae0fced5d688fa1d",
              "leftValue": "={{ $json.ok }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "name": "Success?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        680,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "0224d98846674d30b4786d0d045802b4",
              "leftValue": "={{ $json.retry }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "name": "Retry?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        920,
        120
      ]
    },
    {
      "parameters": {
        "amount": "={{ $json.waitSeconds }}",
        "unit": "seconds"
      },
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1160,
        40
      ],
      "webhookId": "f0c1b8e7-597b-4402-9109-2f3be6c9c242"
    },
    {
      "parameters": {
        "errorMessage": "={{ \"Request failed with HTTP \" + $json.status + \" after \" + $json.attempt + \" attempt(s)\" }}"
      },
      "name": "Stop and Error",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        1160,
        240
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { ok: true, attempts: $json.attempt, data: $json.body } }];"
      },
      "name": "Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        920,
        -140
      ]
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Check response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check response": {
      "main": [
        [
          {
            "node": "Success?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Success?": {
      "main": [
        [
          {
            "node": "Result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Retry?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retry?": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Stop and Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}