Quick overview
This workflow polls Gmail for emails with XML attachments, extracts Brazilian NF-e invoice data, validates the 44-digit access key against the XML content, and upserts one row per invoice into Google Sheets while sending invalid or conflicting records to a separate Review tab.
How it works
- Triggers every 5 minutes by polling Gmail for messages that have
.xml attachments and downloads the attachments.
- Splits each email into one item per attachment and keeps only files whose filename ends with
.xml.
- Reads each XML file, converts it to JSON, and filters out non-NF-e documents.
- Validates the NF-e access key (including the mod-11 check digit) and checks that it matches the issuer and protocol details in the XML to assign a status.
- Writes invalid or mismatched keys to a Google Sheets
Review tab with the reason, message metadata, and invoice identifiers.
- Sorts valid items to prefer Authorized invoices, de-duplicates by access key, checks existing Google Sheets
NF-e rows for duplicate keys, and sends conflicts to Review.
- Upserts a single row per Access Key into the Google Sheets
NF-e tab in RAW format. As long as runs do not overlap, an existing Authorized row is never overwritten by a copy without protocol.
Setup
- Create a Google Sheets document with
NF-e and Review tabs and add the expected header columns (including Access Key) in row 1.
- Add Gmail credentials (for Gmail Trigger) and Google Sheets credentials (for reading and writing rows).
- Replace
YOUR_SHEET_ID with your Google Sheets document ID in all Google Sheets nodes.
- Ensure the Google Sheets operations use RAW cell format so 44-digit access keys and CNPJ/CPF values are not auto-formatted.
- Optionally adjust the Gmail search query and polling interval, then activate the workflow.
Requirements
- Gmail account that receives NF-e XML attachments, and a Google Sheets account (OAuth2 credentials for both).
Customization
- Change the Gmail search query (for example, a label such as label:nfe) or add columns by extending the "Build sheet row" node.
Additional info
Limitations: the workflow reads the authorization protocol stored in the XML; it does not query SEFAZ or verify the digital signature. ZIP attachments are not opened, and attachments without a file name are skipped. Overlapping executions can duplicate or downgrade a row, so keep the Gmail polling interval longer than one run.