# magnet-uri

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

Latest version **7.0.10** (published 2026-07-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install magnet-uri
pnpm add magnet-uri
yarn add magnet-uri
bun add magnet-uri
```

## Health

**Score 73/100 (B)** — status: active.

Positive: has types package; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.0.10 |
| Published | 2026-07-26 |
| First published | 2013-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/magnet-uri) |
| Module format | ESM |
| Node | >=12.20.0 |
| Dependencies | 3 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Author | WebTorrent LLC |
| Maintainers | feross, mafintosh, flet, watson, diegorbaquero, hicom150, jhiesey, webtorrent-bot, alxhotel |
| Keywords | bittorrent, cryptolinks, magnet, p2p, peer-to-peer, uri, urn, webtorrent |

## Links

- npm: https://www.npmjs.com/package/magnet-uri
- Repository: https://github.com/webtorrent/magnet-uri
- Homepage: https://github.com/webtorrent/magnet-uri#readme
- Issues: https://github.com/webtorrent/magnet-uri/issues
- Funding: https://github.com/sponsors/feross
- npm.io page: https://npm.io/package/magnet-uri

## Dependencies (3)

- [uint8-util](https://npm.io/package/uint8-util.md) ^2.3.2
- [bep53-range](https://npm.io/package/bep53-range.md) ^2.0.0
- [@thaunknown/thirty-two](https://npm.io/package/@thaunknown/thirty-two.md) ^1.0.6

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 7.0.10 (latest) — 2026-07-26
- 7.0.9 — 2026-07-23
- 7.0.8 — 2026-05-25
- 7.0.7 — 2025-01-04
- 7.0.6 — 2025-01-04
- 7.0.5 — 2023-05-30
- 7.0.4 — 2023-05-27
- 7.0.3 — 2023-04-02
- 7.0.2 — 2023-01-31
- 7.0.1 — 2023-01-26
- 7.0.0 — 2023-01-26
- 6.2.0 — 2021-05-12
- 6.1.1 — 2021-04-14
- 6.1.0 — 2020-11-14
- 6.0.0 — 2020-10-30
- … 36 more at https://npm.io/package/magnet-uri/versions

## README

# magnet-uri [![ci][ci-image]][ci-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]

[ci-image]: https://img.shields.io/github/workflow/status/webtorrent/magnet-uri/ci/master
[ci-url]: https://github.com/webtorrent/magnet-uri/actions
[npm-image]: https://img.shields.io/npm/v/magnet-uri.svg
[npm-url]: https://npmjs.org/package/magnet-uri
[downloads-image]: https://img.shields.io/npm/dm/magnet-uri.svg
[downloads-url]: https://npmjs.org/package/magnet-uri
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com

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

Also works in the browser with [browserify](http://browserify.org/)! This module is used by [WebTorrent](http://webtorrent.io).

## install

```
npm install magnet-uri
```

## usage

### decode

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

```js
import magnet from 'magnet-uri'

// "Leaves of Grass" by Walt Whitman
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 = 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:

```js
  {
    "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"
    ],

    // added for convenience:
    "infoHash": "d2474e86c95b19b8bcfdb92bc12c9d44667cfa36",
    "infoHashBuffer": ...,
    "name": "Leaves of Grass by Walt Whitman.epub",
    "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"
    ]
  }
```

### encode

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

```js
import magnet from 'magnet-uri'

const uri = magnet.encode({
  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'
  ]
})

console.log(uri) // the magnet uri
```

The returned magnet uri will be:

```
magnet:?xt=urn:ed2k:354B15E68FB8F36D7CD88FF94116CDC1&xt=urn:tree:tiger:7N5OAMRNGMSSEUE3ORHOKWN4WWIQ5X4EBOOTLJY&xt=urn:btih:QHQXPYWMACKDWKP47RRVIV7VOURXFE5Q&xl=10826029&dn=mediawiki-1.15.1.tar.gz&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&as=http%3A%2F%2Fdownload.wikimedia.org%2Fmediawiki%2F1.15%2Fmediawiki-1.15.1.tar.gz&xs=http%3A%2F%2Fcache.example.org%2FXRX2PEFXOOEJFRVUCX6HMZMKS5TWG4K5&xs=dchub%3A%2F%2Fexample.org
```

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

## license

MIT. Copyright (c) [Feross Aboukhadijeh](https://feross.org) and [WebTorrent, LLC](https://webtorrent.io).

---
_Source: https://npm.io/package/magnet-uri · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
