Setup
Welcome to the official Node.js SDK documentation for Nanonets. This guide will help you integrate Nanonets into your Node.js applications for intelligent document processing, data extraction, and workflow automation.
The Nanonets Node.js SDK provides a simple and intuitive interface to interact with the Nanonets API.
Installation
Install the SDK using npm:
npm install nanonets
Authentication
Sign up and get your API key from your Nanonets dashboard.
The SDK uses your API key for authentication. You can set it in two ways:
- Environment variable:
export NANONETS_API_KEY='your_api_key'
- Direct initialization:
const NanonetsClient = require('nanonets');
const client = new NanonetsClient('your_api_key');
Quick Start
Here's a quick example of how to use the SDK:
const NanonetsClient = require('nanonets');
const client = new NanonetsClient('your_api_key');
(async () => {
// Create a workflow
const workflow = await client.workflows.create({
description: 'SDK Example Workflow',
workflow_type: '' // Instant learning
});
const workflowID = workflow.workflow_id || workflow.id;
// Configure fields and table headers
const fields = [
{ name: 'invoice_number' },
{ name: 'total_amount' },
{ name: 'invoice_date' }
];
const table_headers = [
{ name: 'item_description' },
{ name: 'quantity' },
{ name: 'unit_price' },
{ name: 'total' }
];
await client.workflows.setFields(workflowID, { fields, table_headers });
// Upload a document to process
const uploadResult = await client.documents.upload(workflowID, {
file: '/path/to/document.pdf',
async: false,
metadata: { test: 'true' }
});
console.log('Upload result:', uploadResult);
})();
Features
The SDK provides comprehensive functionality for:
Error Handling
The SDK provides custom error classes for better error handling:
const { NanonetsError, AuthenticationError, ValidationError } = require('nanonets');
try {
const workflow = await client.workflows.create({...});
} catch (error) {
if (error instanceof AuthenticationError) {
console.error("Authentication failed:", error.message);
} else if (error instanceof ValidationError) {
console.error("Invalid input:", error.message);
} else if (error instanceof NanonetsError) {
console.error("An error occurred:", error.message);
}
}
Best Practices
Resource Management
// Use async/await for better resource management
async function processDocument(workflow, document) {
try {
return await workflow.processDocument(document);
} finally {
// Cleanup if needed
}
}Batch Processing
// Process documents in batches
async function processBatch(workflow, documents, batchSize = 10) {
const results = [];
for (let i = 0; i < documents.length; i += batchSize) {
const batch = documents.slice(i, i + batchSize);
const batchResults = await Promise.all(
batch.map(doc => workflow.processDocument(doc))
);
results.push(...batchResults);
}
return results;
}Error Recovery
const { retry } = require('nanonets/utils');
// Retry with exponential backoff
const processWithRetry = retry(
async (workflow, document) => {
return await workflow.processDocument(document);
},
{
retries: 3,
factor: 2,
minTimeout: 1000,
maxTimeout: 10000
}
);
For more detailed information about specific features, please refer to the following sections: