Open Source · Library Published

cfdi-go: Read, Verify and Validate Mexican CFDI Invoices in Go

Zero-dependency Go library that reads CFDI 3.3, 4.0 and withholding invoices, builds the original string, verifies seals and validates against SAT schemas and rules.

Role
Design & Development
Year
2026
Published
cfdi-go cover: the go get github.com/OrtaMarco/cfdi-go command, a window with cfdi.Parse and cfdi.Verificar, seal and original string verified, and CFDI 3.3, CFDI 4.0, Retenciones and TFD chips.

cfdi-go is a Go library that reads, verifies and validates Mexican electronic invoices (CFDI, the XML format mandated by the SAT, Mexico’s tax authority): CFDI 3.3 and 4.0 and withholding CFDI (Retenciones). It turns the XML into Go structs and versioned-schema JSON, builds the original string (cadena original), checks the digital seals and validates against the SAT schemas and rules. Zero dependencies: standard library only. MIT licensed.

go get github.com/OrtaMarco/cfdi-go@latest

Why it exists

A system that receives invoices (an ERP, a supplier portal, an accounting importer) has to read every CFDI without breaking. The spec changes per version, each complement adds its own nodes, and some details are easy to get wrong: the original string, the seal, rounding. Almost everything out there is built to issue and stamp invoices, not to read them rigorously. This library only reads, and it is tested against independent oracles. It is the Go counterpart of the Mexican tax MCP server, which does the same for an AI assistant.

Usage

import "github.com/OrtaMarco/cfdi-go" // the package is named cfdi

c, err := cfdi.Parse(f) // err only if it is not XML or the root is not a cfd/3 or cfd/4 Comprobante
if err != nil {
    log.Println(cfdi.ClaseDe(err)) // no_es_xml | xml_mal_formado | no_es_comprobante | lectura
}
for _, a := range c.Advertencias {
    fmt.Println(a.Codigo, a.Ruta, a.Mensaje)
}
fmt.Println(c.UUID(), c.Total.Original()) // "ABCDEF01-…", "1640.00"

v := cfdi.Verificar(xml, cfdi.CertificadosEn(certsSAT...)) // issuer seal and stamp seal
val := cfdi.Validar(xml, c)                                // XSD + rules: val.Estado, val.Esquema, val.Reglas

And from the terminal, with go install github.com/OrtaMarco/cfdi-go/cmd/cfdi@latest:

cfdi parse factura.xml
cfdi verificar factura.xml
cfdi validar factura.xml
cfdi lote carpeta/ otro.zip -o salida.jsonl -resumen resumen.json -validar

The API and the documentation are in Spanish, like the SAT specification they follow.

What is in the box

  • Parsing: CFDI 3.3 and 4.0 of every type (income, expense, payment, payroll, transfer), with PUE and PPD, related CFDI and per-item and total taxes; withholding CFDI 1.0 and 2.0. Versions 3.2 and 3.0 are read partially.
  • Complements: Timbre Fiscal Digital 1.0 and 1.1, Pagos 1.0 and 2.0, Nómina 1.2, Carta Porte, Comercio Exterior, Impuestos Locales and dozens more from the SAT catalog. A complement it cannot read is flagged as unsupported, never silently dropped.
  • Coded warnings: every parsing finding carries a stable code, the node path and a message, so you decide what to do with each one.
  • Original string and seals: builds the original string with the SAT rules and verifies the issuer seal and, with the SAT certificates, the stamp seal.
  • Validation: against the official XSDs (valid, invalid, or incomplete when a complement has no schema) and arithmetic and consistency rules, with one code per finding.
  • The cfdi command: parse, verificar and validar on a file, and lote over folders and ZIPs (ZIPs inside ZIPs too), in parallel with bounded memory. Its summary is safe to share: no tax IDs, names, amounts or paths.
  • Tested against independent oracles: expected original strings are generated by libxslt with the SAT XSLT sheets, schemas are cross-checked with libxml2 and the official XSDs, and there are public fixtures from community projects (personal data replaced with SAT test identities) plus deliberately invalid cases, one per rule.

What it does not do

  • It does not stamp, cancel, generate or sign CFDI. Issuing needs a PAC (authorized certification provider) and your own seal certificates.
  • Passing Validar does not mean the SAT accepts it. It covers what can be checked offline: it does not query the SAT, download certificates or check revocation or chain of trust, and it does not validate against catalogs that are not in the XSDs. To check a CFDI’s status with the SAT there is the Mexican tax MCP server.
  • The API may change: it is 0.x until 1.0.0.

Tech stack

  • Go 1.24+, standard library only
  • libxslt and libxml2 as test oracles, never at runtime
  • GitHub Actions for CI

Repository

View on GitHub → · Docs on pkg.go.dev →

Goal

Make reading a SAT invoice in Go boring: a library that does not break on the complement it had never seen, reports what it found with a code instead of loose text, and whose results can be checked against the SAT’s own sheets.

07 / Contact

Something similar? Write to me.

Two lines are enough: what you need and what you already have. I'll tell you whether this kind of project fits and what the first step would be.