Skip to content

Creating Documents

This guide walks through the process of creating sales and purchase documents via the API.


Overview

The following endpoints support document creation:

Endpoint Document Type
POST /invoices Sales invoices
POST /orders Sales orders
POST /receipts Receipts / payment confirmations
POST /purchases Purchase / supplier invoices
POST /purchase_orders Purchase orders
POST /transfer_orders Stock transfer orders

All document creation endpoints:

  • Require a session token (Authorization: Bearer <xa-token>)
  • Require a nonce query parameter
  • Accept a JSON request body
  • Optionally return a PDF when print=1 is passed

POST /invoices answers 202 Accepted without an ID or number, or 200 with the PDF when print=1. Send your own reference and read the invoice back by it to get its id and number. Invoices cannot be cancelled through the API; cancel them in Gestix.

Gestix calculates document totals on the server. For rounding, VAT-inclusive prices, discounts, and Peppol/EN 16931 scenarios, see Invoice Totalization.


Identifying the third party

When creating a customer or supplier through the API, provide sufficient identifying data under the applicable entity/tenant validation rules. In the workflow where either is accepted, this means vat_id or email. Email is not universally mandatory. Name alone is not a reliable unique identifier: different real entities may share the same name.

For an embedded customer, use an accepted lookup key, such as an existing code, or VAT ID/email. If the workflow creates a new third party, include name and top-level country_iso in the third-party object as well as the identifying data. These fields do not replace one another. See third-party creation requirements for applicable validation rules.


Step-by-step: Creating an Invoice

1. Prepare the request body

A customerDocument object. Required fields: document_set, seq_number, date, currency, total_amount.

{
  "document_set": "FT",
  "seq_number": "FT2024/1002",
  "date": "2024-11-15",
  "currency": "EUR",
  "net_amount": 1000.00,
  "vat": 230.00,
  "total_amount": 1230.00,
  "payment_type": "Transferência Bancária",
  "customer": {
    "name": "Acme Corp",
    "country_iso": "PT",
    "vat_id": "PT123456789",
    "billing_address": {
      "postal_address": "Rua da Liberdade 1",
      "city": "Lisboa",
      "zip_code": "1250-001",
      "country": "PT"
    }
  },
  "items": [
    {
      "item_code": "CONSULT",
      "description": "Consulting Services — November 2024",
      "qty": 2,
      "unit_price": 500.00,
      "vat": 23.0
    }
  ]
}

2. Send the request

POST /api/03101176/v4.0.0/invoices?nonce=1700000000001
Authorization: Bearer <xa-token>
Content-Type: application/json

{ ... document body ... }

3. Handle the response

On 202 Accepted the invoice is created, but the response has no ID or number. Read it back by your reference:

GET /api/03101176/v4.0.0/invoices/?nonce=1700000000002&searchColumn=reference&searchString=503-2026PTSEC

The result has the invoice's id and number (document_alias). For its PDF, call GET /invoices/{id}/?nonce=N&print=1.

On 400 Bad Request nothing is stored. From 7.2.03 the body is {"status":400,"error":true,"message":"..."}; before 7.2.03 it is empty and the reason is read with GET /status. See Error Handling.

On 409 Conflict — a document with this document_set + seq_number already exists.


Getting a PDF

Pass print=1 to receive the PDF of the created document directly in the response body:

POST /api/03101176/v4.0.0/invoices?nonce=1700000000002&print=1
Authorization: Bearer <xa-token>
Content-Type: application/json

The response is 200 with Content-Type: application/pdf and the PDF binary. The invoice number is in the Content-Disposition file name, for example filename=DOC-506819523-CI-FAT-63-7022.pdf (series FAT, number 63).

response = requests.post(url, params={"nonce": nonce, "print": 1}, ...)
if response.headers["Content-Type"] == "application/pdf":
    with open("invoice.pdf", "wb") as f:
        f.write(response.content)

Document Lines (items)

Each line item in the items array is a documentLine:

{
  "item_code": "WIDGET-A",
  "description": "Widget Type A",
  "qty": 10,
  "unit_price": 25.00,
  "discount": 5.0,
  "vat": 23.0,
  "vat_included": 0
}
  • description is required; it is printed on service (S) and free-text (T) lines, while article lines (P or no item_type) print the article's description. See Line types
  • item_code is optional but links the line to a product in /articles
  • vat_included 0 means unit_price excludes VAT; any other value means it includes VAT
  • vat_exemption_code (for example M07) is accepted on lines with vat 0
  • Numbers: JSON numbers or strings with a dot as the decimal point; see Numbers

Batch / lot tracked items

Create the article with track_batch_number: "1" first (see Articles). In each document item, supply lot identifiers, allocated quantities and dates inside serial_numbers. Multiple allocations can split one logical item quantity; their quantities must total the item's qty exactly. See the complete input example and validation rules, including date consistency, available stock on exits and separate rows returned by GET.


Notes on Required Fields

Field Notes
document_set Must match a document set configured in Gestix
seq_number Must be unique within its document_set
date Format: YYYY-MM-DD
currency Must be a valid currency code (e.g., EUR)
total_amount Must match the sum of line totals (server may validate)

Creating Supplier Documents

The process is identical for POST /purchases and POST /purchase_orders, but the request body uses a supplierDocument schema — note the slight field name differences (related_doc vs related_document, source_warehouse instead of warehouse).


Example in Python

This example uses email as the identifying data for a workflow that accepts it. Supply VAT ID or other data instead/as well when the applicable entity/tenant rules require it.

import requests
import time

BASE_URL = "https://gestix.pt:443/api/03101176/v4.0.0"
TOKEN = "your-session-token"

document = {
    "document_set": "FT",
    "seq_number": "FT2024/1003",
    "date": "2024-11-15",
    "currency": "EUR",
    "total_amount": 246.00,
    "customer": {
        "name": "Test Customer",
        "country_iso": "PT",
        "email": "customer@example.com"
    },
    "items": [
        {
            "description": "Test Item",
            "qty": 1,
            "unit_price": 200.00,
            "vat": 23.0
        }
    ]
}

nonce = int(time.time() * 1000)

response = requests.post(
    f"{BASE_URL}/invoices",
    params={"nonce": nonce},
    headers={
        "Authorization": f"Bearer {TOKEN}",
        "Content-Type": "application/json"
    },
    json=document
)

if response.status_code == 202:
    print("Invoice accepted; search by reference for its id and number")
else:
    try:
        error = response.json()
    except ValueError:
        error = response.text or None
    print(f"HTTP {response.status_code}: {response.reason or ''}")
    if error:
        print(error)