curl and five endpoints, in this order:
GET /packsshows the pack and the document type of the invoice.POST /contactscreates the contact.POST /collectionscreates the collection of the contact.POST /documentssends the invoice to the collection.GET /documents/{id}shows the progress of the reading and, at the end, the fields of the invoice.
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.
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
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
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_statusfield shows the progress of the lookup. - The organization can already have a contact with this CNPJ. In that case, the response is
409with the codecontact_exists, and thecontactfield contains the contact. Use theidof that contact in the next steps.
3. Create the collection
201 response contains the collection, with an empty slot for each requirement of the pack:
- The collection starts in the
assemblingstate, in assembly. - The
protocolis the name of the collection. When the pack has a protocol rule, QX composes the name from the fields of the documents, such asACME TRADING MXA188-UXA837. Without documents or without a rule, the protocol is the generated protocol, such asDOC-2026-00123. - The protocol changes when the documents of the collection change, in every state of the collection. Keep the
idas 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
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_310from the collection. Use theidof 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
CallGET /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
- Each item of
fieldscontains the key, the label, the text of the field inraw_value, and the typed value invalue. value_typegives the shape of the value:text,integer,decimal,money,date,boolean,listortable.- 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,
currencyisnull. - A text that does not convert to the type of the field, such as the date 31/02/2026, stays in
raw_value, withvalueequal tonull. sourcegives the recorded origin of the value:reading: the reading.operator: an operator, the client on the portal, or your system through thePATCHof 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,valueandsourceequal tonull. has_problemtells whether the document is a document with a problem.reading_ruleslists the reading rules that the client sees in the portal.- The
numberfield contains the printed number that identifies the document. On the invoice, it is the invoice number.
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 theid of each document:
- 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_idof its own upload, which can benull. - 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:- 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. - Send a registration document, from a
kycpack, withcollection_id. Without a collection, the sweep discards this document when the organization has an active operation pack. The response showsdiscardedequal totrueanddiscard_reasonequal tooutside_pack. - Check the
reading.stateevery 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. - 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.