📜

JavaScript/TypeScript SDK — OCR for Node.js

Extract document data from JavaScript/TypeScript apps. Works with Node.js, Deno, Bun, and browser fetch.

Quick Start

Use fetch or axios to call Cargoffer OCR from any JS runtime.

Node.js (fetch)

javascript
const API = 'https://ocr.cargoffer.com';
const KEY = 'ocr_your_api_key';
const fs = require('fs');

async function extractInvoice(pdfPath) {
  // 1. Upload
  const form = new FormData();
  form.append('file', fs.createReadStream(pdfPath));
  const upload = await fetch(`${API}/api/upload`, {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${KEY}` },
    body: form,
  });
  const { job } = await upload.json();

  // 2. Analyze
  const analyze = await fetch(`${API}/api/analyze/${job}`, {
    method: 'POST',
    headers: { 'Authorization': `Bearer ${KEY}` },
  });
  const data = await analyze.json();

  return Object.values(data.results)[0];
}

extractInvoice('invoice.pdf').then(inv => {
  console.log(`Total: ${inv.totals.total}€`);
});

TypeScript Types

typescript
interface InvoiceResult {
  document_type: 'invoice';
  provider: string;
  invoice_number: string;
  issue_date: string;
  customer: { name: string; nif: string };
  totals: { total_base: number; total_iva: number; total: number };
  line_items: Array<{
    concept: string;
    quantity: number;
    base: number;
    iva_pct: number;
    total: number;
  }>;
}

interface AnalyzeResponse {
  job: string;
  results: Record<string, InvoiceResult>;
}

Ready to try it?

Start Free →