Swarm.js
This library allows you to interact with the Swarm network from JavaScript.
Getting started
-
Install
npm install swarm-js -
Import
// Loads the Swarm API pointing to the official gatewayconst swarm = ;
Examples
Uploads
-
With JSON:
-
Raw data:
const file = "test file"; // could also be an Uint8Array of binary dataswarm -
Directory:
To upload a directory, just call
swarm.upload(directory)
, where directory is an object mapping paths to entries, those containing a mime-type and the data (Uint8Array or UTF-8 String).const dir ="/foo.txt": type: "text/plain" data: "file 0""/bar.txt": type: "text/plain" data: "file 1";swarm;
-
-
From disk:
-
On Node.js:
swarm// optional, and only for kind === "directory"; -
On browsers:
// only works inside an eventdocument {swarm // could also be "directory" or "data";};
-
Downloads
-
With JSON:
-
Raw data:
const fileHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";swarm; -
Directory:
const dirHash = "7e980476df218c05ecfcb0a2ca73597193a34c5a9d6da84d54e295ecd8e0c641";swarm;
-
-
To disk:
-
On Node.js:
swarm; -
On browser:
(Just link the Swarm URL.)
-
SwarmHash
console;console;console;console;
More
For more examples, check out examples.