space ocr
ArticlesDocs
developer

Parse Invoices in Python: A REST API Guide

Extract structured data from invoices using Python. A guide to calling a simple REST API to get JSON fields, line items, and verifiable coordinates for any invoice image.

7 min read· 2026-06-27

Getting structured data from invoices is a common but frustrating task. You might have a folder of scanned receipts or vendor invoices as JPEGs or PNGs, and you need to pull out the invoice number, total amount, and each line item for accounting or analysis. Most OCR tools dump a wall of text, leaving you to piece the structure back together with fragile regular expressions.

There is a more direct way: a REST API call that returns the structured JSON you need, with every value traced back to its exact location on the page. The interactive demo below shows the final result. Click any field on the left to see its position highlighted on the invoice.

Invoice with extracted-field bounding boxes
Verified fields
Invoice

Each value with a box carries a verified on-page location — bbox + 4-point vertices + match_ratio — on a 0–1000 normalized grid (0,0 top-left → 1000,1000 bottom-right), the same shape the live API returns. Hover a field to trace it back to the pixels it came from.

An example invoice for OCR processing
An invoice image — the input to /ocr/fields.

space-ocr is not a library you install, but a plain HTTP service. The process is simple: take an invoice image, send it to the POST /ocr/fields endpoint, and define the schema you want back. You can specify fields like invoice_number, total_due, and a table of line_items with columns for description and price. The API returns a clean JSON object matching your schema.

Every extracted value is linked to its bounding box on the original image, providing a verifiable audit trail.

The response for each field includes not just the text value, like an invoice_number of "20250430-001", but also its coordinates and a confidence score. This match_ratio tells you how well the extracted text aligned with the characters found on the page, giving you a reliable signal for data quality. The Python script below shows how to do this from scratch.

parse_invoice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import base64
import requests
import json

# Get your API key from an environment variable
API_KEY = os.environ.get("SPACE_OCR_API_KEY")
IMAGE_PATH = "./path/to/your/invoice.jpg" # Path to your invoice image
API_URL = "https://api.space-ocr.com/ocr/fields"

if not API_KEY:
    raise ValueError("API key not found. Set the SPACE_OCR_API_KEY environment variable.")

# Define the schema for the data you want to extract
# For line items, use type 'array' and define columns in 'children'
fields_schema = [
    {"name": "supplier", "type": "string", "description": "The name of the company that issued the invoice."},
    {"name": "invoice_number", "type": "string"},
    {"name": "issue_date", "type": "date"},
    {"name": "total_due", "type": "number"},
    {
        "name": "line_items",
        "type": "array",
        "description": "All items listed in the invoice table.",
        "children": [
            {"name": "item_description", "type": "string"},
            {"name": "unit_price", "type": "number"},
            {"name": "quantity", "type": "number"},
            {"name": "line_total", "type": "number"}
        ]
    }
]

# Read the image file and encode it in base64
with open(IMAGE_PATH, "rb") as image_file:
    base64_image = base64.b64encode(image_file.read()).decode('utf-8')

headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json"
}

payload = {
    "image": base64_image,
    "imageType": "base64",
    "fields": fields_schema
}

print(f"Sending request for {IMAGE_PATH}...")
response = requests.post(API_URL, headers=headers, json=payload)

if response.status_code == 200:
    result = response.json()['data']
    print("\n--- Extracted Invoice Data ---")
    for field_name, field_data in result.items():
        if isinstance(field_data, list):
            print(f"\n{field_name}:")
            for i, row in enumerate(field_data):
                print(f"  - Item {i+1}:")
                for col_name, col_data in row.items():
                    print(f"    {col_name}: {col_data['value']} (Confidence: {col_data['match_ratio']:.2f})")
        else:
            print(f"{field_name}: {field_data['value']} (Confidence: {field_data['match_ratio']:.2f})")
            # Bounding box is available at field_data['bbox']
            # e.g., print(f"  bbox: {field_data['bbox']}")
else:
    print(f"Error: {response.status_code}")
    print(response.text)
✓ Verified

To ensure accuracy, space-ocr doesn't just trust the language model's output. The model returns the text value plus hints about which words it used on the page. The engine then performs a character-by-character match of the extracted value against the page's actual OCR symbols. This process generates the match_ratio confidence score (where ≥ 0.85 is a high-confidence match) and the final bounding box. All coordinates (xmin, ymin, xmax, ymax) are normalized to a 0-1000 scale, making them independent of the original image resolution.

The API is priced per call, not per field or page complexity. It costs $0.05 per image, and requests that fail to produce a result are not charged. New accounts get 100 free scans each month to start.

  1. Get Your API Key
    Sign up for a free space-ocr account and find your API key in the dashboard settings. The key will start with 'spocr_'.
  2. Prepare Your Invoice Image
    Save your invoice as a common image file, such as a JPEG or PNG. Note the file path for your script.
  3. Define Your Data Schema
    Create a JSON array in your Python script that defines the names and types of the fields you want to extract, including any line-item tables.
  4. Write the Python Script
    Using the requests library, write a script to read the image, convert it to base64, and POST it to https://api.space-ocr.com/ocr/fields with your API key in the Authorization header.
  5. Run and Process the JSON
    Execute your script. It will print the structured JSON response, which you can then load into your database, accounting software, or analytics tool.
Do I need to install an SDK or library?
No, space-ocr is a standard HTTP REST service. You can call it from any language or tool that can make HTTP requests, like cURL or Python's requests library.
Can I process PDF invoices?
The API endpoint accepts raster image formats like JPEG, PNG, or WebP. To process a PDF file, you first need to render each page into an image in your own code before sending it to the API. The space-ocr web application handles this conversion for you automatically.
What are the coordinates in the response?
The API returns 0-1000 normalized coordinates, where (0,0) is the top-left corner and (1000,1000) is the bottom-right. The bbox object contains integer keys: xmin, ymin, xmax, and ymax. This system is independent of the source image's pixel dimensions.
How do I handle line items or tables?
In your `fields` schema, define a field with `type: 'array'`. Then, specify the columns you want to extract for each row inside the nested `children` property. The API will return an array of objects for that field.
What if I don't know the invoice layout beforehand?
For situations where you don't have a predefined template, you can set `autoFields: true` in your request body instead of providing a `fields` schema. The service will attempt to detect the document's structure and extract relevant fields automatically.
What does the match_ratio score mean?
It's a confidence score from 0.0 to 1.0 that represents how completely the extracted text characters were matched against the symbols physically detected on the page. Scores of 0.85 and above are considered high-confidence matches.

Start parsing invoices in minutes.

Get your API key and 100 free scans a month. No credit card required.

Related