REST API File Conversion in Python
A complete guide to performing file conversions using the Convert Fleet REST API in Python. Convert any file format programmatically with the requests library.
You need an API key to follow this guide. Get a free API key →
1Install requests
Make sure the Python requests library is installed.
pip install requests2Universal Conversion Function
This function can convert any supported file format by passing the source file path and target format.
import requests
def convert_file(input_path: str, to_format: str, output_path: str, api_key: str):
"""
Convert any file to the specified format using Convert Fleet API.
Args:
input_path: Path to the input file
to_format: Target format (e.g. 'pdf', 'mp3', 'docx', 'png')
output_path: Where to save the converted file
api_key: Your Convert Fleet API key
"""
url = "https://convertfleet.com/api/convert"
headers = {"Authorization": f"Bearer {api_key}"}
with open(input_path, "rb") as f:
response = requests.post(
url,
files={"file": f},
data={"to": to_format},
headers=headers
)
response.raise_for_status()
with open(output_path, "wb") as out:
out.write(response.content)
print(f"Converted to {to_format}: {output_path}")
# Examples
API_KEY = "YOUR_API_KEY"
# Convert PDF to DOCX
convert_file("document.pdf", "docx", "document.docx", API_KEY)
# Convert MP4 to MP3
convert_file("video.mp4", "mp3", "audio.mp3", API_KEY)
# Convert PNG to WebP
convert_file("image.png", "webp", "image.webp", API_KEY)That's it!
You've successfully integrated the Convert Fleet API. Check the API reference for the full list of parameters and error codes.
Related API Endpoints
More Integration Guides
Start Building Today
Free tier includes 200 free credits (valid 30 days from signup). Pro plan unlocks unlimited API access.