{
  "name": "Process multiple requests in FIFO using OpenAI Batch API and Supabase/Postgres",
  "nodes": [
    {
      "id": "9d2458b4-fea4-4a3d-a1a9-c9a6d2849560",
      "name": "Call Files API",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -752,
        -160
      ]
    },
    {
      "id": "974a2186-4480-4cc2-bd82-391a9a0ceca7",
      "name": "Call Batch API",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -528,
        -160
      ]
    },
    {
      "id": "208f7358-6098-4137-b067-07238efc9d08",
      "name": "Start (mock data)",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -1200,
        -160
      ]
    },
    {
      "id": "a93d70c7-923d-4219-9df4-00fde597a542",
      "name": "If status = completed",
      "type": "n8n-nodes-base.if",
      "position": [
        -528,
        160
      ]
    },
    {
      "id": "09f854d5-43bd-47b0-825b-220eff998eaa",
      "name": "Convert to batch requests in .jsonl",
      "type": "n8n-nodes-base.code",
      "position": [
        -976,
        -160
      ]
    },
    {
      "id": "fe5c9d43-a1ee-4f1b-bd4b-988fe0194d50",
      "name": ".jsonl to base64",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        -80,
        64
      ]
    },
    {
      "id": "6e372e85-930f-4a71-96e0-5f3a5668f513",
      "name": "Decode base64",
      "type": "n8n-nodes-base.code",
      "position": [
        144,
        64
      ]
    },
    {
      "id": "de97fb6a-2795-41f2-a2da-67aac90bd87e",
      "name": "Call Batch API to retrieve batch object",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -752,
        160
      ]
    },
    {
      "id": "6ac10207-84af-4386-96ee-af15e74e71fe",
      "name": "Download .jsonl result",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -304,
        64
      ]
    },
    {
      "id": "9df69ab5-13c5-4a4f-8969-ed6bd4a640e0",
      "name": "Cron Job (5 mins)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1200,
        160
      ]
    },
    {
      "id": "de0aebc0-d494-406c-8d2e-d3eb95e14930",
      "name": "Create a row in batch table",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -304,
        -160
      ]
    },
    {
      "id": "6b78a91e-aeec-4777-800a-5b9751d88365",
      "name": "Get the earliest uncompleted batch",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -976,
        160
      ]
    },
    {
      "id": "7477a084-cf40-4922-bf1a-edacd3e2f87c",
      "name": "Update status",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -304,
        256
      ]
    },
    {
      "id": "433a549e-b052-451b-9eba-309ceaea800c",
      "name": "Update status and result",
      "type": "n8n-nodes-base.supabase",
      "position": [
        368,
        64
      ]
    },
    {
      "id": "602d3950-5d6d-4707-b51d-3e5f2550be8e",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1792,
        -960
      ],
      "parameters": {
        "width": 640,
        "height": 688,
        "content": "# OpenAI Batch API (FIFO with Supabase / Postgres)\n\n### How it works\nThis workflow uses OpenAI's **Batch API** to process multiple prompts at reduced cost, with Supabase as a persistent queue for trac"
      }
    },
    {
      "id": "16cd7a56-a24b-4e68-81cb-947eb446f75b",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1792,
        -160
      ],
      "parameters": {
        "width": 480,
        "height": 112,
        "content": "## 📤 Phase 1: Submit Batch\nConverts inputs to `.jsonl`, uploads to OpenAI, creates a batch job, and records it in `openai_batches`."
      }
    },
    {
      "id": "46ddb6e7-1571-42a0-b9c8-bac07c947224",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1792,
        160
      ],
      "parameters": {
        "width": 480,
        "height": 112,
        "content": "## 📥 Phase 2: Poll & Retrieve (cron)\nQueries `openai_batches` for uncompleted batches, checks OpenAI status, downloads + decodes results and stores them back if completed."
      }
    },
    {
      "id": "b2374aa6-7301-4c46-9465-bd596b95fe8f",
      "name": "Submission done",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -80,
        -160
      ]
    },
    {
      "id": "e7977e36-2dbb-47d5-afe5-eaa2165d9f34",
      "name": "Retrieval done",
      "type": "n8n-nodes-base.noOp",
      "position": [
        592,
        64
      ]
    }
  ],
  "connections": {
    "Decode base64": {
      "main": [
        [
          {
            "node": "Update status and result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call Batch API": {
      "main": [
        [
          {
            "node": "Create a row in batch table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call Files API": {
      "main": [
        [
          {
            "node": "Call Batch API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    ".jsonl to base64": {
      "main": [
        [
          {
            "node": "Decode base64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cron Job (5 mins)": {
      "main": [
        [
          {
            "node": "Get the earliest uncompleted batch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start (mock data)": {
      "main": [
        [
          {
            "node": "Convert to batch requests in .jsonl",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If status = completed": {
      "main": [
        [
          {
            "node": "Download .jsonl result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download .jsonl result": {
      "main": [
        [
          {
            "node": ".jsonl to base64",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update status and result": {
      "main": [
        [
          {
            "node": "Retrieval done",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a row in batch table": {
      "main": [
        [
          {
            "node": "Submission done",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get the earliest uncompleted batch": {
      "main": [
        [
          {
            "node": "Call Batch API to retrieve batch object",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to batch requests in .jsonl": {
      "main": [
        [
          {
            "node": "Call Files API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call Batch API to retrieve batch object": {
      "main": [
        [
          {
            "node": "If status = completed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}