{
  "name": "Honor Retry-After with Loop Over Items, If and Wait",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "t1"
    },
    {
      "parameters": {
        "jsCode": "return [1,2,3,4,5].map(id => ({ json: { id } }));"
      },
      "name": "Make 5 Items",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        200,
        0
      ],
      "id": "c1"
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        400,
        0
      ],
      "id": "l1"
    },
    {
      "parameters": {
        "url": "=https://api.example.com/items/{{ $('Loop Over Items').first().json.id }}",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true
            }
          }
        }
      },
      "name": "Call API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        600,
        0
      ],
      "id": "h3"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "k1",
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 429,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "name": "Rate Limited?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        800,
        0
      ],
      "id": "i1"
    },
    {
      "parameters": {
        "resume": "timeInterval",
        "amount": "={{ Number($json.headers['retry-after'] ?? 5) }}",
        "unit": "seconds"
      },
      "name": "Wait Retry-After",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1000,
        -100
      ],
      "id": "w1"
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nreturn [{ json: { results: items.length, statuses: items.map(i => i.json.statusCode) } }];"
      },
      "name": "Summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        -200
      ],
      "id": "s1"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Make 5 Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Make 5 Items": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Summary",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Call API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call API": {
      "main": [
        [
          {
            "node": "Rate Limited?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rate Limited?": {
      "main": [
        [
          {
            "node": "Wait Retry-After",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait Retry-After": {
      "main": [
        [
          {
            "node": "Call API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
