n8n Tutorial

Automate Invoice Generation with n8n and Stripe

Automatically generate branded invoice PDFs when a Stripe payment succeeds. Email to your customer and save to Google Drive.

Vincent Ventalon
Vincent Ventalon
· Updated
TemplateFox

About the TemplateFox Node

The official TemplateFox community node for n8n lets you generate PDFs from templates with dynamic data. Works synchronously — returns the PDF URL immediately. Compatible with n8n AI Agents.

Why automate invoices?

A customer pays through Stripe Checkout. Now you need to generate an invoice, email it to them, and file it somewhere. For one payment? Quick. For hundreds per month? That's a full-time job.

This workflow handles the entire chain: Stripe payment succeeds → invoice PDF is generated with your branding → customer gets it by email → a copy lands in Google Drive for bookkeeping. Zero manual work.

Here's what we're building: a workflow that triggers on every Stripe checkout, fetches the line items, formats an invoice, generates a branded PDF, and delivers it to the customer and your Drive folder.

1

Create your invoice template

1.1 — Choose a template

Start with one of our pre-built invoice templates or create your own:

  1. Go to Templates
  2. Filter by Invoice category
  3. Choose a template that matches your style
  4. Click Use Template to add it to your dashboard
Professional Corporate Invoice template in the visual editor

The template includes a QR code for payment. Click it to configure the URL.

QR code selected showing properties panel

1.2 — Understand the template structure

The template uses two types of fields: static ones you set once in the template editor, and dynamic ones filled by Stripe data at runtime.

Set in the template editor

These never change — type them directly into the template design as plain text, not as variables.

  • Company name, logo
  • Company address
  • Bank details (name, IBAN, BIC)
  • VAT number, registration
  • Payment terms, footer notes

Dynamic variables (from Stripe)

These change per invoice — use template variables that the workflow fills at runtime.

  • client_company (customer name)
  • client_address_1, client_email
  • items[] (line items array)
  • invoice_number, invoice_date, due_date
  • subtotal, tax, total

Why hardcode in the template? Your company name, address, and bank details never change between invoices. Setting them as plain text in the template editor means less data to pass from n8n, fewer variables to maintain, and no risk of typos in the Code node.

Learn more: Expressions · Data Binding · Tables

1.3 — Test with sample data

Use the Preview tab to test your template with sample JSON before connecting n8n. This JSON structure is exactly what the Code node will output.

Invoice template preview with sample data in TemplateFox editor

Tip: Copy the JSON structure from the Preview tab. You'll use it as a reference when writing the Code node.

2

Build the n8n workflow

The complete workflow: Stripe payment → fetch line items → format data → generate PDF → email customer + save to Drive.

Stripe Trigger → Get Line Items → Format Data → TemplateFox →
Gmail Google Drive
Complete n8n workflow: Stripe Trigger → Get Line Items → Format Invoice Data → TemplateFox → Email Invoice + Save to Google Drive

2.1 — Set up Stripe trigger

Connect your Stripe account to n8n, then add the Stripe Trigger node to listen for payments.

  1. Go to Stripe Dashboard → API Keys and copy your Secret key — it starts with sk_live_ or sk_test_
  2. In n8n, go to Credentials → Add Credential → Stripe API
  3. Paste the secret key in the Secret Key field — leave Signing Secret empty
  4. Add a Stripe Trigger node to your workflow
  5. Select the checkout.session.completed event
  6. Activate the workflow — n8n registers the webhook in Stripe automatically
Stripe API credentials configured in n8n with connection tested successfully

Other events: Use invoice.payment_succeeded for subscriptions or charge.succeeded for direct charges.

2.2 — Fetch line items from Stripe

Stripe doesn't include line items in the webhook payload — you need a separate API call to get them. Add an HTTP Request node:

  1. Add an HTTP Request node
  2. Set method to GET
  3. URL: https://api.stripe.com/v1/checkout/sessions/{{ $json.data.object.id }}/line_items
  4. Authentication: Predefined Credential Type → Stripe API

This returns the product names, quantities, and amounts for each item in the checkout.

2.3 — Format invoice data

A Code node extracts the Stripe data and formats it to match your template variables. No company details here — those are already in the template itself.

// Extract Stripe data
const session = $('Stripe Trigger').first().json.data.object;
const customer = session.customer_details;
const lineItems = $input.first().json.data;
// Format line items for the template
const items = lineItems.map(item => ({
item_description: item.description,
item_quantity: item.quantity,
item_price: (item.amount_total / 100).toFixed(2)
}));
// Generate invoice number from date + session ID
const now = new Date();
const invoiceNumber = `INV-${now.getFullYear()}-${session.id.slice(-6)}`;
// Due date: Net 30
const dueDate = new Date(now);
dueDate.setDate(dueDate.getDate() + 30);
// Return formatted data
return [{ json: {
client_company: customer.name,
client_email: customer.email,
client_address_1: customer.address?.line1,
invoice_number: invoiceNumber,
invoice_date: now.toISOString().split('T')[0],
due_date: dueDate.toISOString().split('T')[0],
items,
total: (session.amount_total / 100).toFixed(2),
tax: (session.total_details?.amount_tax / 100).toFixed(2)
}}];

This is the full Code node. It extracts everything from Stripe: customer info, line items, amounts, and generates an invoice number and due date. Adapt the variable names to match your template fields.

No company details here. Your company name, address, bank info, and logo are set directly in the template editor — they never change between invoices, so there's no reason to pass them from n8n.

2.4 — Generate the PDF

Add the TemplateFox node to generate the invoice PDF:

  1. Add a TemplateFox node
  2. Add your TemplateFox API key in Credentials → Add Credential → TemplateFox API
  3. Select Generate PDF operation
  4. Choose your invoice template from the dropdown
  5. Keep Data Input Mode on Use Template Fields — the node loads your template's fields automatically
  6. Map each field to the matching output from the Code node (e.g. client_company → {{ $json.client_company }})
TemplateFox API credentials configured in n8n with connection tested successfully
TemplateFox node showing template fields loaded automatically after selecting a template

TemplateFox returns a signed URL to the generated PDF. This URL is used in the next step to email and store the invoice.

2.5 — Email + save to Google Drive

After TemplateFox generates the PDF, the workflow splits into two parallel branches:

Gmail — Email to customer

  • Recipient from Stripe customer email
  • Subject with invoice number
  • Body with PDF download link, amount, due date

Google Drive — Archive

  • Downloads the PDF from TemplateFox URL
  • Uploads to your Invoices folder
  • File named with invoice number

Done. Stripe payment succeeds → invoice PDF generated → customer gets it by email → copy saved in Drive. Fully automatic.

Same workflow, other platforms

Not your automation tool? The same setup works elsewhere:

Frequently asked questions

Which Stripe events should I use?

The workflow uses checkout.session.completed by default, which covers Stripe Checkout payments. You can also use invoice.payment_succeeded for subscription renewals, or charge.succeeded for direct charges.

Can I use this without Google Drive?

Yes. The Google Drive branch is optional — just remove the Download PDF and Save to Google Drive nodes. The email branch works independently. You can also replace Drive with Dropbox, OneDrive, S3, or any storage node.

How are invoice numbers generated?

The Code node generates invoice numbers in INV-YYYY-MMDD-XXXXXX format using the date and the last 6 characters of the Stripe session ID. Edit the format in the Code node to match your numbering scheme.

Does this handle taxes and discounts?

Yes. The workflow reads total_details.amount_tax from the Stripe session for tax amounts. Discounts applied in Stripe Checkout are already reflected in the line item totals. You can add a separate discount field in the Code node if needed.

What if a payment fails or is refunded?

The workflow only triggers on successful checkouts. For refunds, you can add a separate Stripe Trigger node listening to charge.refunded and generate a credit note PDF instead.

What does this automation cost?

n8n self-hosted is free. TemplateFox offers 60 free PDFs per month. Stripe webhooks are free. Google Drive and Gmail are free for personal accounts. View pricing.

Automate your PDF generation

Complete documentation, no-code integrations, and a powerful API to help you generate PDFs at scale. Let us handle the boring stuff.