Work in progress
This library will be used to read, write and verify electronic invoices (XML) which conform to the EN 16931 standard.
go get github.com/speedata/einvoice
The package documentation describes the API and has runnable examples for reading, validating, building and converting invoices.
invoice, err := einvoice.ParseXMLFile(filename)
if err != nil {
...
}
// now invoice contains all the information from the XML file
// check for validation violations
err = invoice.Validate()
if err != nil {
var valErr *einvoice.ValidationError
if errors.As(err, &valErr) {
for _, v := range valErr.Violations() {
fmt.Printf("Error: %s - %s\n", v.Rule.Code, v.Text)
}
for _, w := range valErr.Warnings() {
fmt.Printf("Warning: %s - %s\n", w.Rule.Code, w.Text)
}
}
} else {
// Validation passed - check for recommendations
for _, w := range invoice.Warnings() {
fmt.Printf("Recommendation: %s - %s\n", w.Rule.Code, w.Text)
}
}Building and validating an invoice programmatically:
import "errors"
func buildInvoice() error {
inv := &einvoice.Invoice{
InvoiceNumber: "INV-001",
// ... set all required fields
}
// Validate before writing
if err := inv.Validate(); err != nil {
var valErr *einvoice.ValidationError
if errors.As(err, &valErr) {
for _, v := range valErr.Violations() {
fmt.Printf("Rule %s: %s\n", v.Rule, v.Text)
}
}
return err
}
return inv.Write(os.Stdout)
}Round-trip: parsing and writing back:
func dothings() error {
inv, err := einvoice.ParseXMLFile("...")
if err != nil {
return err
}
// Write back in the same format (CII or UBL)
return inv.Write(os.Stdout)
}The Validate() method automatically detects and applies the appropriate validation rules:
- EN 16931 Core Rules: Always validated for all invoices
- PEPPOL BIS Billing 3.0: Auto-detected based on specification identifier (BT-24)
- Country-Specific Rules: Auto-detected based on seller country (future: DK, IT, NL, NO, SE)
Example of a PEPPOL invoice being automatically validated:
inv := &einvoice.Invoice{
GuidelineSpecifiedDocumentContextParameter: einvoice.SpecPEPPOLBilling30, // BT-24
BPSpecifiedDocumentContextParameter: einvoice.BPPEPPOLBilling01, // BT-23
// ... other fields
}
// Automatically validates both EN 16931 AND PEPPOL rules
if err := inv.Validate(); err != nil {
// Handle validation errors
}No need to call separate validation methods - Validate() handles everything automatically!
The identifiers have their own string types, so mixing them up is a compile
error: SpecificationID (BT-24, constants Spec*), BusinessProcessID
(BT-23, constants BP*) and EASCode (scheme of BT-34/BT-49, constants
EAS*). String literals can still be assigned directly; values held in a
string variable need a conversion such as
einvoice.SpecificationID(profile).
The validation framework distinguishes between errors (hard requirements) and warnings (recommendations):
- Errors: Violations of "must" requirements cause
Validate()to return an error - Warnings: Violations of "should" recommendations don't fail validation but are reported
err := invoice.Validate()
if err == nil {
// Validation passed - but check for recommendations
if invoice.HasWarnings() {
for _, w := range invoice.Warnings() {
fmt.Printf("Recommendation: %s - %s\n", w.Rule.Code, w.Text)
}
}
}The warning infrastructure is available for future "should" (German: "soll") rules from various CIUS specifications.
There is a dedicated example in the documentation.
A CLI tool is available for validating invoices from the command line.
go install github.com/speedata/einvoice/cmd/einvoice@latestValidate an invoice and display violations in human-readable format:
einvoice validate invoice.xmlOutput validation results as JSON (useful for automation and CI/CD):
einvoice validate --format json invoice.xmlWarnings ("should" rules) are listed after the violations, or in the
warnings array of the JSON output. They do not make an invoice invalid.
One example is BR-USER-05, reported when an invoice line net amount does not
match quantity × price ÷ base quantity ± line allowances/charges:
✓ Invoice 906 is valid
⚠ 1 warning(s):
- BR-USER-05 (BT-131): Line 1: Invoice line net amount 9.17 does not match calculated 9.13 (qty 0.083 × price 110 / baseQty 1 + charges 0 - allowances 0)
For a ZUGFeRD/Factur-X PDF, the CLI also checks how the XML is embedded,
following the BR-HYBRID-* rules of Factur-X 1.07.2 / ZUGFeRD 2.3.2: the XMP
metadata (namespace, fx:DocumentType, fx:DocumentFileName,
fx:Version, fx:ConformanceLevel matching the profile of the XML, the
PDF/A part), the name of the embedded file, its /AFRelationship and its
entry in the catalog's /AF array:
einvoice validate invoice.pdf0- Invoice is valid (no violations; warnings may still be reported)1- Error occurred (file not found, parse error, etc.)2- Invoice has validation violations
These exit codes make it easy to integrate the validator into shell scripts and CI/CD pipelines:
if einvoice validate invoice.xml; then
echo "Invoice is valid!"
else
echo "Validation failed"
fi- Reading and writing of EN 16931 invoices in both formats:
- ZUGFeRD/Factur-X (CII format)
- UBL 2.1 (Invoice and CreditNote)
- Intelligent validation with auto-detection:
- EN 16931 Core Rules: BR-1 to BR-65, BR-CO-, BR-DEC-
- EN 16931 Code Lists: BR-CL-* (parsed invoices)
- EN 16931 Syntax Binding: CII-SR-, CII-DT-, UBL-SR-, UBL-CR-, UBL-DT-* (parsed invoices)
- VAT Category Rules: BR-S-, BR-AE-, BR-E-, BR-Z-, BR-G-, BR-IC-, BR-IG-, BR-IP-, BR-O-*
- PEPPOL BIS Billing 3.0: Auto-detected and validated (PEPPOL-EN16931-R*)
- Single
Validate()method handles all rule sets automatically
- XML output for all ZUGFeRD profiles (Minimum, BasicWL, Basic, EN16931, Extended, XRechnung)
- UBL 2.1 Invoice and CreditNote output with full EN 16931 compliance
- Profile detection based on specification identifier URN (BT-24)
- Format auto-detection when parsing (automatically recognizes CII or UBL)
- Round-trip support: parse and write back in the same format
We welcome contributions! Please see CONTRIBUTING.md for:
- Development setup and prerequisites
- Running tests and checking coverage
- Code style and conventions
- Pull request process
- How to work with test fixtures
Test fixtures are organized by profile and format in the testdata/ directory. See testdata/README.md for:
- Directory structure and organization
- Fixture sources and provenance (from official EN 16931 and PEPPOL test suites)
- How to add new fixtures
- Usage patterns in tests