Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including magnet-uri with all npm packages installed. Try it out:

var magnetUri = require("magnet-uri")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

magnet-uri v5.3.0

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

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), announce (tr), and keywords (kt).


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


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free