Py
Language
Invoice Parsing in Python — Extract Invoice & Receipt Data with One API Call
Skip the OCR pipeline. Add invoice parsing to your Python app with ten lines of code.
Trusted by developers worldwide to automate document workflows.
✓ Free DocuParseAPI account✓ Python 3.10+✓ requests/httpx
The guide
1
Install the requests library
One dependency is all you need:
pip install requests
2
Set your API key as an environment variable
Never hardcode the key in your source code:
export DOCUPARSE_API_KEY="dex_your_key_here"
3
Send the file and read the response
Open the file in binary mode and POST it. The response is structured JSON with every field already named — no parsing required:
import os, requests
with open('invoice.pdf', 'rb') as f:
r = requests.post(
'https://docuparseapi.com/api/v1/extract',
headers={'Authorization': f'Bearer {os.environ["DOCUPARSE_API_KEY"]}'},
files={'file': f},
timeout=30
)
data = r.json()
print(data['merchant'], data['total'])Need your API key first?
20 free documents/month — no credit card
4
Handle errors by error code
Check the error code to give specific feedback:
if not data['success']:
code = data['error']['code']
if code == 'LIMIT_EXCEEDED':
print('Monthly limit reached')
elif code == 'EXTRACTION_FAILED':
print('Try a cleaner scan')
elif code == 'UNSUPPORTED_FILE_TYPE':
print('Use PDF, JPG, or PNG')5
For async apps, use httpx
If you're using FastAPI or asyncio:
import httpx
async def parse_invoice(path: str) -> dict:
async with httpx.AsyncClient(timeout=30) as client:
with open(path, 'rb') as f:
r = await client.post(
'https://docuparseapi.com/api/v1/extract',
headers={'Authorization': f'Bearer {os.environ["DOCUPARSE_API_KEY"]}'},
files={'file': f.read()}
)
return r.json()Full working example
python
import os
import requests
def parse_invoice(file_path: str) -> dict:
with open(file_path, "rb") as f:
response = requests.post(
"https://docuparseapi.com/api/v1/extract",
headers={"Authorization": f"Bearer {os.environ['DOCUPARSE_API_KEY']}"},
files={"file": (os.path.basename(file_path), f)},
timeout=30,
)
response.raise_for_status()
data = response.json()
if not data.get("success"):
raise RuntimeError(f"[{data['error']['code']}] {data['error']['message']}")
return data
# Usage
invoice = parse_invoice("invoice.pdf")
print(f"Vendor: {invoice['merchant']}")
print(f"Total: {invoice['currency']} {invoice['total']}")
print(f"Tax: {invoice['tax']}")
print(f"Date: {invoice['date']}")
for item in invoice.get("line_items", []):
print(f" {item['description']}: {item['quantity']} x {item['unit_price']}")What gets extracted
Common errors and fixes
Ready to start?
20 documents free every month. No credit card. Set up in 5 minutes.
Get Your Free API Key