Skip to main content

Developer Docs · Schema v1.0.0

GulfInvoice Check Developer Documentation

Complete technical reference for running gic_core locally, calling FastAPI v1 endpoints, batching invoices, and integrating Apify Actors or CI/CD.

1. Local Getting Started ($0 Cost)

Install the isolated Python 3.12 environment via uv, verify tests, and launch the localhost-only FastAPI adapter:

Install & Start Local APIpowershell
uv sync --extra dev
uv run pytest --cov=packages/gic_core
uv run uvicorn gic_api.main:app --host 127.0.0.1 --port 8000 --app-dir apps/local-api/src

2. Input Formats & Security Limits

  • ZATCA XML POST /v1/validate/zatca: UTF-8 UBL 2.1 XML, max 1 MiB, max depth 64, max elements 25,000. DOCTYPE and ENTITY declarations are strictly blocked.
  • ZATCA QR POST /v1/decode/qr: Base64 TLV string (max 4,096 chars accepted; warning above ZATCA's 700-char guideline) + optional comparison XML.
  • JoFotara Validate & Build POST /v1/validate/jofotara / POST /v1/build/jofotara: UBL 2.1 XML or strictly typed JSON request with Decimal numeric fields.
  • Batch Limit POST /v1/validate/batch: Bounded to 50 items per request by default; remote URL fetching is disabled for SSRF protection.

3. Stable Output Schema (v1.0.0)

ValidationResult Envelopejson
{
  "schema_version": "1.0.0",
  "standard": "zatca",
  "status": "invalid",
  "is_valid_preflight": false,
  "summary": {
    "total_rules_evaluated": 40,
    "passed_count": 34,
    "error_count": 6,
    "warning_count": 0,
    "unsupported_count": 2,
    "duration_ms": 1.82
  },
  "findings": [
    {
      "rule_id": "BR-KSA-65",
      "category": "business_rule",
      "severity": "error",
      "xpath": "/Invoice/cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:PlotIdentification",
      "actual_value": "88",
      "expected_value": "4 numeric digits (e.g. 2223)",
      "evidence_status": "under_review"
    }
  ]
}

4. Python Client & Direct Core Examples

clients/python/gic_clientpython
from pathlib import Path
from gic_client import GulfInvoiceClient

xml = Path("fixtures/zatca/invalid_br_ksa_65_and_vat.xml").read_text(encoding="utf-8")
with GulfInvoiceClient("http://127.0.0.1:8000") as client:
    result = client.validate_zatca(xml)
    print(result.status, [f.rule_id for f in result.findings])

5. Apify Actors & Pay-Per-Event Readiness

Three independent Actors live under actors/: zatca-invoice-validator, zatca-qr-checker, and jofotara-invoice-toolkit. Billing stays disabled unless PAYMENTS_ENABLED, LIVE_MONETIZATION_ENABLED, and LIVE_ACTOR_ENABLED are explicitly enabled.

Actor Input (zatca-invoice-validator)json
{
  "invoices": [
    { "item_id": "inv-001", "xml": "<Invoice>...</Invoice>" },
    { "item_id": "inv-002", "xml": "<Invoice>...</Invoice>" }
  ],
  "maxItems": 50
}

6. n8n Automation & CI/CD Integration

In n8n, point an HTTP Request node to your local FastAPI instance during staging—or use the official Apify node after publishing Actors. In GitHub Actions, fail builds on invoice regressions:

.github/workflows/erp-invoice-gate.ymlyaml
- name: Preflight Validate ERP Synthetic Fixtures
  run: |
    uv run python -c "
    from pathlib import Path
    from gic_core.validation import validate_zatca_xml
    res = validate_zatca_xml(Path('fixtures/zatca/valid_standard_b2b.xml').read_text())
    assert res.is_valid_preflight, [f.rule_id for f in res.findings]
    "

7. Troubleshooting

  • SEC-XML-DTD: Strip any <!DOCTYPE> header injected by legacy XML serializers.
  • QR-03 (Arabic UTF-8 length mismatch): Always measure TLV lengths in UTF-8 bytes (len(s.encode("utf-8"))), never character length.
  • Browser Quick Check vs Local API: Start Uvicorn on 127.0.0.1:8000. On a locally served site (localhost ports 3000 and 4173, pre-configured in CORS) the workspace detects it automatically. On a public site origin the page never contacts localhost on its own: click Connect local API in the workspace or status page (your browser may ask for local-network permission), and start Uvicorn with that origin allowed, e.g. GIC_API_CORS_ORIGINS=https://your-site.example (comma-separated; setting it replaces the localhost defaults).