# Torrents (/docs/torrents)

Torrents are TorBox's main use. You add a torrent with a magnet link or a torrent file, then control it by its
ID: reannounce, pause, resume or delete. Every request needs your [API key](/docs/authentication) in the
`Authorization: Bearer` header, except the download link endpoints, which take it as `token`.

## Add a torrent [#add-a-torrent]

[`POST /v1/api/torrents/createtorrent`](/operations/create_torrent) takes `multipart/form-data` with **either**
`magnet` or `file`. Choose **Magnet link** or **Torrent file** in the example.

**Magnet link**

```python
import os
from torbox import TorBox

client = TorBox(api_key=os.environ["TORBOX_API_KEY"])
result = client.torrents.create_torrent(magnet="magnet:?xt=urn:btih:YOUR_TORRENT_HASH")
print(result)
```

**Torrent file**

```python
import os
from torbox import TorBox

client = TorBox(api_key=os.environ["TORBOX_API_KEY"])
result = client.torrents.create_torrent(file="./ubuntu.torrent")
print(result)
```

[View endpoint](/operations/create_torrent)

| Field | Meaning |
| --- | --- |
| `magnet` / `file` | The magnet link or the torrent file. Send one of them. |
| `seed` | Seeding preference: `1` auto, `2` seed, `3` don't seed. Default is `1` or your setting. |
| `allow_zip` | Whether the torrent may be zipped. TorBox only zips torrents with 100 files or more. |
| `name` | The name you want the torrent to have. |
| `as_queued` | Queue the torrent instead of starting it (ignored on the free plan). |
| `add_only_if_cached` | Only add it if it is cached on TorBox; otherwise it is not added (`404 DOWNLOAD_NOT_CACHED`). |

Once checked, it starts downloading if your account has a free active download slot and it isn't too large
(`DOWNLOAD_TOO_LARGE`, `ACTIVE_LIMIT`). Mind the [two limits of this endpoint](/docs/rate-limits#how-createtorrent-counts).
[`asynccreatetorrent`](/operations/async_create_torrent) is the asynchronous variant.

## Check if something is cached [#check-if-something-is-cached]

[`GET /v1/api/torrents/checkcached`](/operations/get_torrent_cached_availability) takes comma-separated hashes in
`hash` (around 100 per request; repeat `hash` for more) and a `format` of `object` or `list`.

## List and track [#list-and-track]

[`GET /v1/api/torrents/mylist`](/operations/get_torrent_list) returns your torrents. Pass `id` to get one torrent
as an object instead of a list.

| Parameter | Meaning |
| --- | --- |
| `offset` | Offset of the items to get. Default `0`. |
| `limit` | Number of items per request. Default `1000`. |
| `bypass_cache` | Always get fresh information instead of the cached list. |

The list is only updated every 600 seconds unless you bypass the cache, so use `bypass_cache` sparingly.
Paginate with `offset` + `limit` until a page has fewer than `limit` items.

Download states include `downloading`, `uploading`, `stalled (no seeds)`, `paused`, `completed`, `cached`,
`metaDL` and `checkingResumeData`; the others are qBittorrent states.

## Control [#control]

[`POST /v1/api/torrents/controltorrent`](/operations/control_torrent) takes a JSON body with `torrent_id` (or
`all: true`) and an `operation` such as `reannounce`, `delete` or `resume`.

```python
import os
from torbox import TorBox
from torbox.models.control_torrent import ControlTorrent

client = TorBox(api_key=os.environ["TORBOX_API_KEY"])
result = client.torrents.control_torrent(ControlTorrent(torrent_id=123, operation="delete"))
print(result)
```

[View endpoint](/operations/control_torrent)

## Download links [#download-links]

[`GET /v1/api/torrents/requestdl`](/operations/request_torrent_download_link) returns a CDN link for a file
(`file_id`) or a zip of the torrent (`zip_link=true`). It takes your key as the `token` **query parameter**.

The endpoint description says it "opens the link for 3 hours for downloads" and that the time limit only
applies to starting downloads: once started, a download can take as long as it needs.

**Permalinks.** Instead of saving CDN links (they are not permanent), build a link that redirects:

```text
https://api.torbox.app/v1/api/torrents/requestdl?token=APIKEY&torrent_id=NUMBER&file_id=NUMBER&redirect=true
```

Opening it redirects to a fresh CDN link. This saves requests. To invalidate permalinks, reset your API key
or delete the item. Use `user_ip` to pick the CDN closest to your user when requesting on their behalf.

For many files at once use the batch variants
([GET](/operations/request_torrent_download_links_get), [POST](/operations/request_torrent_download_links_post)).

## Queued torrents [#queued-torrents]

Send `as_queued=true` when creating and the torrent is queued instead of started. Manage queued downloads with
the [Queued endpoints](/operations/get_queued_downloads).