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.
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
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. - 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
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_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_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
- Each item of
fieldscontains the key, the label and the answer of the field. - A field without an answer has
valueequal tonull. - 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.
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_statusevery 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.