fatoora-cli 0.6.0

Command-line interface for the fatoora ZATCA SDK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<h1 align="center">
  πŸ‡ΈπŸ‡¦ fatoority 🧾
  </br>
  <a href="https://github.com/mqqz/fatoority">
    <img src="https://github.com/mqqz/fatoority/blob/main/docs/assets/images/crab-logo.webp" height=150px alt="fatoority-logo">
  </a>
</h1>
<p align="center">
<a target="_blank" href="#license"><img src="https://img.shields.io/badge/License-MIT%20OR%20Apache--2.0-green.svg"/></a>
<a href="https://codecov.io/gh/mqqz/fatoority"><img src="https://codecov.io/gh/mqqz/fatoority/graph/badge.svg?token=JRI6609XU6"/></a>
</p>
<p align="center">
<a href="https://docs.fatoority.com">🌐 Homepage</a> &nbsp;&bull;&nbsp;
<a href="#-features">⚑ Features</a> &nbsp;&bull;&nbsp;
<a href="#-installation">πŸ“₯ Installation</a> &nbsp;&bull;&nbsp;
<a href="#-examples">πŸ’‘ Examples</a> &nbsp;&bull;&nbsp;
<a href="#%EF%B8%8F-documentation">πŸ—‚οΈ Documentation</a>
</p>

#

`fatoority` is an open-source SDK and CLI for Saudi e-invoicing. Build standard and
simplified invoices from application data, including credit notes, debit notes,
and prepayment invoices. Calculate totals and VAT, generate UBL XML, then sign
and validate invoices locally before submitting them through the ZATCA APIs.

With bindings and support for many programming languages:
- πŸ¦€ Rust <a href="https://crates.io/crates/fatoora-core"><img src="https://img.shields.io/crates/v/fatoora-core?logo=rust&label=fatoora-core" alt="fatoora-core Crates.io Version"/></a>
  
- 🐍 Python <a href="https://pypi.org/project/fatoority/"><img src="https://img.shields.io/pypi/v/fatoority?logo=python" alt="PyPi Version"/></a>

- πŸ‡―β€ŒπŸ‡Έβ€Œ JavaScript / TypeScript <a href="https://www.npmjs.com/package/fatoority"><img src="https://img.shields.io/npm/v/fatoority?logo=javascript" alt="NPM Version"/></a>

- 🟣 C# / .NET <a href="https://www.nuget.org/packages/Fatoority"><img src="https://img.shields.io/nuget/v/Fatoority?logo=dotnet" alt="NuGet Version"/></a>

- β˜• Kotlin / Java / Android <a href="https://central.sonatype.com/artifact/com.fatoority/fatoority"><img src="https://img.shields.io/maven-central/v/com.fatoority/fatoority?logo=kotlin" alt="Maven Central Version"/></a>

- πŸŒ€ C/C++ (through the C FFI see [releases]https://github.com/mqqz/fatoority/releases)

- πŸ”Œ Also, a Command Line Interface (CLI) Tool  <a href="https://crates.io/crates/fatoora-cli"><img src="https://img.shields.io/crates/v/fatoora-cli?logo=rust&label=fatoora-cli" alt="fatoora-cli Crates.io Version"/></a>

> **Disclaimer**:
> `fatoority` is not affiliated, associated, authorized, endorsed by, or in any way officially connected with ZATCA (Zakat, Tax and Customs Authority), or any of its subsidiaries or its affiliates.
> The official ZATCA website can be found at https://zatca.gov.sa.

## πŸ—‚οΈ Documentation
Follow the [complete sandbox walkthrough](https://docs.fatoority.com/guides/sandbox-walkthrough/)
in Python, C#, or TypeScript: create an invoice, sign it, validate it, report it,
and handle the response. The guide includes a ZATCA journey diagram and production changes.

For local XML output, start with [invoice creation](https://docs.fatoority.com/guides/invoice-creation/), browse the
[guides](https://docs.fatoority.com), or read the [Rust API reference](https://docs.rs/fatoora-core/latest/fatoora_core/).

## ⚑ Features

| Build into your application | What fatoority provides |
| --- | --- |
| 🧾 [Invoice creation]https://docs.fatoority.com/guides/invoice-creation/ | Typed builders for standard and simplified tax invoices, credit/debit notes, and prepayments; UBL XML output. |
| πŸ’° [Pricing and VAT]https://docs.fatoority.com/guides/invoice-recipes/ | Exact decimal calculations, mixed VAT categories and rates, exemptions, line and document discounts/charges, price-base quantities, and payable totals. |
| πŸ’Ό [Business document data]https://docs.fatoority.com/reference/invoice-model/ | Purchase orders, contracts, delivery dates, payment accounts/instructions, repeated references, and party/item identifiers. |
| </>  [XML import]https://docs.fatoority.com/reference/xml/ | Read supported invoice fields into structured data. Signed import checks signature, digest, and QR consistency and retains the original XML. |
| βœ… [Local validation]https://docs.fatoority.com/guides/validation/ | UBL schemas and all 257 business-rule assertions in the pinned SDK 238-R3.4.8 profile, with structured findings and explicit stage coverage. |
| ✍️ [Invoice Signing]https://docs.fatoority.com/guides/invoice-signing/ | Sign XML with a matching certificate and key, and compute invoice hashes. |
| π–£― [QR Generation]https://docs.fatoority.com/guides/qr/ | Generate base64 QR payloads or read them from signed XML. |
| πŸ“© [CSR Generation]https://docs.fatoority.com/guides/csr/ | Generate signing keys and certificate requests for device onboarding. |
| πŸ“¨ [API Requests]https://docs.fatoority.com/guides/api/ | Obtain and renew credentials, check compliance, report simplified invoices, and request standard-invoice clearance. |

Local invoice processing needs no official SDK installation or network access.
The Rust core has no Java dependency.
Use the JavaScript WebAssembly package in Node.js or a browser, and the Android
artifact in mobile applications. API calls require network access; prepared
requests let you use your own HTTP transport.

πŸš€ The recorded .NET benchmarks measured hashing at 8.5–14.1Γ—, signing at 11.3–15.6Γ—,
and validation at about 44Γ— the speed of the official .NET SDK on the tested
fixtures. The separate CLI hashing benchmark measured about 187Γ—, including
process startup. See [benchmarks](https://docs.fatoority.com/benchmarks/) for
hardware, versions, timings, and reproduction instructions.

βš™οΈ The test suite includes property tests, fuzzing, and recorded comparisons with
the official SDK. See [SDK compatibility](https://docs.fatoority.com/development/sdk-parity/)
for the tested scope. Local validation does not establish certificate issuer
trust or ZATCA gateway acceptance.

## πŸ“₯ Installation
<details>
<summary>Rust</summary>
  
```sh
cargo add fatoora-core
```
</details>

<details>
<summary>Python</summary>
  
```sh
pip install fatoority
```
</details>

<details>
<summary>JavaScript / TypeScript</summary>

```sh
npm install fatoority
```
</details>

<details>
<summary>C# / .NET 8</summary>

```sh
dotnet add package Fatoority
```

</details>

<details>
<summary>Kotlin / Java / Android</summary>

Install a published release from Maven Central.

**Gradle** (`build.gradle.kts`):

```kotlin
repositories { mavenCentral() }
dependencies {
    implementation("com.fatoority:fatoority:0.6.0")
}
```

**Maven** (`pom.xml`):

```xml
<dependency>
  <groupId>com.fatoority</groupId>
  <artifactId>fatoority</artifactId>
  <version>0.6.0</version>
</dependency>
```

For Android, use `com.fatoority:fatoority-android:0.6.0` instead of the desktop
artifact. Java/Kotlin imports remain `com.fatoora`.

</details>

<details>
<summary>C/C++</summary>
  
Download the `fatoora-ffi-*` archive for your platform from
[releases](https://github.com/mqqz/fatoority/releases) and extract it. The shared
library is at the archive root. The C API header is `include/fatoora.h`,
with its adapter in `src/fatoora.c` and generated headers in `include/generated`; C++ headers
are in `include/diplomat-cpp` and expose types in namespace `fatoora`.

For example, on Linux, compile your application from the extracted directory:

```sh
cc -std=c11 -Iinclude app.c src/fatoora.c -L. -lfatoora_ffi -Wl,-rpath,'$ORIGIN' -o app
c++ -std=c++17 -Iinclude/diplomat-cpp app.cpp -L. -lfatoora_ffi -Wl,-rpath,'$ORIGIN' -o app-cpp
```

Include `fatoora.h` in C or `fatoora/Config.hpp` in C++. Keep the shared library
beside these executables. On Windows, link the supplied import library and keep
the DLLs from the archive beside your executable.

To build the library from a source checkout:

```sh
cargo build -p fatoora-ffi --release --locked
```

The [C API](https://github.com/mqqz/fatoority/blob/main/docs/reference/bindings/c.md)
provides one `fatoora.h`, plain strings, shared errors, and named-field options.
Compile `bindings/c/fatoora.c` once with your application, add `bindings/c` to
your include path, and link `fatoora_ffi`. Release archives also include the C
examples under `examples/c`.

The library is written to `target/release/`. The generated headers are checked in
under `bindings/c/generated` and `bindings/cpp`; raw ABI callers use those include paths and
`-Ltarget/release` when compiling against a source build. Cargo does not regenerate
headers. After changing bridge declarations, follow the
[Diplomat generation workflow](https://github.com/mqqz/fatoority/blob/main/docs/development/diplomat.md#generate-and-verify).
</details>

<details>
<summary>CLI</summary>
  
The cli tool can also be installed with `cargo`: 

```
cargo install fatoora-cli
```
Alternatively, you can grab the precompiled binary for your platform on the repo's [releases](https://github.com/mqqz/fatoority/releases) (`fatoora-cli-*`). Extract the archive and run `fatoora`.
</details>

## πŸ’‘ Examples

<details>
<summary>Create an invoice</summary>

Build a simplified invoice with two SAR 50 items and 15% VAT. The library
calculates SAR 15 VAT and a SAR 115 payable total, then writes `invoice.xml`.
This example uses demonstration identity and invoice-history data; replace them
for your integration. Running it replaces `invoice.xml` in the working folder.

Install with `pip install fatoority`, save as `creation.py`, and run `python creation.py`.

```python
from datetime import datetime, timezone
from pathlib import Path
from uuid import UUID
from fatoora import (Address, InvoiceBuilder, InvoiceSubType, InvoiceTypeKind,
                     PaymentMeans, VatCategory)

seller_address = Address(
    "SA", "Riyadh", "King Fahd Road", "1234", "12222",
    additional_number="5678", district="Olaya",
)
invoice = (
    InvoiceBuilder(
        InvoiceTypeKind.TAX, InvoiceSubType.SIMPLIFIED,
        id="INV-1001",
        uuid=UUID("8e6000cf-1a98-4174-b3e7-b5d5954bc10d"),
        issue_datetime=datetime(2026, 10, 26, 9, 0, tzinfo=timezone.utc),
        currency="SAR",
        # Demonstration predecessor; use your own invoice history in production.
        previous_invoice_hash="NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==",
        invoice_counter=1,
        vat_category=VatCategory.STANDARD,
    )
    .set_seller("Example Store", address=seller_address, vat_id="399999999900003",
                other_id="7003339333", other_id_scheme="CRN")
    .add_payment_means(PaymentMeans("10"))
    .add_line_item(description="Notebook", quantity="2", unit_code="PCE", unit_price="50",
                   vat_rate="15", vat_category=VatCategory.STANDARD)
    .build()
)
Path("invoice.xml").write_text(invoice.xml, encoding="utf-8")
totals = invoice.totals
print(f"VAT: {totals.tax_amount} SAR")
print(f"Payable: {totals.payable_amount} SAR")
print("Saved invoice.xml")
```

See [invoice creation](https://docs.fatoority.com/guides/invoice-creation/) for the
Rust example and next steps. Building checks model fields and arithmetic; run
local validation separately before submission.
</details>

<details>
<summary>CSR Generation</summary>

Rust
```rust
use fatoora_core::config::Environment;
use fatoora_core::csr::{CsrProperties, SigningKey};

let props = CsrProperties::from_file("csr.properties")?;
let key = SigningKey::generate();
let csr = props.build(&key, Environment::NonProduction)?;
let csr_pem = csr.to_pem()?;
let key_pem = key.to_pem()?;
```

Python
```python
from fatoora import CsrProperties, Environment, SigningKey

props = CsrProperties.from_file("csr.properties")
key = SigningKey.generate()
csr = props.build(key, Environment.NON_PRODUCTION)
csr_pem = csr.to_pem_base64()
key_pem = key.to_pem()
```

JavaScript
```js
import { CsrProperties, Environment, SigningKey } from 'fatoority';

const key = SigningKey.generate();
const csr = CsrProperties.fromStr(propertiesText).build(key, Environment.NonProduction);
const csrPem = csr.toPem();
```

CLI
```bash
fatoora csr --csr-config csr.properties --generated-csr csr.pem --private-key key.pem --pem
```
</details>

<details>
<summary>Invoice Signing</summary>

Rust
```rust
use fatoora_core::invoice::sign::InvoiceSigner;

let cert_pem = std::fs::read_to_string("cert.pem")?;
let key_pem = std::fs::read_to_string("key.pem")?;
let signer = InvoiceSigner::from_pem(cert_pem.trim(), key_pem.trim())?;
let xml = std::fs::read_to_string("invoice.xml")?;
let signed_xml = signer.sign_xml(&xml)?;
```

CLI
```bash
fatoora sign --invoice invoice.xml --cert cert.pem --key key.pem --signed-invoice signed.xml
```
</details>

<details>
<summary>Validation</summary>

Rust
```rust
use fatoora_core::invoice::validation::{Validator, ZatcaValidationOptions};

let xml = std::fs::read_to_string("invoice.xml")?;
let options = ZatcaValidationOptions {
    previous_invoice_hash: Some(previous_hash_from_history),
    ..Default::default()
};
let report = Validator::new().validate_zatca(&xml, &options)?;
if !report.is_valid() {
    // Inspect stages and findings; missing predecessor context is incomplete.
}
```

CLI
```bash
fatoora validate --invoice invoice.xml --profile zatca --format json \
  --previous-invoice-hash "$PREVIOUS_INVOICE_HASH"
```

The local profile implements all 257 business-rule assertions from SDK
`238-R3.4.8`, plus applicable signature, QR and predecessor checks. It preserves
warnings and partial reports. Standard invoices skip signature/QR checks under
this SDK profile. Local validation does not establish issuer trust or remote
acceptance. `Validator::validate_xml` and the default CLI profile
continue to check XSD only.
</details>

<details>
<summary>QR Generation & Reading</summary>

Rust
```rust
use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;

let xml = std::fs::read_to_string("signed.xml")?;
let signed = parse_signed_invoice_xml(&xml)?;
let qr = signed.qr_code();
```

CLI
```bash
fatoora qr --invoice invoice.xml
fatoora qr --invoice signed.xml
fatoora qr --invoice signed.xml --fail-on-signed
fatoora qr-read --invoice signed.xml
```
</details>

<details>
<summary>Invoice Hash</summary>

Rust
```rust
use fatoora_core::invoice::xml::parse::parse_finalized_invoice_xml;

let xml = std::fs::read_to_string("invoice.xml")?;
let invoice = parse_finalized_invoice_xml(&xml)?;
let hash = invoice.hash()?;
```

CLI
```bash
fatoora generate-hash --invoice invoice.xml
```
</details>

<details>
<summary>Invoice Request Payload</summary>

Rust
```rust
use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;

let xml = std::fs::read_to_string("signed.xml")?;
let signed = parse_signed_invoice_xml(&xml)?;
let payload = serde_json::json!({
    "invoiceHash": signed.invoice_hash(),
    "uuid": signed.uuid(),
    "invoice": signed.to_xml_base64(),
});
```

CLI
```bash
fatoora invoice-request --invoice signed.xml --api-request request.json
```
</details>

For discounts, mixed VAT, credit notes, and prepayments, see the
[invoice recipes](https://docs.fatoority.com/guides/invoice-recipes/).

## 🀝 Contributing

Contributions are always welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for more details.

## πŸͺ§ Roadmap
- Increase test coverage to 100% (Inshallah)
- Expand bindings to other languages (subject to demand)
- PDF invoice generation

## Author πŸ€“

This project began as a personal undertaking, and 
continues to be developed and maintained by me [@mqqz](https://github.com/mqqz).

While this means development happens alongside other commitments, I do my
best to keep the project maintained and responsive to issues. Contributions,
feedback, and suggestions are always welcome.

If you or your team are working on fintech or ZATCA integrations and need help building
reliable scalable infrastructure, feel free to get in touch.

## πŸ”— Relevant Links
- [ZATCA E-Invoicing Homepage]https://zatca.gov.sa/en/E-Invoicing/Pages/default.aspx
- [ZATCA Fatoora SDK]https://sandbox.zatca.gov.sa/downloadSDK
- [ZATCA API Sandbox]https://sandbox.zatca.gov.sa/IntegrationSandbox

## License

This project's code is licensed under either the [MIT License](LICENSE-MIT) or the
[Apache License, Version 2.0](LICENSE-APACHE), at your option.

Third-party schemas, fixtures, templates, and dependencies retain their applicable
upstream licenses and notices. The project license does not relicense those materials.

See [third-party notices](THIRD_PARTY_NOTICES.md) for attribution, applicable
terms, and outstanding permission questions.