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

Get Your Free API KeyView API Docs
Free DocuParseAPI accountPython 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
Get Free API Key →
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

merchantThe vendor or store name
totalThe final charged amount including tax
subtotalThe pre-tax amount
taxThe tax amount charged
dateInvoice or transaction date (ISO 8601)
due_datePayment due date, or null if not present
invoice_idInvoice number or receipt ID from the document
currencyISO 4217 currency code (USD, EUR, GBP, etc.)
payment_methodCard type, cash, or other payment method
line_itemsArray of items with description, quantity, unit_price, total

Common errors and fixes

MISSING_API_KEYAuthorization header missingAdd header: Authorization: Bearer YOUR_KEY
INVALID_API_KEYKey not recognisedCheck key in dashboard — regenerate if needed
LIMIT_EXCEEDEDMonthly document limit reachedUpgrade at docuparseapi.com/pricing
UNSUPPORTED_FILE_TYPEFile format not supportedUse PDF, JPG, or PNG only
FILE_TOO_LARGE_AUTHENTICATEDFile exceeds 10 MBCompress the file before sending
EXTRACTION_FAILEDDocument could not be parsedTry a cleaner scan or different file

Ready to start?

20 documents free every month. No credit card. Set up in 5 minutes.

Get Your Free API Key