ZATCA e-invoicing (Saudi Fatoora)
v0.1.1Create, 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.
| Parameter | Type | Description |
|---|---|---|
kind * | string | standard = B2B tax invoice (cleared), simplified = B2C (reported) |
lines * | array | |
number * | string | Your invoice number |
billingReference | string | Original invoice number (credit/debit notes) |
buyer | object | |
counter | integer | ICV; omit to use the local chain state |
issueDate | string | YYYY-MM-DD; defaults to today in KSA |
issueTime | string | HH:mm:ss; defaults to now in KSA |
note | string | |
outputPath | string | Write the XML to this file instead of returning it inline |
paymentMeansCode | string | 10 cash, 30 credit transfer, 42 bank account, 48 card |
previousInvoiceHash | string | PIH; omit to use the local chain state |
reason | string | Reason for a credit/debit note |
seller | object | Omit to use ZATCA_SELLER from the server configuration |
sign | boolean | Sign if credentials are configured |
supplyDate | string | YYYY-MM-DD; required for standard invoices |
type | string |
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.
| Parameter | Type | Description |
|---|---|---|
path | string | Or a path to the XML file |
xml | string | The 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.
| Parameter | Type | Description |
|---|---|---|
qr * | string | Base64 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.
| Parameter | Type | Description |
|---|---|---|
error * | string | A 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.
| Parameter | Type | Description |
|---|---|---|
path | string | |
xml | string |
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.
| Parameter | Type | Description |
|---|---|---|
commonName * | string | Unique name of the EGS unit |
deviceSerial * | string | |
industry * | string | |
invoiceType * | string | 1100 both, 1000 standard only, 0100 simplified only |
keyOutputPath * | string | Where to write the private key (keep it secret) |
location * | string | Branch address / national short address |
modelOrVersion * | string | |
organizationName * | string | |
organizationUnit * | string | Branch name (or 10-digit TIN for VAT group members) |
solutionName * | string | |
vatNumber * | string | |
environment | string |
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.
| Parameter | Type | Description |
|---|---|---|
mode * | string | |
path | string | |
xml | string |