Extract & Poll

Extraction is asynchronous. When you submit a document for extraction, Tracore creates a run and returns immediately with a 202 status and a run ID. You then poll the run until it reaches a terminal status.

Start extraction

const run = await client.extract('my-workspace', 'invoice', {
  documentId: 'doc_abc123',
});

console.log(run.id); // "run_xyz789"
console.log(run.status); // "pending"

Auto-polling

Pass poll: true to have the SDK automatically poll until the run completes or fails:

const run = await client.extract('my-workspace', 'invoice', {
  documentId: 'doc_abc123',
  poll: true,
});

// Resolves only when the run reaches a terminal status
console.log(run.status); // "completed"
console.log(run.result); // extracted data

Custom poll options

Control the polling interval and timeout:

const run = await client.extract('my-workspace', 'invoice', {
  documentId: 'doc_abc123',
  poll: true,
  pollIntervalMs: 2000, // check every 2 seconds (default: 1000)
  pollTimeoutMs: 120000, // give up after 2 minutes (default: 60000)
});

Manual polling

If you prefer to control the polling loop yourself:

const run = await client.extract('my-workspace', 'invoice', {
  documentId: 'doc_abc123',
});

let result = run;

while (result.status === 'pending' || result.status === 'processing') {
  await new Promise((resolve) => setTimeout(resolve, 1000));
  result = await client.runs.get(result.id);
}

if (result.status === 'completed') {
  console.log(result.result);
} else {
  console.error('Extraction failed:', result.error);
}

Inline upload and extract

You can upload a file and start extraction in one request:

import { readFileSync } from 'node:fs';

const file = new Blob([readFileSync('./invoice.pdf')], {
  type: 'application/pdf',
});

const run = await client.extract('my-workspace', 'invoice', {
  file,
  fileName: 'invoice.pdf',
  poll: true,
});

Run statuses

StatusDescription
pendingRun created, waiting to be picked up
processingExtraction in progress
completedExtraction finished successfully, results available
failedExtraction encountered an unrecoverable error
validation_failedExtraction completed but results did not match the schema

Error handling

try {
  const run = await client.extract('my-workspace', 'invoice', {
    documentId: 'doc_abc123',
    poll: true,
  });

  if (run.status === 'completed') {
    console.log(run.result);
  } else if (run.status === 'validation_failed') {
    console.error('Validation failed:', run.error);
  } else {
    console.error('Extraction failed:', run.error);
  }
} catch (error) {
  // Network or timeout errors
  console.error('Request failed:', error);
}