Skip to content

Node.js SDK

Updated

Install#

npm install @docxtract/sdk

Node 18+. No dependencies — native fetch, FormData, and AbortSignal.timeout. TypeScript types ship with the package; no build step and no @types install.

Warning
Server-side only. A DocXtract API key is a billable credential, so bundling this for a browser ships a working key to every visitor. Call it from your own backend and expose your own endpoint to the browser. There is deliberately no browser build.

Extract a document#

import { DocXtract } from '@docxtract/sdk';

const dx = new DocXtract(process.env.DOCXTRACT_API_KEY);
const result = await dx.extract('invoice.pdf', { model: 'invoice' });

console.log(result.data.vendor);
console.log(result.get('line_items.0.hsn'));
Note
DocXtract keys use an underscore (sk_). A hyphen means the key is from a different API provider; the SDK rejects it immediately rather than letting you debug a 401.

Large PDFs#

A PDF over 3 pages returns 202 and requires the multi-page flow. extract() handles it:

const result = await dx.extract('500-page-statement.pdf', { model: 'bank_statement' });

Same call, any page count. Progress callback:

await dx.extract('big.pdf', { model: 'invoice' }, (done, total) => console.log(`${done}/${total}`));

Chunks run sequentially by design — the default rate limit is 10 requests per minute, so parallel calls only produce 429s.

Error handling#

import { RateLimitError, QuotaError, DocXtractError } from '@docxtract/sdk';

try {
  await dx.extract('invoice.pdf', { model: 'invoice' });
} catch (err) {
  if (err instanceof RateLimitError) await sleep((err.retryAfter ?? 30) * 1000);
  else if (err instanceof QuotaError) return;            // out of credits
  else if (err instanceof DocXtractError && err.retryable) requeue();
  else throw err;
}

retryable is conservative: input errors, expired jobs, and exhausted credits are all false. See Error codes for the full mapping.

Discovering document types#

await dx.models();   // costs no credits

Configuration#

OptionDefaultNotes
apiKeyRequired
baseUrlhttps://api.docxtract.ioNo /api prefix
basePath/v3.1/v3 only if pinned to the old version
timeout120000Milliseconds per request
maxRetries3Retryable errors only
chunkPauseMs0Pace chunk calls on tight rate limits

Pre-flight validation runs locally: a bad file type or oversized file fails before any request, costing no round trip and no credits.

Full reference#

sdk/node/README.md in the platform repository.