Awesome Open Source
Awesome Open Source

magnet-uri travis npm downloads javascript style guide

Parse a magnet URI and return an object of keys/values.

Also works in the browser with browserify! This module is used by WebTorrent.


npm install magnet-uri



Parse a magnet URI and return an object of keys/values.

const magnet = require('magnet-uri')

// "Leaves of Grass" by Walt Whitman
const uri = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&'

const parsed = magnet.decode(uri)
console.log(parsed.dn) // "Leaves of Grass by Walt Whitman.epub"
console.log(parsed.infoHash) // "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"

The parsed magnet link object looks like this:

    "xt": "urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
    "dn": "Leaves of Grass by Walt Whitman.epub",
    "tr": [

    // added for convenience:
    "infoHash": "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
    "infoHashBuffer": ...,
    "name": "Leaves of Grass by Walt Whitman.epub",
    "announce": [


Convert an object of key/values into a magnet URI string.

const magnet = require('magnet-uri')

const uri = magnet.encode({
  xt: [
  xl: '10826029',
  dn: 'mediawiki-1.15.1.tar.gz',
  tr: [
  as: '',
  xs: [

console.log(uri) // the magnet uri

The returned magnet uri will be:


You can also use convenience key names like name (dn), infoHash (xt), infoHashBuffer (xt), publicKey (xs), publicKeyBuffer (xs), announce (tr), and keywords (kt).


MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (68,100
nodejs (3,637
browser (434
torrent (142
parse (121
bittorrent (97
browserify (49
webtorrent (43
magnet-link (17

Find Open Source By Browsing 7,000 Topics Across 59 Categories