Skip to main content
The /api/ocr-structured-v4 endpoint provides the most advanced invoice extraction with comprehensive India GST support, multi-page PDF processing, and sophisticated reconciliation logic.

Endpoint

POST /api/ocr-structured-v4

Request Body

imageBase64
string
Base64-encoded image data or data URL. Required if pdfUrl and pdfBase64 are not provided.Formats:
  • Bare base64: iVBORw0KGgoAAAANS...
  • Data URL: data:image/png;base64,iVBORw0KGgo...
mimeType
string
default:"image/png"
MIME type of the image (e.g., image/png, image/jpeg, image/webp)
pdfUrl
string
Publicly accessible URL to a PDF document. Required if imageBase64 and pdfBase64 are not provided.
pdfBase64
string
Base64-encoded PDF data or data URL. Required if imageBase64 and pdfUrl are not provided.
filename
string
default:"invoice.pdf"
Optional filename for PDF documents
model
string
Override the default model. Default: google/gemini-2.0-flash if OPENROUTER_MODEL not set
annotations
object
Pass-through OpenRouter annotations to avoid re-parsing costs on subsequent requests
plugins
array
Override PDF parsing plugins. Advanced use only.Default:
[
  {
    "id": "file-parser",
    "pdf": { "engine": "pdf-text" }
  }
]

Response Schema

The v4 schema provides comprehensive invoice data with normalized GST calculations:

Document Level

doc_level
object
Invoice header metadata

Items Array

items
array
Line items with detailed breakdowns

Header Discounts

header_discounts
array
Invoice-level discounts applied before charges and GST

Charges

charges
array
Additional charges (Freight, Packing, Insurance, etc.)

TCS

tcs
object
Tax Collected at Source

Round-Off

round_off
number
Rounding adjustment (typically ±1.00)

Totals

totals
object
Computed invoice totals

Printed Values

printed
object
Values extracted from printed subtotals/tables

Reconciliation

reconciliation
object
Calculation verification results

Metadata

meta
object
Extraction metadata

Example Requests

curl -X POST https://your-domain.com/api/ocr-structured-v4 \
  -H "Content-Type: application/json" \
  -d '{
    "imageBase64": "data:image/png;base64,iVBORw0KGgo...",
    "model": "google/gemini-2.0-flash"
  }'

Example Response

Success (200) - Excerpt
{
  "doc_level": {
    "supplier_name": "Tech Solutions Pvt Ltd",
    "supplier_gstin": "27AABCT1234C1Z5",
    "invoice_number": "INV-2024-001",
    "invoice_date": "2024-01-15",
    "place_of_supply_state_code": "27",
    "buyer_gstin": "27AABCU9603R1ZM",
    "currency": "INR"
  },
  "items": [
    {
      "name": "Premium Widget",
      "hsn": "8517",
      "qty": 10,
      "uom": "PCS",
      "rate_ex_tax": 1000.00,
      "discount": {
        "d1_pct": 10.0,
        "d2_pct": 5.0,
        "flat_per_unit": null,
        "effective_pct": 14.5,
        "amount": 1450.00
      },
      "gst": {
        "rate": 18.0,
        "cgst": 769.50,
        "sgst": 769.50,
        "igst": 0.00,
        "amount": 1539.00
      },
      "totals": {
        "line_ex_tax": 8550.00,
        "line_inc_tax": 10089.00
      },
      "raw": {
        "rate_printed": 1000.00,
        "price_mode_detected": "WITHOUT_TAX",
        "discount_pct_printed": 10.0,
        "special_discount_pct_printed": 5.0,
        "amount_printed": 8550.00
      },
      "ui_hints": {
        "qty_x_rate_text": "10 × ₹1,000.00",
        "discount_row": { "label": "Discount", "percent": 14.5, "amount": 1450.00 },
        "tax_row": { "label": "GST @18%", "percent": 18.0, "amount": 1539.00 }
      },
      "confidence": 0.95
    }
  ],
  "header_discounts": [],
  "charges": [
    {
      "label": "Freight",
      "ex_tax": 500.00,
      "gst_rate_hint": 18.0,
      "gst_amount": 90.00,
      "inc_tax": 590.00,
      "taxable": true
    }
  ],
  "tcs": { "rate": 0.0, "amount": 0.0, "base_used": "grand_total_before_tcs" },
  "round_off": -0.00,
  "totals": {
    "items_ex_tax": 8550.00,
    "header_discounts_ex_tax": 0.0,
    "charges_ex_tax": 500.00,
    "taxable_ex_tax": 9050.00,
    "gst_total": 1629.00,
    "grand_total": 10679.00
  },
  "printed": {
    "taxable_subtotal": 9050.00,
    "gst_total": 1629.00,
    "hsn_tax_table": [
      { "hsn": "8517", "taxable": 9050.00, "cgst": 814.50, "sgst": 814.50, "total": 10679.00 }
    ],
    "grand_total": 10679.00
  },
  "reconciliation": {
    "error_absolute": 0.00,
    "alternates_considered": [],
    "warnings": []
  },
  "meta": {
    "pages_processed": 1,
    "language": "en",
    "overall_confidence": 0.92
  }
}

Reconciliation Logic

The v4 reconciliation engine (lib/invoice_v4.ts) performs sophisticated validation:
  1. Price Mode Detection: Determines if printed rates are WITH_TAX or WITHOUT_TAX by comparing with GST summary
  2. Sequential Discounts: Applies d1 then d2: effective = d1 + d2 - (d1 × d2)
  3. Line Calculation: line_ex_tax = qty × rate_ex_tax × (1 - effective_pct) - flat_discount
  4. Header Discounts: Applied to subtotal before charges and GST
  5. Charges: Added with GST (inherits weighted avg rate if not specified)
  6. TCS: Applied after GST
  7. Round-Off: Applied last
  8. Validation: Compares against printed totals and HSN tables
  9. Alternate Strategies: Tests multiple calculation approaches, selects lowest error

Schema Constraints

The system prompt enforces:
  • GST Slabs: 0, 0.25, 3, 5, 12, 18, 28 (as percentages, not decimals)
  • UQC Codes: Valid GST unit codes only (NOS, PCS, KGS, LTR, MTR, etc.)
  • Date Format: YYYY-MM-DD (ISO 8601)
  • Decimal Precision: 2 decimals in final JSON, higher precision internally
  • Unknown Values: null with reduced confidence
  • Round-Off: Expected |round_off| ≤ 1.00 unless explicitly printed

Advanced Features

Multi-Page PDF Processing

For PDFs, the endpoint:
  1. Processes all pages
  2. Prefers HSN tax tables and summaries (often on page 2+)
  3. Uses tables as reconciliation anchors
  4. Sets meta.pages_processed count

Price Mode Ambiguity

When unclear if rates include GST:
  1. Tests BOTH WITH_TAX and WITHOUT_TAX modes
  2. Compares each against printed totals, HSN tables, and subtotals
  3. Prefers mode with lowest absolute error AND small round-off (≤ 1.00)
  4. Stores detected mode in raw.price_mode_detected

Confidence Scoring

Each item includes confidence (0.0-1.0) based on:
  • OCR quality of relevant fields
  • Presence of validation anchors (HSN table, subtotals)
  • Calculation consistency
  • meta.overall_confidence aggregates across document

OpenRouter Annotations

To avoid re-parsing costs on retries:
  1. First request processes the PDF
  2. OpenRouter returns annotations in response metadata
  3. Pass annotations in subsequent requests to reuse parsed content
  4. Significantly faster and cheaper for large PDFs

Migration from Legacy

Upgrading from /api/ocr-structured:
Legacy FieldV4 EquivalentNotes
voucher.invoice_numberdoc_level.invoice_numberDirect mapping
voucher.invoice_datedoc_level.invoice_dateFormat: dd-mm-yyyy → YYYY-MM-DD
items[].priceitems[].rate_ex_taxSame concept
items[].tax_rateitems[].gst.ratePercentage (18, not 0.18)
items[].discount_rateitems[].discount.effective_pctV4 supports multi-level
voucher.invoice_discountheader_discounts[]Array for sequential discounts
voucher.additional_chargescharges[]Enhanced structure
party.*Not in v4Extract from buyer_gstin if needed
voucher.reconciliation.statusreconciliation.error_absoluteNumeric error instead of binary status

Best Practices

1

Use PDF for Multi-Page Invoices

PDFs preserve better quality and allow processing multiple pages with tax summaries
2

Validate HSN Tax Tables

Check printed.hsn_tax_table matches totals for high-confidence extraction
3

Monitor Reconciliation Error

Accept error_absolute ≤ 1.00 as normal rounding, investigate larger differences
4

Use Confidence Scores

Flag items with confidence < 0.7 for manual review
5

Cache Annotations for PDFs

Store and reuse annotations from response to avoid re-parsing same PDFs

Limitations

  • Requires India GST format (GSTIN, HSN, CGST/SGST/IGST)
  • Optimized for myBillBook-style invoices
  • May struggle with highly custom layouts
  • Confidence scoring requires validation anchors
  • TCS support is basic (single rate, single base)

Next Steps

Reconciliation Engine

Deep dive into v4 reconciliation logic

Integration Guide

Build a complete v4 extraction workflow

UI Components

Display v4 data with pre-built components

Testing

Test your v4 integration

Build docs developers (and LLMs) love