Reviewed

ZATCA Invoice Hash Chain and Counter: ICV, PIH and UUID Explained

How the UUID, invoice counter and previous invoice hash link every Phase 2 e-invoice, and what happens after rejections and across branches

Lavender illustration: three invoice cards linked by chains, with counter boxes, hash patterns, a lock and a shield badge

Every Phase 2 ZATCA e-invoice carries three small fields that make the whole sequence tamper-evident: a UUID, an invoice counter value (ICV) and the previous invoice hash (PIH). Together they turn each EGS unit's invoices into a numbered, hash-linked chain, so a deleted or swapped invoice shows up. They are also behind many hard-to-diagnose rejections and audit questions, because they depend on the order in which your system generates documents, not on the data in any one invoice. This guide explains what each field is, where it sits in the XML, how the hash is calculated, how the chain behaves after rejections and across branches, and what to test in your ERP.

The Short Answer

  • UUID (KSA-1) is a unique identifier that the issuing machine generates for each invoice or note. Rule BR-KSA-03 in ZATCA's XML Implementation Standard says it may contain only letters, digits and dashes.
  • ICV (KSA-16) is a numeric counter. The E-Invoicing Implementation Resolution requires it to be tamper-resistant, to increment for every invoice or associated note, and never to be reset or reformatted. Rule BR-KSA-34 says it contains only digits.
  • PIH (KSA-13) is the base64-encoded SHA-256 hash of the previous invoice. Rule BR-KSA-61 says it must exist on every invoice, and the first invoice uses a fixed starting value published in ZATCA's data dictionary.
  • One chain per unit. The Resolution says an e-invoice solution must not generate more than one sequence of invoices and notes through each unit, so each onboarded EGS and CSID has its own counter and its own hash chain.
  • Rejected documents stay in the chain. ZATCA's FAQ says a rejected document keeps its hash and counter, the fixed document is submitted as a new one with its own UUID, ICV and hash, and the next PIH points to the last document generated, not the last one accepted.
  • They are mandatory. Annex 2 of the Resolution marks the UUID, the previous document hash and the tamper-resistant counter as mandatory, enforced from 1 January 2023 for each target group according to its wave, and not required to be visible on the printed invoice.

The Three Identifiers at a Glance

The table puts the three fields next to the ordinary invoice number so you can see how they differ. Rule numbers come from the XML Implementation Standard (Version 1.2), and the term IDs and examples come from ZATCA's E-Invoice Data Dictionary (19 May 2023).

FieldKSA termWhere it sits in the UBL XMLKey rulesDictionary example
Invoice number (IRN)BT-1cbc:IDA unique, sequential number issued by the taxpayer (Annex 2, item 2.1)2021/02/12/1230
UUIDKSA-1cbc:UUIDBR-KSA-03: unique identifier given by the issuing machine; letters, digits and dashes only3cf5ee18-ee25-44ea-a444-2c37ba7f28be
Invoice counter value (ICV)KSA-16An AdditionalDocumentReference whose cbc:ID is ICV; the counter value is held in its cbc:UUID elementBR-KSA-33: every invoice must have one. BR-KSA-34: digits only46531
Previous invoice hash (PIH)KSA-13An AdditionalDocumentReference whose cbc:ID is PIH; the hash is in cac:Attachment / cbc:EmbeddedDocumentBinaryObject with mimeCode text/plainBR-KSA-26: base64-encoded SHA-256. BR-KSA-61: must existThe fixed first-invoice value, shown below

Two notes on that table. First, the data dictionary spells the parent element of the counter as "AdditionalDocumentReferene" (sic), so copy the structure from ZATCA's sample XML rather than from the typo. Second, the invoice number and the counter are different things: ZATCA's Detailed Guideline answers a question on the difference by treating the Invoice Reference Number as the invoice sequence number, and then describing the counter separately as a tamper-resistant value that increments for every invoice or note, with the examples "1, 2, 3, 4, 5". Your ERP may keep both in step, but ZATCA treats them as separate fields with separate rules.

Why ZATCA Built a Hash Chain

The Resolution explains the purpose directly. A compliant solution must generate a hash for each invoice or note, and that hash is embedded in the next invoice in the sequence. According to the Resolution, this protects the sequence of invoices from tampering, whether by deletion or by replacement. A hash is a one-way fingerprint: change one character in an issued invoice and its hash changes, so the next invoice's PIH no longer matches. The counter adds a second check, because a deleted invoice leaves a gap in the numbers.

The same logic sits behind the prohibited functions in the Resolution and ZATCA's Detailed Guidelines for E-Invoicing. They ban any feature that lets users alter or delete issued invoices, reset the invoice counter, generate logs out of sequence, or run more than one invoice sequence at a time. The Guideline's information-security section adds that access to the counter value should be protected from system users. If a customer needs to cancel an invoice, the Guideline says it can only be done through an associated credit note and a new invoice. Our guide to ZATCA credit notes and debit notes covers that process.

UUID: One Fresh Identifier per Document

The Resolution requires the solution to generate a UUID in addition to the sequential invoice number, for every invoice and associated note. The Security Features Implementation Standard defines it as a 128-bit number whose generation scheme makes it globally unique with high probability and without checking a central database. Rule BR-KSA-03 adds that the machine that issued the document gives the value, and notes that Windows calls UUIDs GUIDs.

  • Never reuse a UUID after a rejection. ZATCA's FAQ says that after an invoice is rejected, the UUID and ICV should not be reused, and the fixed document is a new document with new identifiers.
  • A resend of the same signed XML is different. ZATCA's FAQ says there is no restriction on submitting the same document more than once, and that ZATCA will consider the invoice only once, based on identifiers such as the UUID and the hash. So if a timeout leaves you unsure whether ZATCA received a document, resending the identical document is not the same as creating a new one.
  • Do not confuse it with the invoice number. The UUID is generated by the machine for interchange. The invoice number is the sequential number the taxpayer issues.

ICV: The Invoice Counter

The Resolution says the counter must increment for each generated invoice or associated note, that it cannot be reset or reformatted, and that the solution must record its value in each document. ZATCA's data dictionary shows it as a numeric value and rule BR-KSA-34 limits it to digits. We did not find a rule in ZATCA's documents that fixes the starting number, so follow your solution provider's setup and ZATCA's compliance checks rather than assuming one.

  • Count every document type. The counter increments for credit and debit notes as well as invoices, and for standard and simplified invoices alike.
  • Do not leave gaps. An answer in the Detailed Guideline says that if invoices with only "not subject to VAT" supplies are generated by the same billing solution as standard-rate invoices, there should be no gaps in the counter value or in the previous invoice hash sequence. The Technical Guideline adds that gaps found in the sequence during back-end validation will be investigated and may lead to penalties if found non-compliant.
  • Never reuse or edit a counter value. ZATCA's FAQ says an ICV cannot appear on another document once a document has been generated with it, and a rejected document's hash and counter must not be changed.
  • Do not offer a reset. Resetting the counter, for example at the start of a new year, conflicts with the Resolution's requirement that it cannot be reset. Our reading is that this also applies to housekeeping tools: if your system has an admin screen that can reset or edit the counter, ask the vendor to explain it.

PIH: The Previous Invoice Hash

The PIH is a base64-encoded SHA-256 hash of the previous invoice. The data dictionary says it is computed from the business payload of that invoice: the UBL XML without the QR code (KSA-14) and the cryptographic stamp (KSA-15). The exact steps are in ZATCA's E-Invoicing Detailed Technical Guideline (signing steps, step 1):

  1. Open the invoice XML file.
  2. Remove the UBLExtensions element, the AdditionalDocumentReference whose ID is QR, and the cac:Signature element, using the XPath expressions in the Guideline.
  3. Remove the XML version declaration.
  4. Canonicalize the invoice using the C14N11 standard.
  5. Hash the canonicalized invoice with SHA-256.
  6. Encode the result in base64.

The Security Features Implementation Standard says the hash of the previous invoice uses the same transform as the cryptographic stamp and the SHA-256 algorithm. In ZATCA's worked example, the 64-character hex hash a11b6fe5...c11a3f becomes the 44-character base64 string oRtv5Y...BGj8=, which is the form that goes in the XML. Because the signed invoice hash is the same value the signature's digest and QR Tag 6 carry, it is worth reading our guide to the ZATCA QR code alongside this one.

What Counts as the Previous Invoice

According to the Technical Guideline, taxpayers must generate documents in a single sequence per onboarded EGS, covering both standard (B2B) and simplified (B2C) documents. The previous document hash of a document should equal the hash of the last document generated before it. Documents do not have to be submitted in the same order they were generated, as long as the PIH always refers to the last document generated. Three consequences follow:

  • Rejected documents count. The Guideline says the PIH must be maintained even for documents ZATCA rejected, because the platform records the hash of rejected submissions.
  • Clearance does not change the hash. ZATCA's FAQ says its stamp and QR string are not part of the hash, so you do not need to wait for clearance of one invoice before generating the next.
  • Hash the document as you generated it. The hash comes from the invoice you built and signed, not from a different version of the file.

The First Invoice

The data dictionary gives a fixed PIH for the first invoice of a chain: NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==. It describes this as the equivalent of the base64-encoded SHA-256 of the character "0". We checked it: it is the base64 text of the 64-character hex SHA-256 of "0" (5feceb66...fb57e9), so at 88 characters it looks different from the 44-character hashes that follow. Use the dictionary value exactly as published for the first invoice of each new chain, because rule BR-KSA-61 requires a PIH on every invoice.

A Worked Example of the Chain (Hypothetical)

The example below follows ZATCA's own FAQ scenario about resubmitting a rejected document. The counter values are made up for illustration, and "hash of 101" means the invoice hash that your system computed for the document with ICV 101.

DocumentICVPIH is the hash ofZATCA resultNote
Invoice A101The previous document (ICV 100)AcceptedNormal link in the chain
Invoice B102Hash of 101Rejected (an error)Keeps ICV 102, its UUID and its hash; do not edit it
Invoice C103Hash of 102AcceptedPoints to B even though B was rejected
Invoice B (fixed)104Hash of 103AcceptedNew document with a new UUID, new ICV and a current timestamp; its PIH is C's hash, not A's

ZATCA's FAQ says the date on a corrected simplified invoice should remain the date the transaction took place, while for a rejected standard invoice the date should be updated, because a non-cleared invoice is not considered a valid tax invoice. Our guide to rejected ZATCA invoices walks through the error handling in more detail.

One Chain per EGS Unit: What This Means for Branches and POS

The Resolution says a compliant solution must not generate more than one sequence of invoices and notes through each unit, and defines the unit as the key component that adds the hash, includes the previous invoice's hash and generates the cryptographic stamp for simplified invoices. The Detailed Guidelines repeat it: all invoices generated by one unit are linked by the previous invoice hash into a single chain. The Technical Guideline then links this to CSIDs: there should be one CSID per taxpayer and also one CSID per unique sequence of generated documents. The common set-ups it describes are:

Set-upWhere the CSID is held (Technical Guideline, section 3.5)What it means for counters and hashes (our reading)
Centralized server, on premise or cloudOn the server, for both signing and authentication; one CSID per unique sequence of generated documentsOne counter and one chain for each sequence the server keeps
Branch smart POS devices that issue and send their own invoicesOn each POS deviceEach device has its own counter and chain
Branch POS devices with branch servers and a central sending serverOn the branch servers that sign, plus the central server that sends to ZATCAEach signing branch server has its own chain; the sending server only authenticates
Dumb-terminal POS where a server stamps invoicesOn the stamping server, not on the terminalsThe server's sequence is the chain, whatever the number of tills

The right-hand column is our reading of how those set-ups play out, not ZATCA wording, so confirm it with your solution provider. A practical point follows from it: if several tills share one cloud sequence, your system must hand out counter values and previous hashes one at a time, in the order documents are generated, so two tills cannot both claim the same ICV or the same previous document. For onboarding steps and renewals, see our explainer on ZATCA CSIDs, and for shops and tills, our guide to a ZATCA compliant POS.

Common Mistakes

  • Reusing an ICV or UUID after a rejection. ZATCA says both should be assigned fresh when the fixed document is submitted.
  • Pointing the PIH at the last accepted document. The Guideline says it follows the last generated document, rejected or not.
  • Editing a rejected document. Its hash and counter must not be changed or updated.
  • Hashing the wrong content. Leaving the QR code, the signature or the UBL extensions in, skipping canonicalization, or hashing a different file from the one you signed gives a hash that will not match.
  • Starting a second chain by accident. Two systems, or two integrations, that issue under one CSID produce competing sequences.
  • Resetting or recreating the counter. Restarting the counter after a migration, a reinstall or a new financial year looks like a reset, which the Resolution rules out.
  • Forgetting the PIH on the first invoice. Rule BR-KSA-61 requires it, so use the fixed starting value.
  • Issuing documents outside the sequence. Credit notes, debit notes and invoices that your billing tool creates but your e-invoicing module never sees leave gaps in the counter.

Questions to Ask Your ERP Vendor

Ask your vendor to show you, live, how its system handles these points:

  1. Where does the system store the counter and the last invoice hash for each EGS or CSID, and who can change them?
  2. Can an administrator reset or edit the counter? If yes, how is that blocked or logged?
  3. Does it use the data dictionary's fixed PIH for the first invoice of each new chain?
  4. Does it calculate the hash exactly as in the Technical Guideline, and can it show the XML for one invoice with the UUID, ICV and PIH in place?
  5. What does it do after a rejection: new UUID, new ICV, and a PIH based on the last generated document?
  6. How does it keep one sequence per CSID when several tills or users create invoices at the same time?
  7. Are credit notes and debit notes counted in the same sequence?
  8. Has a sample set of invoices, credit notes and a rejected-and-resubmitted case passed ZATCA's compliance checks in the vendor's integration?

A wider shortlist is in how to choose a ZATCA compliant ERP. If your business is still preparing for Phase 2, see our Phase 2 onboarding checklist, and remember that issued invoices must also be kept: our guide to ZATCA e-invoice archiving explains how long and where.

How Raad ERP Can Help

Raad ERP is a cloud ERP for GCC trading, manufacturing, van sales and contracting businesses, with ZATCA integration for Saudi e-invoicing, invoices that include QR codes and tax registration numbers, Arabic language support and local support in Saudi Arabia. Other ERPs and e-invoicing tools also handle these fields, so compare a few using the checklist above. If you run several branches or tills and want to check how your invoice sequences should be set up, message us on WhatsApp at +971 56 102 1565 and we will walk through your set-up with you.

Questions and answers

What do ICV, PIH and UUID mean on a ZATCA invoice?
ICV is the invoice counter value (KSA-16), PIH is the previous invoice hash (KSA-13) and UUID is the unique invoice identifier (KSA-1). All three are mandatory in Phase 2 and together make the sequence of invoices tamper-evident. They are separate from the invoice number.
How is the ZATCA previous invoice hash calculated?
Take the previous invoice XML, remove the UBLExtensions, the QR reference and cac:Signature, drop the XML version line, canonicalize with C14N11, hash with SHA-256 and encode in base64. ZATCA's Detailed Technical Guideline lists these steps.
What is the PIH for the first invoice?
The data dictionary gives a fixed value for the first invoice: NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==, the base64 form of the SHA-256 of the character "0". Use it exactly as published.
Can the invoice counter be reset or reused?
No. The Implementation Resolution says the counter cannot be reset or reformatted and must increment for every invoice or note. ZATCA's FAQ says a counter value used once cannot appear on another document, even after a rejection.
What happens to the hash chain when ZATCA rejects an invoice?
The rejected document keeps its UUID, ICV and hash. You submit the fixed invoice as a new document with a new UUID, ICV and timestamp. Its PIH is the hash of the last document generated, and ZATCA records the hashes of rejected submissions.
Do I need to wait for clearance before issuing the next invoice?
No. ZATCA's FAQ says its stamp and QR string are not part of the hash, so clearance does not change it. You can keep generating invoices in sequence, though standard invoices still need clearance before they go to the buyer.
Does each branch or POS device need its own counter and hash chain?
Each EGS unit must keep a single sequence, and ZATCA says there should be one CSID per unique sequence of documents. So a signing POS device or branch server holding its own CSID has its own chain. A central server with one sequence has one.
Do credit notes and debit notes use the same counter?
Yes. The counter must increment for each generated invoice or associated note, and the data dictionary applies the counter and previous hash fields to credit and debit notes as well, for both standard and simplified documents.

This article is general information only and is not legal, tax or professional advice. Regulations, including ZATCA, UAE Ministry of Finance and FTA requirements, change often, and this content may be incomplete, out of date or contain errors. Before you act on it, check with the official authority (ZATCA in Saudi Arabia, or the Ministry of Finance or Federal Tax Authority in the UAE) or a qualified tax adviser. Raad ERP makes no warranties about the accuracy or completeness of this content and accepts no liability for any loss arising from reliance on it. Links to third-party websites are provided for convenience and are not endorsements.

Related reading