# Get Started with WebTorrent **WebTorrent** is the first torrent client that works in the **browser**. It's easy to get started! ## Install To start using WebTorrent, simply include the [`webtorrent.min.js`](https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js) script on your page. ```html ``` This provides a `WebTorrent` function on the `window` object. ### Browserify WebTorrent also works great with [browserify](http://browserify.org/), which lets you use [node.js](http://nodejs.org/) style `require()` to organize your browser code, and load packages installed by [npm](https://npmjs.org/). ``` npm install webtorrent ``` Then use `WebTorrent` like this: ```js const WebTorrent = require('webtorrent') ``` ## Quick Examples ### Downloading a torrent (in the browser) ```js const WebTorrent = require('webtorrent') const client = new WebTorrent() // Sintel, a free, Creative Commons movie const torrentId = 'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent' client.add(torrentId, function (torrent) { // Torrents can contain many files. Let's use the .mp4 file const file = torrent.files.find(function (file) { return file.name.endsWith('.mp4') }) // Display the file by adding it to the DOM. // Supports video, audio, image files, and more! file.appendTo('body') }) ``` This supports video, audio, images, PDFs, Markdown, [and more][render-media], right out of the box. There are additional ways to access file content directly, including as a node-style stream, Buffer, or Blob URL. Video and audio content can be streamed, i.e. playback will start before the full file is downloaded. Seeking works too – WebTorrent dynamically fetches the needed torrent pieces from the network on-demand. **Note:** Downloading a torrent automatically seeds it, making it available for download by other peers. ### Creating a new torrent and seed it (in the browser) ```js const dragDrop = require('drag-drop') const WebTorrent = require('webtorrent') const client = new WebTorrent() // When user drops files on the browser, create a new torrent and start seeding it! dragDrop('body', function (files) { client.seed(files, function (torrent) { console.log('Client is seeding ' + torrent.magnetURI) }) }) ``` This example uses the [`drag-drop`][drag-drop] package, to make the HTML5 Drag and Drop API easier to work with. **Note:** If you do not use browserify, use the standalone file [`dragdrop.min.js`](https://bundle.run/drag-drop). This exports a `DragDrop` function on `window`. ### Download and save a torrent (in Node.js) ```js const WebTorrent = require('webtorrent') const client = new WebTorrent() const magnetURI = 'magnet: ...' client.add(magnetURI, { path: '/path/to/folder' }, function (torrent) { torrent.on('done', function () { console.log('torrent download finished') }) }) ``` ### Creating a new torrent and seed it (in Node.js) **Note:** Seeding a torrent that can be downloaded by browser peers (i.e. with support for WebRTC) requires [webtorrent-hybrid](https://github.com/webtorrent/webtorrent-hybrid). ```js import WebTorrent from 'webtorrent-hybrid' const client = new WebTorrent() client.seed('/seed-me.txt', function (torrent) { console.log('Client is seeding ' + torrent.magnetURI) }) ``` where **seed-me.txt** is a text file which is going to be seeded as a torrent. ### Complete HTML page example Looking for a more complete example? Look no further! This HTML example has a form input where the user can paste a magnet link and start a download over WebTorrent. Best of all, it's a single HTML page, under 70 lines! If the torrent contains images, videos, audio, or other playable files (with supported codecs), they will be added to the DOM and streamed, even before the full content is downloaded. ```html

Download files using the WebTorrent protocol (BitTorrent over WebRTC).

Log

``` ### HTML example with status showing UI This complete HTML example mimics the UI of the [webtorrent.io](https://webtorrent.io) homepage. It downloads the [sintel.torrent](https://webtorrent.io/torrents/sintel.torrent) file, streams it in the browser and outputs some statistics to the user (peers, progress, remaining time, speed...). You can try it right now on [CodePen](http://codepen.io/yciabaud/full/XdOeWM/) to see what it looks like and play around with it! Feel free to replace `torrentId` with other torrent files, or magnet links, but keep in mind that the browser can only download torrents that are seeded by WebRTC peers (web peers). Use [WebTorrent Desktop](https://webtorrent.io/desktop) or [Instant.io](https://instant.io) to seed torrents to the WebTorrent network. ```html WebTorrent video player
Downloading Seeding sintel.torrent from to 0 peers.
of
0 b/s / ↗0 b/s
``` ## More Documentation Check out the [API Documentation](//webtorrent.io/docs) and [FAQ](//webtorrent.io/faq) for more details. [render-media]: https://github.com/feross/render-media/blob/master/index.js#L12-L20 [drag-drop]: https://npmjs.com/package/drag-drop