Skip to main content
This guide takes the invoice of an import from your system to the fields that the reading extracts. It uses curl and five endpoints, in this order:
  1. GET /packs shows the pack and the document type of the invoice.
  2. POST /contacts creates the contact.
  3. POST /collections creates the collection of the contact.
  4. POST /documents sends the invoice to the collection.
  5. GET /documents/{id} shows the progress of the reading and, at the end, the fields of the invoice.
At the end, GET /documents?external_id= finds the documents by the number of the exchange operation in your system.

Before you start

You need three things:
  • the API key of the organization. See Authentication;
  • an active operation pack that asks for the commercial invoice. The operator creates the pack in QX;
  • the PDF of a commercial invoice, with the name fatura.pdf.
The examples use qx_sua_chave. Replace this text with the key of the organization. Each POST in the guide carries the Idempotency-Key header. Create a new value for each write, and keep the value with the write in your system. If the response does not arrive, repeat the call with the same value. The API writes the resource only once. See Idempotency.

1. Find the pack and the invoice type

The response contains the active operation packs. Each requirement of the pack contains the document type that the requirement asks for:
The page_numbers and last_pages fields say which pages of each file the reading sends to the model. page_numbers lists pages counted from the first page, such as [2, 4]. last_pages counts pages from the end, such as 2 for the last two pages. With null in both, the reading sends every page. Save the id of the pack (pck_12) and the id of the invoice type (dtp_31).

2. Create the contact

The 201 response contains the contact:
  • QX creates a company when you send a CNPJ, and a person when you send a CPF.
  • QX looks up the CNPJ after the creation. The lookup_status field shows the progress of the lookup.
  • The organization can already have a contact with this CNPJ. In that case, the response is 409 with the code contact_exists, and the contact field contains the contact. Use the id of that contact in the next steps.

3. Create the collection

The 201 response contains the collection, with an empty slot for each requirement of the pack:
  • The collection starts in the assembling state, in assembly.
  • The protocol is the name of the collection. When the pack has a protocol rule, QX composes the name from the fields of the documents, such as ACME TRADING MXA188-UXA837. Without documents or without a rule, the protocol is the generated protocol, such as DOC-2026-00123.
  • The protocol changes when the documents of the collection change, in every state of the collection. Keep the id as the key of the collection.
  • QX does not send an email to the contact. To send the portal invitation to the contact, also send "send_invitation": true.

4. Send the invoice to the collection

The external_id is the external id: a value of your system, such as the number of the exchange operation. QX keeps the value as it came, up to 128 characters. The same value can go with many uploads, such as the documents of one exchange operation. The 201 response contains the new document, and the Location header carries its path. The reading already started, and the fields have no answers yet:
  • The new document takes the empty slot of the invoice, and QX removes the slot doc_310 from the collection. Use the id of the response (doc_318) in the next steps.
  • When every required document of the collection arrives, the collection moves to reviewing, and QX notifies the operators.

5. Wait for the reading

Call GET /documents/{id} every 10 seconds or more. Stop when the reading.state reaches one of these values: The values not_started, extracting, enriching and validating tell that the reading did not end yet. reading.finished_at carries the time recorded at the end of the reading. On a document read before this version of the API, with no reading after it, the value is an estimate and can be null. On a document type with no automatic reading, the reading does not start, and reading.state stays not_started. So the loop below stops after 60 calls, about 10 minutes. The loop below uses jq to read the field:

6. Read the fields

The response contains all the fields of the type, in the catalog order. The example shows only a part of the fields:
  • Each item of fields contains the key, the label, the text of the field in raw_value, and the typed value in value.
  • value_type gives the shape of the value: text, integer, decimal, money, date, boolean, list or table.
  • A date comes as text, in the ISO 8601 format. A decimal number comes as text, with a dot. A money value comes as decimal text, with the currency when the document has one. With no currency, currency is null.
  • A text that does not convert to the type of the field, such as the date 31/02/2026, stays in raw_value, with value equal to null.
  • source gives the recorded origin of the value:
    • reading: the reading.
    • operator: an operator, the client on the portal, or your system through the PATCH of the API edited the value.
    • system: QX computed the value.
    • unknown: QX did not record the origin. This happens, for example, with a value copied from the record of the contact or from another version of the document.
  • A field without an answer has raw_value, value and source equal to null.
  • has_problem tells whether the document is a document with a problem. reading_rules lists the reading rules that the client sees in the portal.
  • The number field contains the printed number that identifies the document. On the invoice, it is the invoice number.
To correct an answer, call PATCH /documents/{id} with fields. The value that your system writes wins over the value that the reading extracted.

7. Find the documents by the external id

The list finds the documents by the external id of the upload, without the contact. So your system does not need to keep the id of each document:
The response is a page of documents, from the newest to the oldest:
  • The match is exact: uppercase letters, spaces and leading zeros count.
  • After a file swap, the list includes the new document, with the same external_id.
  • Triage can discard the upload in favor of another document, as with an identical copy of a document that arrived before. Then the list includes the document that stayed. That document shows the external_id of its own upload, which can be null.
  • The list leaves out a document discarded with no successor. GET /documents/{id} still shows that document.

Usage rules

Follow these four rules in every integration:
  1. Send the documents of the same operation with collection_id. Each upload through the API arrives alone, and the document that it creates has no siblings in Triage. Without a collection, QX links the document to a collection of the operation only by the numbers that the document prints or cites. A document without these numbers can wait in Triage for an operator.
  2. Send a registration document, from a kyc pack, with collection_id. Without a collection, the sweep discards this document when the organization has an active operation pack. The response shows discarded equal to true and discard_reason equal to outside_pack.
  3. Check the reading.state every 10 seconds or more. Each check counts toward the limit of 300 calls per minute of the key, together with the other calls. See Authentication.
  4. Send one document per file. The API does not split the file. A file that joins the invoice, the packing list and the B/L becomes one document, of the type that the call gave.