{
  "name": "n8n-2-4: Airtable Invoice Workflow",
  "nodes": [
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {}
      },
      "id": "51426866-6193-4f68-9bc2-d82b20f5ec14",
      "name": "Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        1140,
        200
      ],
      "typeVersion": 3,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "aaEQJItVVHDFr2GE",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.cloud.llamaindex.ai/api/parsing/upload",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "accept",
              "value": "application/json"
            },
            {
              "name": "parsing_instruction",
              "value": "Please extract invoice line items: Name, Quantity, Unit Price, Amount "
            }
          ]
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "name": "webhook_url",
              "value": "https://n8n.yyy.io/webhook/0f7f5ebb-8b66-453b-a818-20cc3647c783"
            },
            {
              "parameterType": "formBinaryData",
              "name": "file",
              "inputDataFieldName": "data"
            },
            {
              "name": "disable_ocr",
              "value": "true"
            },
            {
              "name": "disable_image_extraction",
              "value": "True"
            }
          ]
        },
        "options": {}
      },
      "id": "4f2eca1f-b8b2-4317-a98e-7e76e1c2b038",
      "name": "Upload File",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1360,
        200
      ],
      "typeVersion": 4.2,
      "credentials": {
        "httpHeaderAuth": {
          "id": "jofZcwMtHpH8EekK",
          "name": "Llama Index"
        }
      }
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "value": "1H88cqfDSv9EWdDjFM1dBf8cCU8YgsYOU",
          "mode": "list",
          "cachedResultName": "Invoices",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1H88cqfDSv9EWdDjFM1dBf8cCU8YgsYOU"
        },
        "event": "fileCreated",
        "options": {}
      },
      "id": "b5f5a18f-2a1c-4515-b733-1751316fc43e",
      "name": "Google Drive Trigger",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        920,
        200
      ],
      "typeVersion": 1,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "aaEQJItVVHDFr2GE",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "operation": "create",
        "base": {
          "__rl": true,
          "value": "appUKFeuSEoNrQdNz",
          "mode": "list",
          "cachedResultName": "n8n-2-webhook-invoices",
          "cachedResultUrl": "https://airtable.com/appUKFeuSEoNrQdNz"
        },
        "table": {
          "__rl": true,
          "value": "tblpjWqk6Wb2XD7Sg",
          "mode": "list",
          "cachedResultName": "A Table",
          "cachedResultUrl": "https://airtable.com/appUKFeuSEoNrQdNz/tblpjWqk6Wb2XD7Sg"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {},
          "matchingColumns": [],
          "schema": [
            {
              "id": "Description",
              "displayName": "Description",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "qty",
              "displayName": "qty",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "unit_price",
              "displayName": "unit_price",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            },
            {
              "id": "amount",
              "displayName": "amount",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "a799c1e5-9ca8-49ba-b846-9dd7cc54c0d4",
      "name": "Create Invoice",
      "type": "n8n-nodes-base.airtable",
      "position": [
        1240,
        520
      ],
      "typeVersion": 2.1,
      "credentials": {
        "airtableTokenApi": {
          "id": "lKrmtLc3Va7YXnXJ",
          "name": "Sales CRM Airtable"
        }
      }
    },
    {
      "parameters": {
        "operation": "create",
        "base": {
          "__rl": true,
          "value": "appUKFeuSEoNrQdNz",
          "mode": "list",
          "cachedResultName": "n8n-2-webhook-invoices",
          "cachedResultUrl": "https://airtable.com/appUKFeuSEoNrQdNz"
        },
        "table": {
          "__rl": true,
          "value": "tblpjWqk6Wb2XD7Sg",
          "mode": "list",
          "cachedResultName": "A Table",
          "cachedResultUrl": "https://airtable.com/appUKFeuSEoNrQdNz/tblpjWqk6Wb2XD7Sg"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Description": "={{ $json.description }}",
            "qty": "={{ $json.qty }}",
            "unit_price": "={{ parseFloat($json.unit_price.replace('$', '').trim()) }}",
            "amount": "={{ parseFloat($json.amount.replace('$', '').trim()) }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Description",
              "displayName": "Description",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "qty",
              "displayName": "qty",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "unit_price",
              "displayName": "unit_price",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "number",
              "readOnly": false,
              "removed": false
            },
            {
              "id": "amount",
              "displayName": "amount",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": true,
              "display": true,
              "type": "number",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "678a67d5-f359-47f4-9ab7-53404eb42a6d",
      "name": "Create Line Item",
      "type": "n8n-nodes-base.airtable",
      "position": [
        1660,
        520
      ],
      "typeVersion": 2.1,
      "credentials": {
        "airtableTokenApi": {
          "id": "lKrmtLc3Va7YXnXJ",
          "name": "Sales CRM Airtable"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.openai.com/v1/chat/completions",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n \"model\": \"gpt-4o-mini\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": {{ JSON.stringify($('Set Fields').item.json.prompt) }}\n },\n {\n \"role\": \"user\",\n \"content\": {{ JSON.stringify( JSON.stringify($('Webhook').item.json.body.json[0].items) ) }}\n }\n ],\n \"response_format\":{ \"type\": \"json_schema\", \"json_schema\": {{ $('Set Fields').item.json.schema }}\n\n }\n }",
        "options": {}
      },
      "id": "b1271c8e-0b1e-4f93-9af8-b409b27ee40f",
      "name": "OpenAI - Extract Line Items",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1040,
        520
      ],
      "typeVersion": 4.2,
      "credentials": {
        "openAiApi": {
          "id": "qKk5wVYPDLRt63R9",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "dc09a5b4-ff6a-4cee-b87e-35de7336ac05",
              "name": "prompt",
              "type": "string",
              "value": "Please, process parsed data and return only needed."
            },
            {
              "id": "4e0f9af6-517f-42af-9ced-df0e8a7118b0",
              "name": "schema",
              "type": "string",
              "value": "={\n  \"name\": \"generate_schema\",\n  \"description\": \"Generate schema for an array of objects representing items with their descriptions, quantities, unit prices, and amounts.\",\n  \"strict\": true,\n  \"schema\": {\n    \"type\": \"object\",\n    \"required\": [\n      \"items\"\n    ],\n    \"properties\": {\n      \"items\": {\n        \"type\": \"array\",\n        \"description\": \"Array of item objects\",\n        \"items\": {\n          \"type\": \"object\",\n          \"required\": [\n            \"description\",\n            \"qty\",\n            \"unit_price\",\n            \"amount\"\n          ],\n          \"properties\": {\n            \"description\": {\n              \"type\": \"string\",\n              \"description\": \"Description of the item\"\n            },\n            \"qty\": {\n              \"type\": \"string\",\n              \"description\": \"Quantity of the item\"\n            },\n            \"unit_price\": {\n              \"type\": \"string\",\n              \"description\": \"Unit price of the item formatted as a string\"\n            },\n            \"amount\": {\n              \"type\": \"string\",\n              \"description\": \"Total amount for the item formatted as a string\"\n            }\n          },\n          \"additionalProperties\": false\n        }\n      }\n    },\n    \"additionalProperties\": false\n  }\n}"
            }
          ]
        },
        "options": {}
      },
      "id": "275ab9d9-2957-49fb-b06f-daf6951de7c0",
      "name": "Set Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        840,
        520
      ],
      "typeVersion": 3.4
    },
    {
      "parameters": {
        "jsCode": "// Get the input from the \"OpenAI - Extract Line Items\" node\nconst input = $(\"OpenAI - Extract Line Items\").first().json;\n\n// Initialize an array for the output\nconst outputItems = [];\n\n// Navigate to the 'content' field in the choices array\nconst content = input.choices[0]?.message?.content;\n\nif (content) {\n try {\n // Parse the stringified JSON in the 'content' field\n const parsedContent = JSON.parse(content);\n\n // Extract 'items' and add them to the output array\n if (Array.isArray(parsedContent.items)) {\n outputItems.push(...parsedContent.items.map(i => ({ json: i })));\n }\n } catch (error) {\n // Handle any parsing errors\n console.error('Error parsing content:', error);\n }\n}\n\n// Return the extracted items\nreturn outputItems;\n"
      },
      "id": "d48e2066-b3a5-47d8-9303-10701703c3e9",
      "name": "Process Line Items",
      "type": "n8n-nodes-base.code",
      "position": [
        1460,
        520
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "0f7f5ebb-8b66-453b-a818-20cc3647c783",
        "options": {}
      },
      "id": "7226f490-cf7e-441a-993f-71dd2b4eb949",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        640,
        520
      ],
      "webhookId": "0f7f5ebb-8b66-453b-a818-20cc3647c783",
      "typeVersion": 2
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Set Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Fields": {
      "main": [
        [
          {
            "node": "OpenAI - Extract Line Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive": {
      "main": [
        [
          {
            "node": "Upload File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Invoice": {
      "main": [
        [
          {
            "node": "Process Line Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Line Items": {
      "main": [
        [
          {
            "node": "Create Line Item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive Trigger": {
      "main": [
        [
          {
            "node": "Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI - Extract Line Items": {
      "main": [
        [
          {
            "node": "Create Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "2d4526d7e2b7123bfb0e1996caa47744a7651d73096bbc7220a5e9657d119dfc"
  },
  "tags": [],
  "id": "i70hLqKQthQ2U9ki",
  "versionId": "fb90b182-b0ca-4c90-83e4-7802de431ac1"
}
