thresh-sdk
v0.2.2
Published
Thresh document extraction API: documents in, schema-conformant JSON out.
Maintainers
Readme
thresh-sdk
TypeScript/JavaScript SDK for Thresh: documents in,
schema-conformant JSON out. Zero runtime dependencies; Node 18+ (uses global
fetch). Full API reference at https://usethresh.com/docs.
npm install thresh-sdkQuickstart
import { Thresh } from "thresh-sdk";
const client = new Thresh({ apiKey: process.env.THRESH_API_KEY! });
// Describe what you want back...
const doc = await client.upload("contract.pdf", {
fields: ["purchase_price", "closing_date"],
tables: [{ name: "monthly_charges", columns: ["item", "mrc"] }],
questions: ["Is the contract contingent upon financing?"],
});
// ...and wait for the schema-conformant result.
const result = await client.wait(doc.id);
console.log(result.fields.purchase_price);
console.log(result.field_meta.purchase_price); // { page: 2, confidence: "high" }upload accepts a filesystem path (Node), raw bytes (Uint8Array /
ArrayBuffer), or a Blob/File. Pass no spec at all to let the engine
discover the schema (auto-extraction), or template: "name" to reuse a saved
template.
Deals
Group one transaction's documents and get a single synthesized object with cross-document discrepancy checks:
const deal = await client.createDeal("7214 Sycamore Bend");
await client.upload("sale-contract.pdf", { template: "purchase", dealId: deal.id as string });
await client.upload("inspection-notice.pdf", { template: "inspection", dealId: deal.id as string });
// once every member document has completed:
await client.summarizeDeal(deal.id as string);
const synced = await client.getDeal(deal.id as string); // poll until status === "completed"Webhooks
Verify deliveries with the Thresh-Signature header:
import { verifyWebhookSignature } from "thresh-sdk";
const ok = await verifyWebhookSignature(process.env.THRESH_WEBHOOK_SECRET!, signatureHeader, rawBody);Errors
Every non-2xx response throws ThreshError with status, errorType, and
requestId from the API's error envelope. wait throws ExtractionTimeout
if the document is still processing after timeoutMs (default 5 minutes).
