conversiontools
v2.0.1
Published
Modern TypeScript/JavaScript library to convert files using Conversion Tools API at https://conversiontools.io - supports ESM & CommonJS with full TypeScript types
Maintainers
Readme
Conversion Tools Node.js Client (v2)
Modern TypeScript/JavaScript library for converting files using the Conversion Tools API. Convert between 100+ file formats including XML, JSON, Excel, PDF, CSV, images, audio, video, and more.
✨ What's New in v2
- 🎯 Full TypeScript Support - Complete type definitions with IntelliSense
- 📦 Dual Module Support - Works with both ESM and CommonJS
- 🚀 Streaming Support - Handle large files efficiently
- 📊 Progress Tracking - Monitor upload, conversion, and download progress
- 🔄 Smart Retry Logic - Automatic retry with exponential backoff
- 🎨 Better DX - Cleaner API with improved error handling
- 🔌 Webhook Support - Async notifications for task completion
- 🧪 Sandbox Mode - Unlimited testing without consuming quota
- ⚡ Modern Stack - Native fetch, Node 18+, zero heavy dependencies
Installation
npm install conversiontoolsOr with Yarn:
yarn add conversiontoolsQuick Start
const { ConversionToolsClient } = require('conversiontools');
// Initialize client
const client = new ConversionToolsClient({
apiToken: 'your-api-token', // Get from https://conversiontools.io/profile
});
// Convert XML to Excel
await client.convert({
type: 'convert.xml_to_excel',
input: './data.xml',
output: './result.xlsx',
});Features
📁 100+ File Format Conversions
Convert between all major file formats:
- Documents: XML, JSON, Excel, PDF, CSV, Word, PowerPoint, Markdown
- Images: JPG, PNG, WebP, AVIF, HEIC, SVG, TIFF
- eBooks: ePUB, MOBI, AZW, AZW3, FB2
- Audio: MP3, WAV, FLAC
- Video: MP4, MOV, MKV, AVI
- And more...
🚀 Simple API
// One-liner conversions
await client.convert({
type: 'convert.json_to_excel',
input: './data.json',
output: './result.xlsx',
});
// With options
await client.convert({
type: 'convert.xml_to_csv',
input: './data.xml',
output: './result.csv',
options: {
delimiter: 'comma',
quote: true,
},
});
// URL-based conversions
await client.convert({
type: 'convert.website_to_pdf',
input: { url: 'https://example.com' },
output: './website.pdf',
options: {
images: true,
javascript: true,
},
});📊 Progress Tracking
const client = new ConversionToolsClient({
apiToken: 'your-token',
onUploadProgress: (progress) => {
console.log(`Upload: ${progress.percent}%`);
},
onConversionProgress: (progress) => {
console.log(`Converting: ${progress.percent}%`);
},
onDownloadProgress: (progress) => {
console.log(`Download: ${progress.percent}%`);
},
});
await client.convert({
type: 'convert.pdf_to_excel',
input: './large-file.pdf',
output: './result.xlsx',
});🎯 TypeScript Support
Full type safety with IntelliSense:
import { ConversionToolsClient, RateLimitError } from 'conversiontools';
const client = new ConversionToolsClient({
apiToken: 'your-token',
});
// TypeScript knows all valid conversion types and options
await client.convert({
type: 'convert.xml_to_csv',
input: './data.xml',
output: './result.csv',
options: {
delimiter: 'comma', // IntelliSense suggests: comma | semicolon | vertical_bar | tabulation
quote: true,
},
});🔄 Advanced Control
For fine-grained control over the conversion process:
// Step 1: Upload file
const fileId = await client.files.upload('./data.xml');
// Step 2: Create task
const task = await client.createTask({
type: 'convert.xml_to_excel',
options: {
file_id: fileId,
},
});
// Step 3: Wait for completion
await task.wait({
onProgress: (status) => {
console.log(`Status: ${status.status}, Progress: ${status.conversionProgress}%`);
},
});
// Step 4: Download result
await task.downloadTo('./result.xlsx');🧪 Sandbox Mode
Test your integration without consuming quota:
await client.convert({
type: 'convert.json_to_excel',
input: './test-data.json',
options: {
sandbox: true, // ✨ Doesn't count against quota
},
});🎨 Error Handling
Type-safe error handling:
import {
RateLimitError,
ValidationError,
ConversionError,
FileNotFoundError,
} from 'conversiontools';
try {
await client.convert({ /* ... */ });
} catch (error) {
if (error instanceof RateLimitError) {
console.error('Quota exceeded:', error.limits);
console.error('Upgrade at: https://conversiontools.io/api-pricing');
} else if (error instanceof ValidationError) {
console.error('Invalid input:', error.message);
} else if (error instanceof ConversionError) {
console.error('Conversion failed:', error.message);
} else if (error instanceof FileNotFoundError) {
console.error('File not found:', error.message);
}
}API Reference
ConversionToolsClient
Constructor
new ConversionToolsClient(config: ConversionToolsConfig)Config Options:
apiToken(string, required) - API token from your profilebaseURL(string, optional) - API base URL (default: https://api.conversiontools.io/v1)timeout(number, optional) - Request timeout in ms (default: 300000 / 5 min)retries(number, optional) - Retry attempts (default: 3)retryDelay(number, optional) - Initial retry delay in ms (default: 1000)pollingInterval(number, optional) - Status polling interval in ms (default: 5000)maxPollingInterval(number, optional) - Max polling interval in ms (default: 30000)webhookUrl(string, optional) - Webhook URL for task notificationsonUploadProgress(function, optional) - Upload progress callbackonDownloadProgress(function, optional) - Download progress callbackonConversionProgress(function, optional) - Conversion progress callback
Methods
convert(options)
Simple conversion method - handles upload, conversion, and download automatically.
await client.convert({
type: 'convert.xml_to_excel',
input: './data.xml', // or { url: '...' } or { stream: ... }
output: './result.xlsx',
options: { /* conversion-specific options */ },
wait: true, // Wait for completion (default: true)
callbackUrl: 'https://your-server.com/webhook', // Optional
});createTask(request)
Create a conversion task manually.
const task = await client.createTask({
type: 'convert.xml_to_excel',
options: {
file_id: 'xxx',
},
});getTask(taskId)
Get an existing task by ID.
const task = await client.getTask('task-id');getRateLimits()
Get rate limits from last API call.
const limits = client.getRateLimits();
// { daily: { limit: 30, remaining: 25 }, monthly: { limit: 300, remaining: 275 } }getUser()
Get authenticated user information.
const user = await client.getUser();
// { email: '[email protected]' }Files API
Accessible via client.files:
// Upload file
const fileId = await client.files.upload('./file.xml');
// Get file info
const info = await client.files.getInfo(fileId);
// Download file
await client.files.downloadTo(fileId, './output.xlsx');
const buffer = await client.files.downloadBuffer(fileId);
const stream = await client.files.downloadStream(fileId);Tasks API
Accessible via client.tasks:
// Create task
const response = await client.tasks.create({
type: 'convert.xml_to_excel',
options: { file_id: 'xxx' },
});
// Get task status
const status = await client.tasks.getStatus('task-id');
// List tasks
const tasks = await client.tasks.list({ status: 'SUCCESS' });Task Model
// Task properties
task.id // Task ID
task.status // 'PENDING' | 'RUNNING' | 'SUCCESS' | 'ERROR'
task.fileId // Result file ID
task.error // Error message (if failed)
task.conversionProgress // Progress (0-100)
// Task methods
await task.refresh(); // Refresh status
await task.wait(); // Wait for completion
await task.downloadTo('./output.xlsx'); // Download result
const buffer = await task.downloadBuffer(); // Get as buffer
const stream = await task.downloadStream(); // Get as streamCommon Conversion Types
Here are some frequently used conversion types:
Document Conversions
// XML to Excel
await client.convert({
type: 'convert.xml_to_excel',
input: './data.xml',
output: './result.xlsx',
});
// JSON to Excel
await client.convert({
type: 'convert.json_to_excel',
input: './data.json',
output: './result.xlsx',
});
// Excel to CSV
await client.convert({
type: 'convert.excel_to_csv',
input: './data.xlsx',
output: './result.csv',
options: {
delimiter: 'comma',
},
});
// PDF to Text
await client.convert({
type: 'convert.pdf_to_text',
input: './document.pdf',
output: './document.txt',
});
// Word to PDF
await client.convert({
type: 'convert.word_to_pdf',
input: './document.docx',
output: './document.pdf',
});Image Conversions
// PDF to JPG
await client.convert({
type: 'convert.pdf_to_jpg',
input: './document.pdf',
output: './image.jpg',
options: {
image_resolution: '300',
jpeg_quality: 90,
},
});
// PNG to WebP
await client.convert({
type: 'convert.png_to_webp',
input: './image.png',
output: './image.webp',
options: {
webp_quality: 85,
},
});Website Conversions
// Website to PDF
await client.convert({
type: 'convert.website_to_pdf',
input: { url: 'https://example.com' },
output: './website.pdf',
options: {
images: true,
javascript: true,
orientation: 'Portrait',
},
});
// Website to JPG
await client.convert({
type: 'convert.website_to_jpg',
input: { url: 'https://example.com' },
output: './screenshot.jpg',
});OCR (Text Recognition)
// OCR PDF to Text
await client.convert({
type: 'convert.ocr_pdf_to_text',
input: './scanned.pdf',
output: './text.txt',
options: {
language_ocr: 'eng', // or 'eng+fra' for multiple languages
},
});For a complete list of conversion types, see the API Documentation.
Migrating from v1
If you're upgrading from v1, see the Migration Guide.
Using the Legacy v1 API
For backward compatibility, you can use the v1 API:
const ConversionClient = require('conversiontools/legacy');
const client = new ConversionClient('your-api-token');
await client.run('convert.xml_to_csv', {
filename: 'data.xml',
outputFilename: 'result.csv',
timeout: 5000,
options: { delimiter: 'tabulation' },
});Note: The v1 API is deprecated. Please migrate to v2.
Examples
See the examples/ directory for more examples:
- Simple conversion
- With progress tracking
- Advanced manual control
- Sandbox testing
- URL-based conversions
- TypeScript usage
Requirements
- Node.js 18 or higher
- API token from Conversion Tools
License
Support
Made with ❤️ by Conversion Tools
