{
  "nodes": [
    {
      "id": "2ca247ef-c2a6-4dfa-8d2e-44b9dcd660da",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -700,
        -260
      ],
      "parameters": {
        "width": 520,
        "height": 1020,
        "content": "## Gmail to DocuPipe to Google Sheets\n\nAutomatically extract structured data from Gmail attachments using [DocuPipe](https://docupipe.ai) AI and save results to Google Sheets.\n\n### Who is this for?\nTeams that receive documents via email (invoices, receipts, contracts) and want structured data automatically extracted and added to a spreadsheet - without manual data entry.\n\n### How it works\nThis template contains two connected flows:\n\n**Scenario 1 - Upload:** Watches Gmail for new emails with attachments, labels them as \"Processing\", uploads the attachment to DocuPipe for extraction, and saves a backup copy to Google Drive.\n\n**Scenario 2 - Process & Save:** When DocuPipe finishes extracting, the webhook fires, results are fetched, processed into a flat row format, enriched with metadata, and appended to your Google Sheet.\n\n### How to set up\n1. Connect your **Gmail** account\n2. Create a Gmail label called **\"DocuPipe - Processing\"** (or customize the label name)\n3. Sign up at [docupipe.ai](https://docupipe.ai), then get your **DocuPipe API key** at [app.docupipe.ai/settings/general](https://app.docupipe.ai/settings/general)\n4. Select an extraction **schema** in the Upload node\n5. Connect your **Google Drive** account and select a backup folder\n6. Connect your **Google Sheets** account and select your spreadsheet\n7. Ensure your sheet's column headers match the schema field names\n8. Activate this workflow\n\n### Requirements\n- A [DocuPipe](https://docupipe.ai) account with an API key\n- A Gmail account\n- A Google Drive folder for backups\n- A Google Sheets spreadsheet\n- An extraction schema configured in DocuPipe\n\n**Note:** Requires the [DocuPipe community node](https://www.npmjs.com/package/n8n-nodes-docupipe). Install via Settings > Community Nodes."
      },
      "typeVersion": 1
    },
    {
      "id": "87eed9b6-3b44-447b-8a66-788822214597",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -60,
        -120
      ],
      "parameters": {
        "color": 7,
        "width": 1200,
        "height": 380,
        "content": "## Scenario 1 - Upload Gmail Attachments to DocuPipe\nNew emails with attachments are detected via Gmail, labeled as \"Processing\", and the attachment is uploaded to DocuPipe for AI-powered extraction. A backup copy of the attachment is also saved to Google Drive. The extraction runs asynchronously - results arrive via webhook in Scenario 2."
      },
      "typeVersion": 1
    },
    {
      "id": "6ce37a48-62c4-4a0c-ba2a-9f0d8cb7d26e",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -60,
        300
      ],
      "parameters": {
        "color": 7,
        "width": 1420,
        "height": 440,
        "content": "## Scenario 2 - Process Extraction Results & Save to Google Sheets\nWhen DocuPipe completes extraction, the webhook triggers this flow. The extracted data is fetched, processed into a flat row-friendly format, enriched with metadata (document name, timestamp), and appended as a new row in your spreadsheet."
      },
      "typeVersion": 1
    },
    {
      "id": "99eb2c64-4cc6-49fd-a740-39a307c4ce96",
      "name": "New Email with Attachment",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        0,
        60
      ],
      "parameters": {
        "filters": {
          "hasAttachments": true
        },
        "options": {
          "downloadAttachments": true
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8080664f-a497-42a3-bbda-7f40e401a0e9",
      "name": "Label as Processing",
      "type": "n8n-nodes-base.gmail",
      "position": [
        240,
        60
      ],
      "parameters": {
        "labelIds": [
          "DocuPipe - Processing"
        ],
        "resource": "message",
        "messageId": "={{ $json.id }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "2c9bebea-a1e7-4326-b04b-9793dac52385",
      "name": "Upload Attachment & Extract Data",
      "type": "n8n-nodes-docupipe.docuPipe",
      "position": [
        480,
        60
      ],
      "parameters": {
        "resource": "extraction",
        "schemaId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "inputMode": "binary",
        "operation": "uploadAndExtract",
        "binaryPropertyName": "attachment_0"
      },
      "credentials": {
        "docuPipeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "d8032eef-623b-4a70-abd8-e7371fc8fa26",
      "name": "Save Attachment to Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        720,
        60
      ],
      "parameters": {
        "name": "={{ $('New Email with Attachment').item.json.subject }}",
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "operation": "upload"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "27348bf8-b8d7-4847-b7cb-64846e5162f0",
      "name": "Extraction Complete",
      "type": "n8n-nodes-docupipe.docuPipeTrigger",
      "position": [
        0,
        480
      ],
      "parameters": {
        "event": "standardization.processed.success"
      },
      "credentials": {
        "docuPipeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7dffada2-a8d6-450e-816d-283e25bdf835",
      "name": "Get Extracted Data",
      "type": "n8n-nodes-docupipe.docuPipe",
      "position": [
        260,
        480
      ],
      "parameters": {
        "resource": "extraction",
        "operation": "getResult",
        "standardizationId": "={{ $json.standardizationId }}"
      },
      "credentials": {
        "docuPipeApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "6a696708-42e0-4950-ab5b-2b852c48e51b",
      "name": "Process Extraction Result",
      "type": "n8n-nodes-base.code",
      "position": [
        520,
        480
      ],
      "parameters": {
        "jsCode": "// Flatten the extraction result into a row-friendly format\n// Nested objects/arrays are converted to JSON strings for spreadsheet compatibility\nconst data = $input.first().json;\nconst result = data.result || {};\nconst output = {};\n\nfor (const [key, value] of Object.entries(result)) {\n  if (Array.isArray(value)) {\n    output[key] = JSON.stringify(value);\n  } else if (typeof value === 'object' && value !== null) {\n    output[key] = JSON.stringify(value);\n  } else {\n    output[key] = value;\n  }\n}\n\nreturn [{ json: output }];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "a9f73223-58ec-44de-9ed7-84816d88c196",
      "name": "Add Metadata",
      "type": "n8n-nodes-base.set",
      "position": [
        780,
        480
      ],
      "parameters": {
        "options": {
          "include": "all"
        },
        "assignments": {
          "assignments": [
            {
              "id": "meta-1",
              "name": "Document Name",
              "type": "string",
              "value": "={{ $('Get Extracted Data').item.json.documentName }}"
            },
            {
              "id": "meta-2",
              "name": "Extracted At",
              "type": "string",
              "value": "={{ $now.toISO() }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "7523e33e-7698-4123-82a7-67d6849a31fe",
      "name": "Append Row to Spreadsheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1040,
        480
      ],
      "parameters": {
        "columns": {
          "mappingMode": "autoMapInputData"
        },
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "byId",
          "value": ""
        },
        "documentId": {
          "__rl": true,
          "mode": "byId",
          "value": ""
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.5
    }
  ],
  "connections": {
    "Add Metadata": {
      "main": [
        [
          {
            "node": "Append Row to Spreadsheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Extracted Data": {
      "main": [
        [
          {
            "node": "Process Extraction Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extraction Complete": {
      "main": [
        [
          {
            "node": "Get Extracted Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Label as Processing": {
      "main": [
        [
          {
            "node": "Upload Attachment & Extract Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New Email with Attachment": {
      "main": [
        [
          {
            "node": "Label as Processing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Extraction Result": {
      "main": [
        [
          {
            "node": "Add Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload Attachment & Extract Data": {
      "main": [
        [
          {
            "node": "Save Attachment to Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}