Every purchase invoice of the last three years is in the accounting as a
PDF. Can the app read those in, so the analysis covers three years
instead of three months?
At a drinks wholesaler with a liquor store, the purchase invoices hang off the purchase entries in Exact Online as PDFs. Years of documents, neatly kept, and exactly what you need if you want to see price development instead of only last week's invoice.
We pulled in those three years: over 1,800 documents. It is live. The client stays anonymous and there are no amounts, prices or margins here.
Fetching them was not the problem. The API's daily limit was.
The three places an attachment lives in
Getting an attachment out of Exact takes three steps, and it takes a while to find because they do not look alike:
- The documents. A list of documents, filtered on the purchase invoice type and on a date from. Here you get the ID, the supplier, the subject and the date.
- The attachments of those documents. A separate list, with the ID, file name and size per attachment. This is the step we missed at first: a document is not its file.
- The file itself. You fetch that with an ordinary request to a fixed path, using the attachment ID.
That third step has a property that made the whole project possible: the downloads themselves do not count towards the API's daily limit. Only the two lists cost calls.
Two phases, so Exact delivers each file once
We deliberately split it in two phases:
Phase 1, fetching. Talk to Exact, write everything into a folder: one file with the documents and their attachments, plus the PDFs. Nothing is read or interpreted here.
Phase 2, reading in. Read that folder, oldest first, through exactly the same upload a person uses: recognise, read, check duplicates on the hash and on the invoice number. This phase does not talk to Exact at all, so it can also run on a development environment with a copy of that folder.
That second point is the win. You want to be able to redo the reading of three years of invoices a few times, because the first time never works in one go. Without this split, every attempt would eat your daily limit again.
Two things we built into phase 1:
- Asking for attachments per twenty documents, in one filtered call, instead of per document. Twenty because the filter sits in the URL and that URL has a limit somewhere.
- A reserve on the daily limit. With six hundred calls left, the run stops politely and continues next time. Those six hundred are for the nightly sync and the work of the day; those must not stall because somebody imported an archive.
The whole fetch cost about 126 calls. Out of a daily limit of five thousand.
The bug we found along the way
And then we looked at the daily sync, because it was occasionally hitting the limit. That turned out to have nothing to do with the archive.
The sync looked up the matching purchase order in Exact for every invoice number. With the analysis covering three years that was a few thousand, so that one step did around 3,600 calls per run. Against a daily limit of five thousand.
The annoying part is that this never showed up as an error. The run just worked, only slowly, and the limit only became a problem once three years of invoices were in it. The fix is small: only look up the invoices that take part in the analysis.
That is the pattern we now look for in every integration: a step that makes one call per row is always a problem eventually. Not because it is wrong, but because the number of rows grows and the limit does not.
What came next: the analysis itself
Putting three years of invoices into the app is one thing. Running the analysis over them is another.
With everything in it, a cold calculation took about 19 seconds, and a web request is allowed 30 here. That is not enough headroom, and the number of signals jumped from around 1,500 to over 12,000; not usable for a human being.
The solution is a boundary that has nothing to do with technology and everything to do with meaning: archive invoices take part in the analysis 180 days back. Older than that they are still there and still viewable, but they do not feed the signals. A price difference from two years ago is not a signal, it is history.
The heavy calculation, about ten seconds, then moved to the background worker with a cache version of its own, so nobody is waiting for it on the first click.
What we would take to the next one
Split fetching and reading in. It is slightly more work and it pays off on day one, because you will read in more often than you think.
Find out what does and does not count towards your limit. That downloading the files was free and listing them was not decided the whole design.
Make an import run leave a reserve. An import must never stop the daily work.
Look for steps that make one call per row. That is the bug you do not have yet.
Pick a window on meaning, not on speed. Old data is allowed to be there without speaking up.
Common mistakes
Making one call per document for its attachments. Ask for them in groups, with one filtered call.
Mistaking a document for its file. Those are two lists and a download.
Letting an import run go all the way to the limit. Then the nightly run falls over, and you find out the next morning.
Including everything in your analysis because it is there now. Twelve thousand signals is the same as no signals.
Ignoring the limit headers in the response. They tell you what is left; read them and stop in time.
What an integration on Exact Online can do is on connecting Exact Online. What else we learned about that API is in what the docs do not say and in keeping customer prices in sync. If you want this for your own bookkeeping, have a look at custom software.