Here’s a complete recipe for automating invoice processing using Python and the Qomplement API. We use this in production to process ~500 invoices/month.\n\n## Setup\n\npython\nimport requests\nimport json\nimport os\n\nAPI_BASE = 'https://api.qomplement.com/v1'\nAPI_KEY = os.environ['QOMPLEMENT_API_KEY']\n\nheaders = {\n 'Authorization': f'Bearer {API_KEY}'\n}\n\n\n## Upload and Process\n\npython\ndef process_invoice(pdf_path, template_id=None):\n """Upload and process a single invoice."""\n with open(pdf_path, 'rb') as f:\n files = {'file': (os.path.basename(pdf_path), f, 'application/pdf')}\n params = {}\n if template_id:\n params['template_id'] = template_id\n \n resp = requests.post(\n f'{API_BASE}/parse',\n files=files,\n headers=headers,\n params=params\n )\n \n if resp.status_code == 200:\n return resp.json()\n else:\n raise Exception(f'Processing failed: {resp.text}')\n\n\n## Extract Key Fields\n\npython\ndef extract_invoice_fields(result):\n """Pull out the fields we care about."""\n fields = result.get('extracted_data', {})\n return {\n 'invoice_number': fields.get('Invoice Number', ''),\n 'vendor_name': fields.get('Vendor Name', ''),\n 'invoice_date': fields.get('Invoice Date', ''),\n 'due_date': fields.get('Due Date', ''),\n 'total_amount': fields.get('Total Amount', ''),\n 'line_items': fields.get('Line Items', []),\n 'confidence': result.get('confidence_score', 0)\n }\n\n\n## Batch Processing\n\npython\nimport glob\nfrom concurrent.futures import ThreadPoolExecutor\n\ndef batch_process(folder_path, max_workers=5):\n pdf_files = glob.glob(os.path.join(folder_path, '*.pdf'))\n results = []\n \n with ThreadPoolExecutor(max_workers=max_workers) as executor:\n futures = {executor.submit(process_invoice, f): f for f in pdf_files}\n for future in futures:\n try:\n result = future.result()\n invoice = extract_invoice_fields(result)\n results.append(invoice)\n print(f'Processed: {invoice["invoice_number"]}')\n except Exception as e:\n print(f'Error: {futures[future]} - {e}')\n \n return results\n\n\nHope this helps someone getting started with the API!
2 Likes
Nice recipe! I’d add exponential backoff for retries. Saved us from intermittent failures in production.
2 Likes
Used this as the base for our financial statement pipeline. Added BigQuery export and was running in an afternoon.
4 Likes