DocRouter TypeScript SDK
Type-safe TypeScript client library for interacting with docrouter.ai
Overview
The DocRouter TypeScript SDK provides type-safe access to the DocRouter API, enabling developers to integrate document processing, OCR, LLM operations, and organization management into their applications. The SDK supports both Node.js and browser environments with comprehensive TypeScript support.
- Full TypeScript type definitions for enhanced developer experience
- Support for both server-side (Node.js) and client-side (browser) usage
- Comprehensive API coverage including documents, OCR, LLM, schemas, and more
- Built-in error handling with retry logic
- Streaming support for real-time LLM operations
Installation
Prerequisites
- Node.js 16+ (for Node.js usage)
- TypeScript 4.9+ (recommended for type safety)
- DocRouter API access with appropriate tokens
Install the Package
npm install @docrouter/sdk
TypeScript Configuration
For optimal TypeScript support, ensure your tsconfig.json
includes:
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
Quick Start
Authentication
The SDK supports three authentication strategies:
1. Account Token (Server-to-Server)
Use DocRouterAccount
for account-level operations:
import { DocRouterAccount } from '@docrouter/sdk';
const client = new DocRouterAccount({
baseURL: 'https://api.docrouter.com',
accountToken: 'your-account-token-here'
});
2. Organization Token
Use DocRouterOrg
for organization-scoped operations:
import { DocRouterOrg } from '@docrouter/sdk';
const client = new DocRouterOrg({
baseURL: 'https://api.docrouter.com',
orgToken: 'your-org-token-here',
organizationId: 'org-123'
});
3. JWT Token (Browser)
Use DocRouterOrg
with JWT tokens for browser applications:
import { DocRouterOrg } from '@docrouter/sdk';
const client = new DocRouterOrg({
baseURL: 'https://api.docrouter.com',
orgToken: 'your-jwt-token-here',
organizationId: 'your-org-id'
});
Basic Usage Example
import { DocRouterOrg } from '@docrouter/sdk';
const client = new DocRouterOrg({
baseURL: 'https://api.docrouter.com',
orgToken: 'your-org-token',
organizationId: 'org-123'
});
// Upload documents
const result = await client.uploadDocuments({
documents: [
{
name: 'document.pdf',
content: fileBuffer,
type: 'application/pdf'
}
]
});
// List documents
const documents = await client.listDocuments();
// Get document details
const document = await client.getDocument({
documentId: 'doc-123',
fileType: 'pdf'
});
SDK Modules
Documents API
Manage documents in your workspace
- Upload new documents
- List documents with optional filtering
- Get document details and content
- Update document metadata
- Delete documents
OCR API
Access document OCR data
- Get OCR blocks with position data
- Get OCR text from documents
- Access document OCR metadata
- Get OCR text for specific pages
LLM API
Run and manage LLM analysis
- Run LLM analysis on documents
- Run LLM chat with streaming support
- Get LLM extraction results
- Update and verify extraction results
- List available LLM models
Schemas API
Manage extraction schemas
- Create new extraction schemas
- List existing schemas
- Get schema details
- Update schemas
- Validate data against schemas
Prompts API
Manage extraction prompts
- Create new prompts
- List existing prompts
- Get prompt details
- Update prompts
- Delete prompts
Tags API
Manage document tags
- Create new tags
- List existing tags
- Update tags
- Delete tags
Forms API
Manage document forms
- Create form definitions
- List existing forms
- Get form details
- Submit form data
- Update and delete forms
Organization Management
Account-level operations
- Organization CRUD operations
- Token creation and management
- User management
- Subscription and billing
Key Features
Type Safety
Full TypeScript support with comprehensive type definitions for all API operations, ensuring type safety and excellent IDE autocomplete support.
Error Handling
Built-in error handling with retry logic and authentication callbacks for robust API interactions.
Streaming Support
Real-time streaming for LLM operations with runLLMChatStream()
, perfect for building interactive chat interfaces.
Browser & Node.js
Works seamlessly in both Node.js server environments and browser applications with proper polyfills.
Environment Configuration
Easy configuration for different environments (development, staging, production) with support for custom HTTP client settings.
Example Use Cases
Document Processing Workflow
// Upload, OCR, and extract data
const uploadResult = await client.uploadDocuments({
documents: [{ name: 'invoice.pdf', content: buffer, type: 'application/pdf' }]
});
const docId = uploadResult.documents[0].id;
// Get OCR text
const ocrText = await client.getOCRText({ documentId: docId });
// Run LLM extraction
const llmResult = await client.runLLM({
documentId: docId,
promptRevId: 'prompt-123'
});
Streaming LLM Chat
// Real-time streaming chat
await client.runLLMChatStream({
messages: [
{ role: 'user', content: 'Analyze this document' }
],
model: 'gpt-4'
}, (chunk) => {
console.log('Received:', chunk);
}, (error) => {
console.error('Error:', error);
});
Browser File Upload
// Upload from browser file input
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
const fileBuffer = await file.arrayBuffer();
const result = await client.uploadDocuments({
documents: [{
name: file.name,
content: fileBuffer,
type: file.type
}]
});
GitHub Repository
The DocRouter TypeScript SDK is part of the docrouter.ai open source project. You can find the source code, examples, and full documentation on GitHub.
View on GitHub