# TypeScript SDK

> The official SDK wraps the REST calls with typed results and errors.

Page: https://doxtract.manoonlabs.com/docs/api/sdk

Works in Node, Bun and Deno. `zod` is a peer dependency, only needed for Zod schemas. [SDK on GitHub](https://github.com/manoonlabs/doxtract-js) · [npm](https://www.npmjs.com/package/@manoonlabs/doxtract)

## Install

**Install**

```bash
npm install @manoonlabs/doxtract
```

## Examples

**Parse (no schema)**

```ts
import { Doxtract } from "@manoonlabs/doxtract";

const doxtract = new Doxtract({ apiKey: "mldxt_..." });

const { values } = await doxtract.parse("invoice.pdf");

// The model picks the keys. Pass schema to choose them yourself (same JSON field name as REST).
console.log(values);
```

**Parse (Zod)**

```ts
import { Doxtract } from "@manoonlabs/doxtract";
import { z } from "zod";

const doxtract = new Doxtract({ apiKey: "mldxt_..." });

const Invoice = z.object({
  vendor: z.string().nullable(),
  invoiceNumber: z.string().nullable(),
  total: z.number().nullable(),
  lineItems: z.array(
    z.object({ description: z.string(), amount: z.number() }),
  ),
});

const { values, citations } = await doxtract.parse("invoice.pdf", {
  schema: Invoice,
  citations: true,
});

values.total; // number | null
citations?.["total"]; // where the total was read on the page
```

**Parse (plain schema)**

```ts
import { Doxtract } from "@manoonlabs/doxtract";

const doxtract = new Doxtract(); // reads DOXTRACT_API_KEY

const { values } = await doxtract.parse<{
  vendor: string | null;
  total: number | null;
}>("invoice.pdf", {
  schema: {
    type: "object",
    properties: { vendor: { type: "string" }, total: { type: "number" } },
  },
});
```

**Text and export**

```ts
import { writeFile } from "node:fs/promises";
import { Doxtract } from "@manoonlabs/doxtract";

const doxtract = new Doxtract();

// Plain text instead of a file, and the spreadsheet next to the JSON.
const result = await doxtract.parse(
  { text: "Invoice INV-1042 from Northwind Supply. Total 1280.50 USD." },
  { schema: "invoice", export: "xlsx" },
);

if (result.export) {
  await writeFile(result.export.filename, result.export.data);
}
```

**Schemas (schemas API)**

```ts
import { Doxtract } from "@manoonlabs/doxtract";

const doxtract = new Doxtract();

const schemas = await doxtract.schemas.list();

await doxtract.schemas.create({
  name: "Invoice",
  schema: {
    type: "object",
    properties: { total: { type: "number" } },
  },
});
await doxtract.schemas.get("invoice");
await doxtract.schemas.update("invoice", {
  name: "Invoice v2",
  schema: {
    type: "object",
    properties: { total: { type: "number" }, paid: { type: "boolean" } },
  },
});
```

**Key and errors**

```ts
import {
  Doxtract,
  DoxtractError,
  InsufficientCreditsError,
  RateLimitError,
} from "@manoonlabs/doxtract";

const doxtract = new Doxtract();
const { credits_remaining } = await doxtract.whoami();

try {
  await doxtract.parse("invoice.pdf", { schema: "invoice" });
} catch (error) {
  if (error instanceof RateLimitError) console.error(`Retry in ${error.retryAfter}s`);
  else if (error instanceof InsufficientCreditsError) console.error(error.upgrade);
  else if (error instanceof DoxtractError) console.error(error.code, error.message);
  else throw error;
}
```
