{
  "openapi": "3.1.0",
  "info": {
    "title": "Baytelmal e-invoice API",
    "version": "0.1.0",
    "description": "Builds unsigned ZATCA and draft PINT-AE invoice XML, pre-flight checks invoice data, decodes ZATCA QR codes. Does not sign or submit invoices.",
    "contact": {
      "email": "us@mewebe.com"
    }
  },
  "servers": [
    {
      "url": "https://baytelmal.com"
    }
  ],
  "paths": {
    "/v1/ksa/invoice": {
      "post": {
        "summary": "Build unsigned ZATCA UBL 2.1 XML",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Invoice"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "XML, hash, QR payload, ICV, PIH, warnings"
          },
          "400": {
            "description": "Input problems"
          },
          "429": {
            "description": "Free daily limit reached"
          }
        }
      }
    },
    "/v1/uae/invoice": {
      "post": {
        "summary": "Build draft PINT-AE XML",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Invoice"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "XML, hash, warnings"
          },
          "400": {
            "description": "Input problems"
          },
          "429": {
            "description": "Free daily limit reached"
          }
        }
      }
    },
    "/v1/ksa/check": {
      "post": {
        "summary": "Pre-flight check a Saudi invoice",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Invoice"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "passed, errors, warnings, checksRun, scope"
          }
        }
      }
    },
    "/v1/uae/check": {
      "post": {
        "summary": "Pre-flight check a UAE invoice",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Invoice"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "passed, errors, warnings, checksRun, scope"
          }
        }
      }
    },
    "/v1/ksa/qr/decode": {
      "post": {
        "summary": "Decode a ZATCA QR payload",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "qr": {
                    "type": "string"
                  }
                },
                "required": [
                  "qr"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "fields, phase, problems"
          },
          "400": {
            "description": "Input problems"
          }
        }
      }
    },
    "/v1/ksa/qr/encode": {
      "post": {
        "summary": "Encode a ZATCA QR payload (tags 1-6)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "sellerName": {
                    "type": "string"
                  },
                  "vatNumber": {
                    "type": "string"
                  },
                  "timestamp": {
                    "type": "string"
                  },
                  "totalWithVat": {
                    "type": "string",
                    "example": "100.00"
                  },
                  "vatTotal": {
                    "type": "string",
                    "example": "100.00"
                  },
                  "invoiceHash": {
                    "type": "string"
                  }
                },
                "required": [
                  "sellerName",
                  "vatNumber",
                  "timestamp",
                  "totalWithVat",
                  "vatTotal"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "qrPayload"
          },
          "400": {
            "description": "Input problems"
          }
        }
      }
    },
    "/v1/tax-number": {
      "get": {
        "summary": "Check Saudi VAT number or UAE TRN format",
        "parameters": [
          {
            "name": "country",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "SA",
                "AE"
              ]
            }
          },
          {
            "name": "value",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "formatValid, reason"
          }
        }
      }
    },
    "/v1/uae/deadline": {
      "get": {
        "summary": "UAE e-invoicing dates by revenue",
        "parameters": [
          {
            "name": "revenue_aed",
            "in": "query",
            "schema": {
              "type": "number"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Dates"
          }
        }
      }
    },
    "/v1/samples/ksa": {
      "get": {
        "summary": "Sample Saudi invoice",
        "responses": {
          "200": {
            "description": "Invoice JSON"
          }
        }
      }
    },
    "/v1/samples/uae": {
      "get": {
        "summary": "Sample UAE invoice",
        "responses": {
          "200": {
            "description": "Invoice JSON"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Party": {
        "type": "object",
        "required": [
          "legalNameEn",
          "address"
        ],
        "properties": {
          "legalNameEn": {
            "type": "string"
          },
          "legalNameAr": {
            "type": "string"
          },
          "taxRegistrationNumber": {
            "type": "string"
          },
          "registrationNumber": {
            "type": "string"
          },
          "networkId": {
            "type": "string"
          },
          "address": {
            "type": "object",
            "required": [
              "street",
              "city",
              "countryCode"
            ],
            "properties": {
              "street": {
                "type": "string"
              },
              "buildingNumber": {
                "type": "string"
              },
              "additionalNumber": {
                "type": "string"
              },
              "district": {
                "type": "string"
              },
              "city": {
                "type": "string"
              },
              "postalCode": {
                "type": "string"
              },
              "province": {
                "type": "string"
              },
              "countryCode": {
                "type": "string"
              }
            }
          }
        }
      },
      "Line": {
        "type": "object",
        "required": [
          "descriptionEn",
          "quantity",
          "unitPrice",
          "netAmount",
          "taxRate",
          "taxAmount"
        ],
        "properties": {
          "descriptionEn": {
            "type": "string"
          },
          "descriptionAr": {
            "type": "string"
          },
          "quantity": {
            "type": "string"
          },
          "unitCode": {
            "type": "string"
          },
          "unitPrice": {
            "type": "string",
            "example": "100.00"
          },
          "netAmount": {
            "type": "string",
            "example": "100.00"
          },
          "discountAmount": {
            "type": "string",
            "example": "100.00"
          },
          "taxCategory": {
            "type": "string",
            "enum": [
              "standard",
              "zero_rated",
              "exempt",
              "out_of_scope",
              "reverse_charge"
            ]
          },
          "taxRate": {
            "type": "string"
          },
          "taxAmount": {
            "type": "string",
            "example": "100.00"
          },
          "taxExemptionReasonCode": {
            "type": "string"
          },
          "taxExemptionReasonText": {
            "type": "string"
          },
          "itemCode": {
            "type": "string"
          }
        }
      },
      "Invoice": {
        "type": "object",
        "required": [
          "invoiceNumber",
          "issueDate",
          "issueTime",
          "currency",
          "supplier",
          "customer",
          "lines",
          "totalNet",
          "totalTax",
          "totalGross"
        ],
        "properties": {
          "invoiceNumber": {
            "type": "string"
          },
          "uuid": {
            "type": "string"
          },
          "typeCode": {
            "type": "string",
            "enum": [
              "invoice",
              "credit_note",
              "debit_note",
              "prepayment"
            ]
          },
          "transactionType": {
            "type": "string",
            "enum": [
              "b2b",
              "b2c",
              "b2g",
              "export"
            ]
          },
          "issueDate": {
            "type": "string"
          },
          "issueTime": {
            "type": "string"
          },
          "dueDate": {
            "type": "string"
          },
          "currency": {
            "type": "string"
          },
          "supplier": {
            "$ref": "#/components/schemas/Party"
          },
          "customer": {
            "$ref": "#/components/schemas/Party"
          },
          "lines": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Line"
            }
          },
          "totalNet": {
            "type": "string",
            "example": "100.00"
          },
          "totalTax": {
            "type": "string",
            "example": "100.00"
          },
          "totalGross": {
            "type": "string",
            "example": "100.00"
          },
          "prepaidAmount": {
            "type": "string",
            "example": "100.00"
          },
          "counterValue": {
            "type": "integer"
          },
          "previousDocumentHash": {
            "type": "string"
          },
          "correctedInvoiceNumber": {
            "type": "string"
          },
          "correctionReason": {
            "type": "string"
          },
          "notes": {
            "type": "string"
          }
        }
      }
    }
  }
}