ZATCA e-invoicing (Saudi Fatoora)

v0.1.1
io.github.SalmanKhan957/zatca-toolkit Checked 2 days ago Published 19 days ago

Create, sign, validate and submit Saudi ZATCA Phase 2 e-invoices; decode QR codes; explain errors.

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

create_invoice Create a ZATCA e-invoice

Build a ZATCA Phase 2 UBL invoice, credit note or debit note with exact VAT totals, and sign it (hash, ECDSA signature, XAdES, QR) when signing credentials are configured. Keeps the invoice counter (ICV) and previous-invoice-hash (PIH) chain in a local state file unless you pass them explicitly.

ParameterTypeDescription
kind *stringstandard = B2B tax invoice (cleared), simplified = B2C (reported)
lines *array
number *stringYour invoice number
billingReferencestringOriginal invoice number (credit/debit notes)
buyerobject
counterintegerICV; omit to use the local chain state
issueDatestringYYYY-MM-DD; defaults to today in KSA
issueTimestringHH:mm:ss; defaults to now in KSA
notestring
outputPathstringWrite the XML to this file instead of returning it inline
paymentMeansCodestring10 cash, 30 credit transfer, 42 bank account, 48 card
previousInvoiceHashstringPIH; omit to use the local chain state
reasonstringReason for a credit/debit note
sellerobjectOmit to use ZATCA_SELLER from the server configuration
signbooleanSign if credentials are configured
supplyDatestringYYYY-MM-DD; required for standard invoices
typestring

validate_invoice Validate a ZATCA e-invoice

Check a ZATCA XML invoice offline: common BR-KSA/EN16931 rules, totals arithmetic, and for signed documents the invoice hash, ECDSA signature, XAdES digests and every QR tag. Explains each problem in English and Arabic with a fix.

ParameterTypeDescription
pathstringOr a path to the XML file
xmlstringThe XML text

decode_qr Decode a ZATCA QR code

Decode the base64 TLV payload of a ZATCA e-invoice QR code into its fields (seller, VAT number, timestamp, totals, hash, signature, key). Flags non-BER lengths that ZATCA rejects.

ParameterTypeDescription
qr *stringBase64 QR payload (the text inside the QR image)

explain_error Explain a ZATCA error

Explain ZATCA rule codes or API error messages (e.g. "BR-KSA-37", "invalid-invoice-hash", or a pasted API response) in plain English and Arabic, with the fix.

ParameterTypeDescription
error *stringA code, a message, or a whole API response

compute_invoice_hash Compute a ZATCA invoice hash

Compute the ZATCA invoice hash (base64 SHA-256 of the canonicalized XML without UBLExtensions, Signature and QR). Use it to debug "invalid-invoice-hash" or as the next PIH.

ParameterTypeDescription
pathstring
xmlstring

generate_csr Generate a ZATCA key pair and CSR

Create a secp256k1 private key (written to a file, never shown) and the CSR needed to onboard an e-invoicing unit (EGS) with ZATCA. Returns the CSR and the exact value to send to POST /compliance.

ParameterTypeDescription
commonName *stringUnique name of the EGS unit
deviceSerial *string
industry *string
invoiceType *string1100 both, 1000 standard only, 0100 simplified only
keyOutputPath *stringWhere to write the private key (keep it secret)
location *stringBranch address / national short address
modelOrVersion *string
organizationName *string
organizationUnit *stringBranch name (or 10-digit TIN for VAT group members)
solutionName *string
vatNumber *string
environmentstring

submit_invoice Submit an invoice to ZATCA

Send a signed invoice to ZATCA's Fatoora API using the configured CSID: "report" (simplified/B2C), "clear" (standard/B2B, returns the stamped invoice) or "compliance" (onboarding checks). Uses ZATCA_ENV (sandbox by default). Production submissions are real tax filings and require ZATCA_ALLOW_PRODUCTION=1.

ParameterTypeDescription
mode *string
pathstring
xmlstring