Complete Node.js code examples for integrating with the ConvertHub API - a powerful file conversion service supporting 800+ format pairs.
- Get your API key from https://converthub.com/api
- Clone this repository:
git clone https://github.com/converthub-api/nodejs-examples.git cd nodejs-examples - Install dependencies:
npm install
- Configure your API key:
cp .env.example .env # Edit .env and add your API key - Run any example:
node simple-convert/convert.js document.pdf docx
Each directory contains working examples for specific API endpoints:
Direct file upload and conversion (files up to 50MB).
convert.js- Convert a local file with automatic download
# Basic conversion
node simple-convert/convert.js image.png jpg
# With API key option
node simple-convert/convert.js document.pdf docx --api-key=YOUR_KEYConvert files directly from URLs without downloading them first.
convert-from-url.js- Convert a file from any public URL
node url-convert/convert-from-url.js https://example.com/file.pdf docxUpload and convert large files (up to 2GB) in chunks.
upload-large-file.js- Upload large files in configurable chunks
# Default 5MB chunks
node chunked-upload/upload-large-file.js video.mov mp4
# Custom chunk size
node chunked-upload/upload-large-file.js large.pdf docx --chunk-size=10Track and manage conversion jobs with dedicated scripts for each operation.
check-status.js- Check job status and optionally watch progresscancel-job.js- Cancel a running or queued jobdelete-file.js- Delete converted file from storagedownload-result.js- Download the converted file
# Check job status
node job-management/check-status.js job_123e4567-e89b-12d3
# Watch progress until complete
node job-management/check-status.js job_123e4567-e89b-12d3 --watch
# Cancel a running job
node job-management/cancel-job.js job_123e4567-e89b-12d3
# Delete a completed file
node job-management/delete-file.js job_123e4567-e89b-12d3
# Download if complete
node job-management/check-status.js job_123e4567-e89b-12d3 --download
# Download conversion result separately
node job-management/download-result.js job_123e4567-e89b-12d3
# Download with custom path
node job-management/download-result.js job_123e4567-e89b-12d3 ./output.pdfExplore supported formats and conversions.
list-formats.js- List formats, check conversions, explore possibilities
# List all supported formats
node format-discovery/list-formats.js list
# List formats by category
node format-discovery/list-formats.js list document
node format-discovery/list-formats.js list image
node format-discovery/list-formats.js list video
# Show all conversions from a format
node format-discovery/list-formats.js conversions pdf
# Check if specific conversion is supported
node format-discovery/list-formats.js check docx pdf
# List all categories
node format-discovery/list-formats.js categoriesReceive real-time conversion notifications.
webhook-receiver.js- Production-ready webhook endpoint
Start the server and use its URL as the webhook endpoint:
# Start webhook server
node webhook-handler/webhook-receiver.js
# In another terminal, expose it publicly with ngrok
ngrok http 3000
# When submitting conversions, use the ngrok URL:
# https://abc123.ngrok.io/webhookAll API requests require a Bearer token. Get your API key at https://converthub.com/api.
# Copy the example file
cp .env.example .env
# Edit .env and add your key
API_KEY="your_api_key_here"node simple-convert/convert.js file.pdf docx --api-key=your_key_hereconst API_KEY = 'your_api_key_here';
const headers = {
'Authorization': `Bearer ${API_KEY}`
};The API supports 800+ format conversions, some popular ones include:
| Category | Formats |
|---|---|
| Images | JPG, PNG, WEBP, GIF, BMP, TIFF, SVG, HEIC, ICO, TGA |
| Documents | PDF, DOCX, DOC, TXT, RTF, ODT, HTML, MARKDOWN, TEX |
| Spreadsheets | XLSX, XLS, CSV, ODS, TSV |
| Presentations | PPTX, PPT, ODP, KEY |
| Videos | MP4, WEBM, AVI, MOV, MKV, WMV, FLV, MPG |
| Audio | MP3, WAV, OGG, M4A, FLAC, AAC, WMA, OPUS |
| eBooks | EPUB, MOBI, AZW3, FB2, LIT |
| Archives | ZIP, RAR, 7Z, TAR, GZ, BZ2 |
Customize your conversions with various options:
# With chunked upload options:
node chunked-upload/upload-large-file.js video.mov mp4 --chunk-size=10 --webhook=https://your-server.com/webhook
# Available chunked upload options:
--chunk-size=MB # Chunk size in megabytes (default: 5MB)
--webhook=URL # Webhook URL for notifications
--api-key=KEY # Your API keyAll examples include comprehensive error handling:
// Every script handles API errors properly:
if (error.response?.data?.error) {
console.error(`Error: ${error.response.data.error.message}`);
if (error.response.data.error.code) {
console.error(`Code: ${error.response.data.error.code}`);
}
}Common error codes:
AUTHENTICATION_REQUIRED- Missing or invalid API keyNO_MEMBERSHIP- No active membership foundINSUFFICIENT_CREDITS- No credits remainingFILE_TOO_LARGE- File exceeds size limitUNSUPPORTED_FORMAT- Format not supportedCONVERSION_FAILED- Processing error
| Endpoint | Limit | Script |
|---|---|---|
| Convert | 60/minute | simple-convert/convert.js |
| Convert URL | 60/minute | url-convert/convert-from-url.js |
| Status Check | 100/minute | job-management/check-status.js |
| Format Discovery | 200/minute | format-discovery/list-formats.js |
| Chunked Upload | 500/minute | chunked-upload/upload-large-file.js |
- Node.js 14.0 or higher
- npm or yarn package manager
| File | Purpose |
|---|---|
.env.example |
Environment configuration template |
.gitignore |
Git ignore rules for sensitive files |
package.json |
Node.js package dependencies |
| Simple Convert | |
simple-convert/convert.js |
Convert local files up to 50MB |
| URL Convert | |
url-convert/convert-from-url.js |
Convert files from URLs |
| Chunked Upload | |
chunked-upload/upload-large-file.js |
Upload files up to 2GB in chunks |
| Job Management | |
job-management/check-status.js |
Check job status, watch, cancel, delete, download |
job-management/download-result.js |
Download conversion results |
| Format Discovery | |
format-discovery/list-formats.js |
Explore supported formats |
| Webhook Handler | |
webhook-handler/webhook-receiver.js |
Handle webhook notifications |
node simple-convert/convert.js document.pdf docxnode url-convert/convert-from-url.js https://example.com/photo.png jpgnode chunked-upload/upload-large-file.js movie.mov mp4 --chunk-size=10node job-management/check-status.js job_abc123 --watchnode format-discovery/list-formats.js check heic jpg- API Documentation: https://converthub.com/api/docs
- Developer Dashboard: https://converthub.com/developers
- Get API Key: https://converthub.com/api
- Email Support: [email protected]
These examples are provided under the MIT License. Feel free to use and modify them for your projects.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
Built with β€οΈ by ConvertHub - Making file conversion simple and powerful.