Complete Python 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/python-examples.git cd python-examples - Install dependencies:
pip install -r requirements.txt
- Configure your API key:
cp .env.example .env # Edit .env and add your API key - Run any example:
python simple-convert/convert.py document.pdf docx
Each directory contains working examples for specific API endpoints:
Direct file upload and conversion (files up to 50MB).
convert.py- Convert a local file with optional quality settings
# Basic conversion
python simple-convert/convert.py image.png jpg
# With options
python simple-convert/convert.py document.pdf docx --api-key=YOUR_KEYConvert files directly from URLs without downloading them first.
convert-from-url.py- Convert a file from any public URL
python url-convert/convert-from-url.py https://example.com/file.pdf docxUpload and convert large files (up to 2GB) in chunks.
upload-large-file.py- Upload large files in configurable chunks
# Default 5MB chunks
python chunked-upload/upload-large-file.py video.mov mp4
# Custom chunk size
python chunked-upload/upload-large-file.py large.pdf docx --chunk-size=10Track and manage conversion jobs with dedicated scripts for each operation.
check-status.py- Check job status and optionally watch progresscancel-job.py- Cancel a running or queued jobdelete-file.py- Delete converted file from storagedownload-result.py- Download the converted file
# Check job status
python job-management/check-status.py job_123e4567-e89b-12d3
# Watch progress until complete
python job-management/check-status.py job_123e4567-e89b-12d3 --watch
# Cancel a running job
python job-management/cancel-job.py job_123e4567-e89b-12d3
# Delete a completed file (with confirmation)
python job-management/delete-file.py job_123e4567-e89b-12d3
# Force delete without confirmation
python job-management/delete-file.py job_123e4567-e89b-12d3 --force
# Download conversion result
python job-management/download-result.py job_123e4567-e89b-12d3
# Download with custom filename
python job-management/download-result.py job_123e4567-e89b-12d3 --output=myfile.pdfExplore supported formats and conversions.
list-formats.py- List formats, check conversions, explore possibilities
# List all supported formats
python format-discovery/list-formats.py
# Show all conversions from PDF
python format-discovery/list-formats.py --from=pdf
# Check if specific conversion is supported
python format-discovery/list-formats.py --check=pdf:docxReceive real-time conversion notifications.
webhook-receiver.py- Production-ready webhook endpoint
Start the webhook server:
python webhook-handler/webhook-receiver.pyThen use the webhook URL in your conversions:
# When submitting conversions:
data = {
'file': file,
'target_format': 'pdf',
'webhook_url': 'https://your-server.com/webhook'
}All 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
CONVERTHUB_API_KEY="your_api_key_here"python simple-convert/convert.py file.pdf docx --api-key=your_key_hereAPI_KEY = 'your_api_key_here'
headers = {'Authorization': f'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:
# In simple-convert/convert.py:
python convert.py image.png jpg --quality=85 --resolution=1920x1080
# Available options:
--quality=N # Image quality (1-100)
--resolution=WxH # Output resolution
--bitrate=RATE # Audio/video bitrate (e.g., "320k")
--sample-rate=N # Audio sample rate (e.g., 44100)
--output=FILENAME # Custom output filenameAll examples include comprehensive error handling:
# Every script handles API errors properly:
if response.status_code >= 400:
error_data = response.json()
error = error_data.get('error', {})
print(f"Error: {error.get('message', 'Unknown error')}")
print(f"Code: {error.get('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.py |
| Convert URL | 60/minute | url-convert/convert-from-url.py |
| Status Check | 100/minute | job-management/check-status.py |
| Format Discovery | 200/minute | format-discovery/list-formats.py |
| Chunked Upload | 500/minute | chunked-upload/upload-large-file.py |
- Python 3.7 or higher
- Dependencies in
requirements.txt:requests- HTTP client librarypython-dotenv- Environment variable managementcolorama- Cross-platform colored terminal outputtqdm- Progress bars for uploads/downloadsFlask- Web framework for webhook receiver
| File | Purpose |
|---|---|
.env.example |
Environment configuration template |
requirements.txt |
Python package dependencies |
| Simple Convert | |
simple-convert/convert.py |
Convert local files up to 50MB |
| URL Convert | |
url-convert/convert-from-url.py |
Convert files from URLs |
| Chunked Upload | |
chunked-upload/upload-large-file.py |
Upload files up to 2GB in chunks |
| Job Management | |
job-management/check-status.py |
Check job status and watch progress |
job-management/cancel-job.py |
Cancel running or queued jobs |
job-management/delete-file.py |
Delete converted files from storage |
job-management/download-result.py |
Download conversion results |
| Format Discovery | |
format-discovery/list-formats.py |
Explore supported formats |
| Webhook Handler | |
webhook-handler/webhook-receiver.py |
Handle webhook notifications |
python simple-convert/convert.py document.pdf docxpython url-convert/convert-from-url.py https://example.com/photo.png jpgpython chunked-upload/upload-large-file.py movie.mov mp4 --chunk-size=10python job-management/check-status.py job_abc123 --watchpython format-discovery/list-formats.py --check=heic:jpgimport requests
API_KEY = 'your_api_key'
headers = {'Authorization': f'Bearer {API_KEY}'}
with open('document.pdf', 'rb') as f:
files = {'file': ('document.pdf', f, 'application/pdf')}
data = {'target_format': 'docx'}
response = requests.post(
'https://api.converthub.com/v2/convert',
headers=headers,
files=files,
data=data
)
job = response.json()
print(f"Job ID: {job['job_id']}")import requests
API_KEY = 'your_api_key'
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
data = {
'url': 'https://example.com/file.pdf',
'target_format': 'docx'
}
response = requests.post(
'https://api.converthub.com/v2/convert/url',
headers=headers,
json=data
)
job = response.json()
print(f"Job ID: {job['job_id']}")For production webhook deployment:
# Install gunicorn
pip install gunicorn
# Run with 4 workers
gunicorn -w 4 -b 0.0.0.0:8080 webhook-receiver:app# /etc/systemd/system/converthub-webhook.service
[Unit]
Description=ConvertHub Webhook Receiver
After=network.target
[Service]
User=www-data
WorkingDirectory=/path/to/webhook-handler
Environment="PATH=/path/to/venv/bin"
ExecStart=/path/to/venv/bin/gunicorn -w 4 -b 0.0.0.0:8080 webhook-receiver:app
Restart=always
[Install]
WantedBy=multi-user.targetFROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY webhook-handler/ .
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8080", "webhook-receiver:app"]- API Documentation: https://converthub.com/api/docs
- Developer Dashboard: https://converthub.com/developers
- Get API Key: https://converthub.com/api
- Email Support: support@converthub.com
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.