LeapOCRLeapOCR DocsAPI, SDKs, and integration guides

Common Use Cases

Real-world examples and patterns for document processing

Common Use Cases

Discover how to use LeapOCR for common document processing scenarios with production-ready examples.

Invoice Processing

Extract structured data from invoices for accounting automation.

Schema Design

{
  "type": "object",
  "properties": {
    "invoice_number": { "type": "string" },
    "invoice_date": {
      "type": "string",
      "description": "Date in YYYY-MM-DD format"
    },
    "due_date": {
      "type": "string",
      "description": "Payment due date in YYYY-MM-DD format"
    },
    "vendor": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "address": { "type": "string" },
        "tax_id": { "type": "string" }
      }
    },
    "line_items": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "description": { "type": "string" },
          "quantity": { "type": "number" },
          "unit_price": { "type": "number" },
          "total": { "type": "number" }
        }
      }
    },
    "subtotal": { "type": "number" },
    "tax": { "type": "number" },
    "total": { "type": "number" }
  },
  "required": ["invoice_number", "invoice_date", "total"]
}

Implementation

import { LeapOCR } from "leapocr";
import { z } from "zod";

const InvoiceSchema = z.object({
  invoice_number: z.string(),
  invoice_date: z.string(),
  due_date: z.string(),
  vendor: z.object({
    name: z.string(),
    address: z.string(),
    tax_id: z.string(),
  }),
  line_items: z.array(
    z.object({
      description: z.string(),
      quantity: z.number(),
      unit_price: z.number(),
      total: z.number(),
    }),
  ),
  subtotal: z.number(),
  tax: z.number(),
  total: z.number(),
});

const client = new LeapOCR({ apiKey: process.env.LEAPOCR_API_KEY });

async function processInvoice(invoiceUrl: string) {
  const result = await client.ocr.processURL(invoiceUrl, {
    format: "structured",
    model: "pro-v2", // Higher accuracy for financial docs
    schema: z.toJSONSchema(InvoiceSchema),
  });

  const invoice = await client.ocr.waitUntilDone(result.jobId);

  // Validate totals
  const calculatedTotal = invoice.data.subtotal + invoice.data.tax;
  if (Math.abs(calculatedTotal - invoice.data.total) > 0.01) {
    console.warn("Total mismatch - manual review required");
  }

  return invoice.data;
}
from leapocr import LeapOCR, ProcessOptions, Format, Model

async def process_invoice(invoice_url: str):
    async with LeapOCR(os.getenv("LEAPOCR_API_KEY")) as client:
        result = await client.ocr.process_and_wait(
            invoice_url,
            options=ProcessOptions(
                format=Format.STRUCTURED,
                model=Model.PRO_V2,
                schema={
                    "invoice_number": "string",
                    "invoice_date": "string",
                    "total": "number",
                    "line_items": [{
                        "description": "string",
                        "total": "number"
                    }]
                }
            )
        )

        # Validate and return
        return result.data
func processInvoice(ctx context.Context, client *ocr.SDK, url string) (*InvoiceData, error) {
    schema := map[string]interface{}{
        "invoice_number": "string",
        "invoice_date": "string",
        "total": "number",
    }

    job, err := client.ProcessURL(ctx, url,
        ocr.WithFormat(ocr.FormatStructured),
        ocr.WithModel(ocr.ModelProV2),
        ocr.WithSchema(schema),
    )
    if err != nil {
        return nil, err
    }

    result, err := client.WaitUntilDone(ctx, job.ID)
    return parseInvoice(result.Data), err
}

Receipt Scanning

Extract merchant and purchase information from receipts.

Simple Approach

import { z } from "zod";

const ReceiptSchema = z.object({
  merchant_name: z.string(),
  date: z.string(),
  total: z.number(),
  items: z.array(
    z.object({
      name: z.string(),
      price: z.number(),
    }),
  ),
});

const result = await client.ocr.processURL(receiptUrl, {
  format: "structured",
  model: "standard-v2", // Cost-effective for simple receipts
  schema: z.toJSONSchema(ReceiptSchema),
  instructions:
    "Extract merchant name, date, total amount, and all purchased items",
});

Advanced Schema

{
  "merchant_name": { "type": "string" },
  "date": { "type": "string" },
  "time": { "type": "string" },
  "items": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "price": { "type": "number" }
      }
    }
  },
  "subtotal": { "type": "number" },
  "tax": { "type": "number" },
  "total": { "type": "number" },
  "payment_method": { "type": "string" }
}

Form Extraction

Process standardized forms like applications, surveys, or government documents.

Multi-Page Form

For forms that span multiple pages with different sections:

import { z } from "zod";

const FormPageSchema = z.object({
  section_title: z.string(),
  fields: z.array(
    z.object({
      label: z.string(),
      value: z.string(),
    }),
  ),
});

const result = await client.ocr.processURL(formUrl, {
  format: "structured",
  model: "pro-v2",
  schema: z.toJSONSchema(FormPageSchema),
});

// Process each page
result.pages.forEach((page, index) => {
  console.log(`Page ${index + 1} - ${page.data.section_title}`);
  page.data.fields.forEach((field) => {
    console.log(`${field.label}: ${field.value}`);
  });
});

Medical Records

Extract patient information and clinical data from medical documents.

HIPAA Compliance Note

Ensure your implementation follows HIPAA compliance requirements when processing protected health information (PHI).

Example Schema

{
  "patient": {
    "type": "object",
    "properties": {
      "name": { "type": "string" },
      "date_of_birth": { "type": "string" },
      "mrn": { "type": "string", "description": "Medical Record Number" },
      "address": { "type": "string" }
    }
  },
  "visit_date": { "type": "string" },
  "provider": {
    "type": "object",
    "properties": {
      "name": { "type": "string" },
      "specialty": { "type": "string" }
    }
  },
  "diagnosis": {
    "type": "array",
    "items": { "type": "string" }
  },
  "medications": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "dosage": { "type": "string" },
        "frequency": { "type": "string" }
      }
    }
  },
  "vitals": {
    "type": "object",
    "properties": {
      "blood_pressure": { "type": "string" },
      "heart_rate": { "type": "number" },
      "temperature": { "type": "number" }
    }
  }
}

Contract Analysis

Extract key terms and parties from legal contracts.

import { z } from "zod";

const ContractSchema = z.object({
  contract_type: z.string(),
  effective_date: z.string(),
  expiration_date: z.string(),
  parties: z.array(
    z.object({
      name: z.string(),
      role: z.string(),
      address: z.string(),
    }),
  ),
  payment_terms: z.string(),
  termination_clause: z.string(),
  governing_law: z.string(),
});

const result = await client.ocr.processURL(contractUrl, {
  format: "structured",
  model: "pro-v2", // Highest accuracy for legal docs
  schema: z.toJSONSchema(ContractSchema),
});

Bank Statements

Process bank statements for financial reconciliation.

import { z } from "zod";

const BankStatementSchema = z.object({
  account_number: z.string(),
  statement_period: z.object({
    start_date: z.string(),
    end_date: z.string(),
  }),
  opening_balance: z.number(),
  closing_balance: z.number(),
  transactions: z.array(
    z.object({
      date: z.string(),
      description: z.string(),
      amount: z.number(),
      type: z.string(),
    }),
  ),
});

const result = await client.ocr.processURL(statementUrl, {
  format: "structured",
  model: "pro-v2",
  schema: z.toJSONSchema(BankStatementSchema),
});

// Validate balance
const totalDebits = result.data.transactions
  .filter((t) => t.type === "debit")
  .reduce((sum, t) => sum + t.amount, 0);

const totalCredits = result.data.transactions
  .filter((t) => t.type === "credit")
  .reduce((sum, t) => sum + t.amount, 0);

const calculatedBalance =
  result.data.opening_balance - totalDebits + totalCredits;

ID Document Verification

Extract information from driver's licenses, passports, and ID cards.

import { z } from "zod";

const IDDocumentSchema = z.object({
  document_type: z.string(),
  document_number: z.string(),
  full_name: z.string(),
  date_of_birth: z.string(),
  issue_date: z.string(),
  expiration_date: z.string(),
  address: z.string(),
  photo: z.boolean(),
});

const result = await client.ocr.processURL(idDocumentUrl, {
  format: "structured",
  model: "pro-v2",
  schema: z.toJSONSchema(IDDocumentSchema),
});

// Verify document hasn't expired
const expirationDate = new Date(result.data.expiration_date);
if (expirationDate < new Date()) {
  console.warn("Document has expired");
}

Best Practices

  1. Choose the Right Model

    • Use standard-v2 for simple documents and prototyping
    • Use pro-v2 for critical applications and complex documents
  2. Design Focused Schemas

    • Extract only the fields you need
    • Use clear, descriptive field names
    • Add descriptions for ambiguous fields
  3. Validate Results

    • Check calculated vs. extracted totals
    • Verify required fields are present
    • Flag results for manual review when confidence is low
  4. Handle Errors Gracefully

    • Implement retry logic for transient failures
    • Log failures for debugging
    • Have fallback workflows for critical processes
  5. Monitor Performance

    • Track processing times
    • Monitor credit usage
    • Set up alerts for failures

On this page