JavaScript SDK

PageIndex JavaScript SDK

Install

npm install @pageindex/sdk

Initialize

To get started, generate your API key from the PageIndex Developer Dashboard.

import { PageIndexClient } from "@pageindex/sdk";

const client = new PageIndexClient({ apiKey: "YOUR_API_KEY" });

Configuration Options

NameTypeRequiredDescriptionDefault
apiKeystringyesPageIndex API key-
apiUrlstringnoCustom API base URLhttps://api.pageindex.ai
folderScopestringnoRestrict all operations to a specific folder and its subfolders-

Quick Start

Upload a PDF, retrieve its tree structure, and ask a question:

import { PageIndexClient } from "@pageindex/sdk";
import { readFileSync } from "fs";

const client = new PageIndexClient({ apiKey: "YOUR_API_KEY" });

// 1. Upload a document
const file = readFileSync("./2023-annual-report.pdf");
const { doc_id } = await client.api.submitDocument(file, "2023-annual-report.pdf");
console.log("Document ID:", doc_id);

// 2. Get tree structure (includes processing status)
const tree = await client.api.getTree(doc_id);
if (tree.status === "completed") {
  console.log("Tree:", tree.result);
}

// 3. Chat with the document
const response = await client.api.chatCompletions({
  messages: [{ role: "user", content: "What are the key findings?" }],
  doc_id,
});
console.log(response.choices[0].message.content);

// 4. Stream a response
const stream = await client.api.chatCompletions({
  messages: [{ role: "user", content: "Summarize this document" }],
  doc_id,
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

See Document Processing for upload and management, and Chat API for the full chat reference.

Error Handling

All SDK errors are instances of PageIndexError:

import { PageIndexClient, PageIndexError } from "@pageindex/sdk";

try {
  const doc = await client.api.getDocument("invalid-id");
} catch (error) {
  if (error instanceof PageIndexError) {
    console.log(error.code);       // "NOT_FOUND" | "UNAUTHORIZED" | "RATE_LIMITED" | ...
    console.log(error.message);    // Human-readable description
    console.log(error.statusCode); // HTTP status code
    console.log(error.details);    // Additional context
  }
}

Error Codes

CodeHTTP StatusDescription
UNAUTHORIZED401Invalid or missing API key
NOT_FOUND404Document or resource not found
RATE_LIMITED429Too many requests
USAGE_LIMIT_REACHED403Account usage limit exceeded
INVALID_INPUT400Invalid parameters
SERVICE_UNAVAILABLE503Service temporarily unavailable
INTERNAL_ERROR500Unexpected server error

SDK Full Reference