Socket
Book a DemoInstallSign in
Socket

@ctrl/magnet-link

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ctrl/magnet-link

Parse a magnet URI into an object

latest
Source
npmnpm
Version
4.2.0
Version published
Weekly downloads
2K
12.4%
Maintainers
1
Weekly downloads
 
Created
Source

Parse a magnet URI into an object

Port of webtorrent/magnet-uri by feross that uses fewer dependencies in typescript

Demo: https://magnet-link.vercel.app

Install

npm install @ctrl/magnet-link

Use

Encode

import { magnetDecode } from '@ctrl/magnet-link';

const uri =
  'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=udp%3A%2F%2Ftracker.example4.com%3A80&tr=udp%3A%2F%2Ftracker.example5.com%3A80&tr=udp%3A%2F%2Ftracker.example3.com%3A6969&tr=udp%3A%2F%2Ftracker.example2.com%3A80&tr=udp%3A%2F%2Ftracker.example1.com%3A1337';
const parsed = magnetDecode(uri);
console.log(parsed.dn); // "Leaves of Grass by Walt Whitman.epub"
console.log(parsed.infoHash); // "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36"

The entire parsed object

{
  "xt": "urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
  "dn": "Leaves of Grass by Walt Whitman.epub",
  "tr": [
    "udp://tracker.example1.com:1337",
    "udp://tracker.example2.com:80",
    "udp://tracker.example3.com:6969",
    "udp://tracker.example4.com:80",
    "udp://tracker.example5.com:80"
  ],
  "name": "Leaves of Grass by Walt Whitman.epub",
  "infoHash": "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
  "announce": [
    "udp://tracker.example1.com:1337",
    "udp://tracker.example2.com:80",
    "udp://tracker.example3.com:6969",
    "udp://tracker.example4.com:80",
    "udp://tracker.example5.com:80"
  ]
}

Decode

import { magnetEncode } from '@ctrl/magnet-link';

// convert object to magnet uri
const uri = magnetEncode({
  xt: [
    'urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1',
    'urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY',
    'urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q',
  ],
  xl: '10826029',
  dn: 'mediawiki-1.15.1.tar.gz',
  tr: [
    'udp://tracker.openbittorrent.com:80/announce',
  ],
  as: 'http://download.wikimedia.org/mediawiki/1.15/mediawiki-1.15.1.tar.gz',
  xs: [
    'http://cache.example.org/XRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5',
    'dchub://example.org',
  ],
});


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

See Also

Keywords

torrent

FAQs

Package last updated on 01 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts