← Case studies / 01

From purchase order to KSeF — invoice automation with email approval

A B2B invoice workflow connects purchase orders received by email, human approval, KSeF submission, and monthly XML/PDF archives in Google Drive. Separate sales and purchase imports keep documents in the right accounting period.

Client
B2B Services Company
Industry
Professional Services
Published
27 February 2026
Core stack
n8n · KSeF · Drive

The challenge

A recurring purchase order arrived as a PDF attachment. Turning it into an invoice meant copying financial data, checking customer details, creating the document, and filing it for accounting. The original workflow also required preparing a response email manually.

As the process expanded to KSeF, the challenge grew: an operator needed to review the invoice before submission, and both sales and purchase documents needed to reach the correct monthly archive. An invoice issued in May for work completed in April should appear in the April sales folder.

The solution

Four connected n8n workflows handle purchase order intake, approval and KSeF submission, sales imports, and purchase imports. The operator reviews a PDF preview and approves by email; the system handles document preparation, submission, retrieval, and filing around that decision.

  1. 01

    Receive the purchase order

    A Gmail forwarding filter routes selected purchase order emails to a dedicated Resend inbound address. n8n retrieves the message and PDF attachment through Resend and checks the sender before processing it. Intake no longer depends on Gmail OAuth polling.

    The workflow extracts the purchase order number, currency, net amount, tax, gross total, and buyer NIP. It checks required data and the NIP checksum, then retrieves customer details from Firestore.

  2. 02

    Preview and approve by email

    The system saves a pending approval record in Firestore and renders an invoice preview. Resend sends the operator an approval request with the purchase order and preview PDFs attached, so the source document and proposed invoice can be reviewed together.

    The operator replies approved. Cloudflare Email Routing and a Worker pass the reply to n8n, where it is matched to the stored purchase order. This keeps an explicit human decision before KSeF submission.

  3. 03

    Submit the approved invoice to KSeF

    After approval, n8n builds the invoice XML, uses the KSeF helper service for encryption, submits the invoice, and checks its processing status. The workflow then starts the sales import to retrieve and archive documents from KSeF.

    This replaces the earlier banking API and Gmail draft flow. The current process ends with the KSeF documents filed in Drive; it does not prepare or send a final customer email.

  4. 04

    Archive sales and purchase invoices by the relevant dates

    Two import workflows retrieve invoice exports from KSeF, extract the XML documents, render readable PDFs, and upload both formats to Google Drive in year/month folders.

    • Purchase invoices: the scheduled import covers the previous calendar month and selects invoices by issue date (P_1).
    • Sales invoices: the import runs after an approved submission or manually, selecting by sale/completion date (P_6 or P_6A) and accounting for service-period overlap.

    For example, a sales invoice issued in May for a service completed in April belongs in the April archive. Keeping these date rules separate makes the archive more useful for the accountant.

Easier to operate

The latest changes address the practical details that matter after the first successful run:

  • Forwarded email handling and attachment processing preserve the PDFs needed for review.
  • Purchase imports preserve the KSeF invoice number through document processing.
  • The custom @mdab25/ksef-pdf service is pinned to version 1.0.0 for consistent PDF rendering.
  • Status checks use spaced polling, and each workflow has a finite execution timeout so an unfinished upstream operation cannot keep a run waiting indefinitely.
  • Per-node progress saving is disabled to reduce unnecessary database writes.
  • Sanitized workflow templates, deployment notes, and backup instructions support repeatable setup and maintenance.

The result

The workflow brings purchase order intake, review, KSeF submission, and accounting archives into one connected process. Operators can review the proposed invoice from their inbox, while the system reuses extracted data and files the resulting documents automatically.

The main benefits are fewer manual transfers between systems, a recorded approval step, XML originals alongside readable PDFs, and separate date rules for sales and purchases.

The earlier version reported a reduction from around 15 minutes of manual work to under 30 seconds of automated processing. That was a benchmark for the original workflow. The current process includes human approval and KSeF processing, so its total turnaround time depends on the operator and the external service; no updated end-to-end timing is claimed here.

Technology

n8n
Workflow orchestration, API calls, and scheduled imports.
Resend
Inbound email and attachment retrieval, plus approval requests.
Cloudflare Email Routing and Workers
Forwarding approval replies to n8n.
Firestore
Customer data and purchase order approval state.
KSeF API
Invoice submission and sales/purchase exports.
Google Drive API
Monthly XML and PDF archives.
Node.js, Express, Docker, and @mdab25/ksef-pdf
PDF previews, invoice rendering, and encryption helpers.

Which task eats the most hours?

Start a conversation

or write to [email protected]