unsiloed-sdk
v0.2.0
Published
JavaScript/TypeScript SDK for Unsiloed Vision API - Parse, Extract, Classify, and Split documents
Maintainers
Readme
Unsiloed JavaScript SDK
The official JavaScript/TypeScript SDK for Unsiloed - a powerful document processing platform that enables you to parse, extract, classify, and split documents with ease.
Features
- Parse: Extract structured content from documents (PDFs, images, etc.)
- Extract: Extract specific data using JSON schemas with citations
- Classify: Classify documents into predefined categories
- Split: Split document pages based on categories
- Full TypeScript Support: Complete type definitions for all APIs
- Promise-based: Works seamlessly with async/await
Installation
npm install unsiloed-sdkor with yarn:
yarn add unsiloed-sdkQuick Start
Basic Usage
import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key-here' });
const result = await client.parseAndWait({ file: 'document.pdf' });
console.log(`Total chunks: ${result.total_chunks}`);Using with JavaScript (CommonJS)
const { UnsiloedClient } = require('unsiloed-sdk');
const client = new UnsiloedClient({ apiKey: 'your-api-key-here' });
client.parseAndWait({ file: 'document.pdf' })
.then(result => console.log(`Total chunks: ${result.total_chunks}`))
.catch(err => console.error(err));Authentication
Get your API key from the Unsiloed Dashboard.
import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key-here' });Set as environment variable:
export UNSILOED_API_KEY="your-api-key"import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({
apiKey: process.env.UNSILOED_API_KEY || ''
});Usage Examples
Parse Documents
Extract structured content from any document:
import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
const result = await client.parseAndWait({
file: 'document.pdf',
mergeTables: true
});
console.log(result)Extract Data with Schema
Define exactly what data you need using JSON schema:
import { UnsiloedClient } from 'unsiloed-sdk';
const schema = {
type: 'object',
properties: {
invoice_number: {
type: 'string',
description: 'Invoice number from the document'
},
date: {
type: 'string',
description: 'Invoice date'
},
total_amount: {
type: 'number',
description: 'Total amount'
}
},
required: ['invoice_number', 'date', 'total_amount'],
additionalProperties: false
};
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
const result = await client.extractAndWait({
file: 'invoice.pdf',
schema
});
// Results include confidence scores
console.log(`Invoice #: ${result.result?.invoice_number?.value}`);
console.log(`Confidence: ${result.result?.invoice_number?.score}`);
console.log(`Total: $${result.result?.total_amount?.value}`);Classify Documents
Automatically categorize your documents:
import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
const result = await client.classifyAndWait({
file: 'document.pdf',
categories: ['Invoice', 'Receipt', 'Contract', 'Letter']
});
console.log(`Type: ${result.result?.classification}`);
console.log(`Confidence: ${result.result?.confidence}`);Split Documents
Separate multi-document files by page type:
import { UnsiloedClient } from 'unsiloed-sdk';
const categories = [
{ name: 'Cover Page', description: 'Document cover or title page' },
{ name: 'Main Content', description: 'Primary document content and body text' }
];
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
const result = await client.splitAndWait({
file: 'report.pdf',
categories,
maxWait: 1800000 // 30 minutes
});
// Check if split was successful
if (result.result?.success) {
console.log(`✓ ${result.result.message}`);
// Access the generated split files
for (const fileInfo of result.result.files || []) {
console.log(`File: ${fileInfo.name}`);
console.log(` Confidence: ${(fileInfo.confidence_score * 100).toFixed(2)}%`);
console.log(` Download: ${fileInfo.full_path}`);
}
} else {
console.log(`Split failed: ${result.result?.message}`);
}Advanced Examples
Manual Polling
If you need more control over the polling process:
import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
// Start parsing (non-blocking)
const response = await client.parse({
file: 'document.pdf',
mergeTables: true,
useHighResolution: true
});
console.log(`Job ID: ${response.job_id}`);
console.log(`Initial status: ${response.status}`);
// Poll manually with custom logic
let result;
while (true) {
result = await client.getParseResult(response.job_id);
console.log(`Current status: ${result.status}`);
if (['Succeeded', 'Failed', 'completed', 'failed'].includes(result.status)) {
break;
}
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait 2 seconds
}
if (['Succeeded', 'completed'].includes(result.status)) {
console.log(`Total chunks: ${result.total_chunks}`);
}Advanced Parse Configuration
const result = await client.parseAndWait({
file: 'contract.pdf',
mergeTables: true,
enhancedTable: true,
validateTableSegments: true,
useHighResolution: true,
ocrMode: 'auto_ocr',
segmentationMethod: 'smart_layout_detection',
keepSegmentTypes: 'Text,Table,Picture',
segmentAnalysis: {
Table: {
html: 'LLM',
markdown: 'LLM',
extended_context: true,
crop_image: 'All'
},
Picture: {
crop_image: 'All',
html: 'LLM',
markdown: 'LLM'
}
}
});Process Multiple Documents Concurrently
import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
const files = ['doc1.pdf', 'doc2.pdf', 'doc3.pdf'];
// Process all files concurrently
const results = await Promise.all(
files.map(file => client.parseAndWait({ file }))
);
results.forEach((result, i) => {
console.log(`${files[i]}: ${result.status} - ${result.total_chunks} chunks`);
});Using with Buffers
You can also pass file content as a Buffer:
import * as fs from 'fs';
import { UnsiloedClient } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
const fileBuffer = fs.readFileSync('document.pdf');
const result = await client.parseAndWait({
file: fileBuffer
});Error Handling
import {
UnsiloedClient,
AuthenticationError,
QuotaExceededError,
InvalidRequestError
} from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
try {
const result = await client.parseAndWait({ file: 'document.pdf' });
console.log(result);
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Invalid API key');
} else if (error instanceof QuotaExceededError) {
console.error('Quota exceeded:', error.responseData);
} else if (error instanceof InvalidRequestError) {
console.error('Invalid request:', error.message);
} else {
console.error('Unexpected error:', error);
}
}API Methods
All methods return Promises and can be used with async/await or .then()/.catch().
Parse
parse(options)- Start parse jobgetParseResult(jobId)- Check job statusparseAndWait(options)- Parse and wait for completion ⭐ Most common
Extract
extract(options)- Start extract jobgetExtractResult(jobId)- Check job statusextractAndWait(options)- Extract and wait for completion ⭐ Most common
Classify
classify(options)- Start classify jobgetClassifyResult(jobId)- Check job statusclassifyAndWait(options)- Classify and wait for completion ⭐ Most common
Split
split(options)- Start split jobgetSplitResult(jobId)- Check job statussplitAndWait(options)- Split and wait for completion ⭐ Most common
Tip: Use the
*AndWait()methods for simpler code. They handle polling automatically.
Configuration Options
Client Configuration
const client = new UnsiloedClient({
apiKey: 'your-api-key',
baseUrl: 'https://prod.visionapi.unsiloed.ai', // Optional
timeout: 300000 // Optional, in milliseconds (default: 5 minutes)
});Polling Configuration
const result = await client.parseAndWait({
file: 'document.pdf',
pollInterval: 2000, // Check every 2 seconds (default)
maxWait: 600000 // Maximum 10 minutes (default)
});TypeScript Support
This SDK is written in TypeScript and includes complete type definitions. You'll get full IntelliSense and type checking in supported editors.
import { UnsiloedClient, ParseResponse, ExtractResponse } from 'unsiloed-sdk';
const client = new UnsiloedClient({ apiKey: 'your-api-key' });
// TypeScript knows the return type
const result: ParseResponse = await client.parseAndWait({
file: 'document.pdf'
});Examples
Check the examples/ directory for complete working examples:
parse-example.ts- Document parsingextract-example.ts- Data extractionclassify-example.ts- Document classificationsplit-example.ts- Document splitting
Support
- Documentation: https://docs.unsiloed.ai/
- API Reference: https://docs.unsiloed.ai/api-reference
- Support Email: [email protected]
- Issues: https://github.com/unsiloed/unsiloed-sdk-js/issues
License
MIT License - see LICENSE file for details.
