qBittorrent for Developers: Integration and Control via the C++/Qt BitTorrent Client API
qBittorrent is a popular, free, open-source BitTorrent client developed primarily in C++ using the Qt toolkit for its cross-platform GUI, and it relies on the libtorrent-rasterbar library for the actual BitTorrent protocol implementation.
As a software engineer, its key attractive features are
Open Source (GNU GPLv2)
This means you can inspect the source code (C++ and Qt) to understand exactly how it works, contribute bug fixes or features, or learn from its robust implementation of the BitTorrent protocol.
Clean and Stable
It's known for being reliable, ad-free, and not bundling any unwanted software.
Cross-Platform
It runs on Windows, macOS, Linux, and more, making it an excellent candidate for various deployment environments (like a dedicated home server or a cloud instance).
Web API
This is the most significant feature for a software engineer. It allows you to remotely control and automate almost every aspect of the client using standard HTTP requests.
The Web API is the powerhouse for automation and integration. It allows you to build custom applications that interact with your torrent client without needing to open the GUI.
Use Cases for Software Engineers
Automation/Scripting
Automatically add new torrents, pause/resume, categorize, or delete torrents based on external events, scripts, or schedules.
Custom Front-Ends/Dashboards
Create your own web, mobile, or desktop application to monitor and manage your torrents with a personalized user interface.
Integration with Media Management Systems
Tools like Sonarr, Radarr, or other media servers often use the qBittorrent Web API to automatically handle media acquisition. You could build similar custom tools.
Monitoring and Reporting
Pull real-time transfer statistics, log data, and application status for monitoring, logging, and reporting systems.
Security Automation
Automatically bind the torrent client to a VPN network interface or toggle speed limits based on system health checks.
To use the powerful Web API, you first need to enable the Web UI in qBittorrent.
In qBittorrent, go to Tools > Options (or Preferences).
Navigate to the Web UI section.
Check the box for "Web UI" (usually under "Web User Interface").
Set a Port (e.g., 8080) and configure a Username and Password for security.
Restart qBittorrent (sometimes required for the Web UI to fully activate).
While you can interact directly with the HTTP endpoints, it's much easier to use one of the existing client libraries available for popular languages, which handle authentication and request formatting for you.
Python
qbittorrent-api (highly recommended and robust)
JavaScript/Node.js
qbittorrent-api-v2
Dart/Flutter
qbittorrent_api
Here's a friendly example using the Python client library (qbittorrent-api) to check the application version and add a new torrent via a magnet link.
First, install the Python library
pip install qbittorrent-api
This script logs in, prints the qBittorrent version, and then attempts to add a new torrent using a magnet link.
import qbittorrentapi
import sys
# --- Configuration ---
HOST = 'localhost' # Replace with your qBittorrent server's IP/hostname
PORT = 8080 # Replace with your Web UI port
USERNAME = 'your_username' # Replace with your Web UI username
PASSWORD = 'your_password' # Replace with your Web UI password
# Example magnet link for an open-source download (e.g., a Linux distribution)
MAGNET_LINK = "magnet:?xt=urn:btih:EXAMPLE_HASH&dn=Example_Torrent_Name"
try:
# 1. Instantiate the Client
qbt_client = qbittorrentapi.Client(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD
)
# The client automatically handles login/authentication on the first call.
print(f"Successfully connected to qBittorrent client on {HOST}:{PORT}")
# 2. Check Application Version (Using the 'app' namespace)
app_version = qbt_client.app.version
api_version = qbt_client.app.api_version
print(f"App Version: {app_version}")
print(f"API Version: {api_version}")
# 3. Add a new torrent (Using the 'torrents' namespace)
# The `urls` parameter accepts a single URL or a list of URLs/magnet links.
print(f"\nAttempting to add magnet link: {MAGNET_LINK[:50]}...")
# Note: For real use, you would put the result in a variable and check it.
qbt_client.torrents_add(urls=MAGNET_LINK, save_path="/downloads/movies")
print("Torrent successfully added! Check your qBittorrent client.")
except qbittorrentapi.exceptions.LoginFailed:
print("ERROR: Login failed. Check your username and password.")
except qbittorrentapi.exceptions.APIConnectionError as e:
print(f"ERROR: Could not connect to qBittorrent Web UI. Is it running? Details: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# In a real application, you might use a 'with' statement for the client
# to ensure resources are properly managed, especially if you plan to log out.
By leveraging the Web API, you transform qBittorrent from a simple desktop application into a powerful, network-accessible service that you can fully integrate into your own software ecosystem!