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.

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:
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.
  • 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 201 response contains the new document. 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_status reaches one of these values: The values not_started, extracting, enriching and validating tell that the reading did not end yet. 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 and the answer of the field.
  • A field without an answer has value equal to null.
  • 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.

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_status 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.