Blog · 27 Nov 2026
How to Generate a ZATCA QR Code in Node.js (No Library Needed)
By Muhammad Hamid · Last reviewed 27 Nov 2026
Free toolZATCA QR Code Generator & Decoder
Build or decode the TLV Base64 QR on Saudi e-invoices. Runs in your browser.
The ZATCA QR payload is small enough that you don’t need a package for it. Here is the code I use, about 20 lines, with nothing but Node’s Buffer. It runs as-is on Node 18 or later.
If you want to understand the format first, read ZATCA QR code explained. To check your output, paste it into the ZATCA QR decoder.
The encoder and decoder
// zatca-qr.js
function tlv(tag, value) {
const bytes = Buffer.isBuffer(value) ? value : Buffer.from(String(value), "utf8");
if (bytes.length > 255) {
throw new Error(`Tag ${tag} is ${bytes.length} bytes; the maximum is 255`);
}
return Buffer.concat([Buffer.from([tag, bytes.length]), bytes]);
}
function zatcaQrBase64({ sellerName, vatNumber, timestamp, total, vatTotal }) {
return Buffer.concat([
tlv(1, sellerName),
tlv(2, vatNumber),
tlv(3, timestamp),
tlv(4, total),
tlv(5, vatTotal),
]).toString("base64");
}
function decodeZatcaQr(base64) {
const data = Buffer.from(base64, "base64");
const fields = [];
for (let i = 0; i < data.length; ) {
const tag = data[i];
const length = data[i + 1];
const value = data.subarray(i + 2, i + 2 + length);
if (value.length !== length) throw new Error(`Tag ${tag} is truncated`);
fields.push({ tag, value });
i += 2 + length;
}
return fields;
}
module.exports = { tlv, zatcaQrBase64, decodeZatcaQr };
Two details matter here:
Buffer.from(value, "utf8")gives the byte length, so Arabic seller names get the right length automatically. Usingvalue.lengthis the most common bug.- The records are joined first and Base64-encoded once at the end.
Try it
const { zatcaQrBase64, decodeZatcaQr } = require("./zatca-qr");
const base64 = zatcaQrBase64({
sellerName: "Bobs Records",
vatNumber: "310122393500003",
timestamp: "2022-04-25T15:30:00Z",
total: "1000.00",
vatTotal: "150.00",
});
console.log(base64);
for (const { tag, value } of decodeZatcaQr(base64)) {
console.log(tag, value.toString("utf8"));
}
Output:
AQxCb2JzIFJlY29yZHMCDzMxMDEyMjM5MzUwMDAwMwMUMjAyMi0wNC0yNVQxNTozMDowMFoEBzEwMDAuMDAFBjE1MC4wMA==
1 Bobs Records
2 310122393500003
3 2022-04-25T15:30:00Z
4 1000.00
5 150.00
That string matches the example widely used in ZATCA’s Phase 1 guidance, so you can use it as a test case in your own suite.
Turn it into a QR image
Install the qrcode package:
npm install qrcode
const QRCode = require("qrcode");
const { zatcaQrBase64 } = require("./zatca-qr");
async function main() {
const base64 = zatcaQrBase64({
sellerName: "Bobs Records",
vatNumber: "310122393500003",
timestamp: "2022-04-25T15:30:00Z",
total: "1000.00",
vatTotal: "150.00",
});
await QRCode.toFile("invoice-qr.png", base64, { errorCorrectionLevel: "M", width: 300 });
const dataUrl = await QRCode.toDataURL(base64);
console.log("saved invoice-qr.png;", dataUrl.slice(0, 30));
}
main();
Use toDataURL when you need to put the image straight into an HTML or PDF invoice template.
A test worth keeping
const assert = require("node:assert/strict");
const { zatcaQrBase64, decodeZatcaQr } = require("./zatca-qr");
// Arabic names: the length byte must count UTF-8 bytes, not characters.
const name = "شركة التوريدات";
const qr = zatcaQrBase64({
sellerName: name,
vatNumber: "310122393500003",
timestamp: "2022-04-25T15:30:00Z",
total: "1150.00",
vatTotal: "150.00",
});
const [seller] = decodeZatcaQr(qr);
assert.equal(seller.value.toString("utf8"), name);
assert.equal(seller.value.length, 27); // 14 characters, 27 bytes
Phase 2: adding tags 6 to 9
tlv() accepts a Buffer, so you can append the Phase 2 values after the first five records:
const phase2 = Buffer.concat([
tlv(1, sellerName),
tlv(2, vatNumber),
tlv(3, timestamp),
tlv(4, total),
tlv(5, vatTotal),
tlv(6, invoiceHash), // from your signing step
tlv(7, signature), // from your signing step
tlv(8, publicKey), // from your certificate
// tlv(9, ...) for simplified invoices, per ZATCA's spec
]).toString("base64");
Where these values come from, and the exact format for each one, is defined in ZATCA’s security features specification and in the ZATCA SDK. Generate them in the same step that signs the invoice XML, and validate the final invoice with the ZATCA SDK or the sandbox before going live.
This post covers the data format and code from implementation work. It isn’t tax advice. Confirm the requirements that apply to you with ZATCA or your e-invoicing provider.
FAQ
Do I need a special library to generate a ZATCA QR code?
No. The payload is TLV records encoded as Base64, which takes about 20 lines with Node's Buffer. You only need a QR library (such as qrcode) to turn that string into an image.
Does this work for Phase 2?
The TLV function works for any tag, so it can add tags 6 to 9. In Phase 2 those values come from signing the invoice XML, so call it from the same step that signs the invoice.