SDK Overview
This page serves as a complete reference to all publicly available classes, methods, types, and utilities provided by the bee-js SDK. After getting started, this is the place to explore everything you can do with bee-js and revisit whenever you need a reminder of how a function works or what a type represents.
The two packages
bee-js is split across two packages:
| Package | Contents |
|---|---|
@ethersphere/bee-js | The Bee client and everything that talks to a Bee node over HTTP. |
@ethersphere/core-sdk | The Swarm primitives with no network access: byte wrappers, chunks, Mantaray, erasure coding, encryption and stamp signing. |
Everything core-sdk exports that you are likely to need is re-exported by bee-js, so a single import is usually enough:
import { Bee, Bytes, PrivateKey, Reference, Topic } from '@ethersphere/bee-js'
Install core-sdk directly when you want the primitives without the HTTP client, for example when hashing or signing chunks in a worker or on a server that never contacts a Bee node:
npm install @ethersphere/core-sdk
Namespaces
Operations on the Bee class are grouped into namespaces by subject. Instead of one flat list of methods, you reach bee.data.upload(...), bee.stamp.getAll(...), bee.feed.makeWriter(...) and so on.
| Namespace | Purpose |
|---|---|
bee.data | Raw data uploads and downloads, backed by /bytes. |
bee.file | Single-file uploads and downloads, backed by /bzz. |
bee.collection | Multi-file (directory) uploads, backed by /bzz. |
bee.chunk | Individual chunk uploads and downloads, backed by /chunks. |
bee.feed | Feed readers, writers and manifests. |
bee.soc | Single owner chunk readers and writers. |
bee.messaging | PSS and GSOC messaging. |
bee.stamp | Postage batches expressed in amount and depth. |
bee.storage | Postage batches expressed in size and duration. |
bee.tag | Tags for tracking upload progress. |
bee.pin | Local pinning and reuploading. |
bee.grantee | Access control (ACT) grantee lists. |
bee.stake | Staking and the redistribution game. |
bee.status | Node health, version, chain and reserve state. |
bee.connectivity | Peers, topology and node addresses. |
bee.wallet | Node wallet balances and external withdrawals. |
bee.chequebook | Chequebook address, balance, deposits and withdrawals. |
bee.cheque | Cheques and cashouts. |
bee.balance | SWAP balances with peers. |
bee.settlement | SWAP settlements with peers. |
bee.transaction | The node's pending transaction queue. |
A handful of methods stay directly on bee because they do not belong to any one subject: makeContentAddressedChunk, unmarshalContentAddressedChunk, makeSingleOwnerChunk, unmarshalSingleOwnerChunk, calculateSingleOwnerChunkAddress, createEnvelope and rchash.
Earlier versions exposed every method directly on bee, for example bee.uploadData and bee.getAllPostageBatch. Those are now bee.data.upload and bee.stamp.getAll. A codemod ships with the library and rewrites the call sites for you. See Migrating from v12 to v13 for the full mapping and the other breaking changes.
Type interfaces
NumberString is a branded type for marking strings that represent numbers. It interops with string and bigint
types. Where NumberString is present, number is disallowed in order to avoid pitfalls with unsafe large values.
Byte primitives
All the classes below extend Bytes, therefore the following methods are available on all of them: toUint8Array,
toHex, toBase64, toBase32, toUtf8, toJSON, static keccak256, static fromUtf8.
The toString method uses toHex.
Bytes and its subclasses may be constructed with new from Uint8Array or hex string.
Elliptic
| Name | Description | Methods |
|---|---|---|
| PrivateKey | 32 bytes private key | publicKey, sign |
| PublicKey | 64 bytes public key | address, toCompressedUint8Array, toCompressedHex |
| EthAddress | 20 bytes Ethereum address | toChecksum |
| Signature | 65 bytes signature | recoverPublicKey |
Swarm
| Name | Description | Methods |
|---|---|---|
| Reference | 32/64 bytes reference (chunk, feed) | toCid |
| Identifier | 32 bytes identifier (SOC, Feed) | - |
| TransactionId | 32 bytes transaction ID | - |
| FeedIndex | 8 bytes feed index (BE) | static fromBigInt, toBigInt |
| Topic | 32 bytes topic | static fromString |
| PeerAddress | 32 bytes peer address | - |
| BatchId | 32 bytes batch ID | - |
| Span | 8 bytes span (LE) | static fromBigInt, toBigInt |
Tokens
| Name | Description | Construction | Conversion | Arithmetic and comparison |
|---|---|---|---|---|
| DAI | ERC20 DAI token (18 digits) | static fromDecimalString, static fromWei, static fromFloat | toWeiString, toWeiBigInt, toDecimalString, toFloat, toSignificantDigits, exchangeToBZZ | plus, minus, divide, gt, gte, lt, lte, eq |
| BZZ | ERC20 BZZ token (16 digits) | static fromDecimalString, static fromPLUR, static fromFloat | toPLURString, toPLURBigInt, toDecimalString, toFloat, toSignificantDigits, exchangeToDAI | plus, minus, divide, gt, gte, lt, lte, eq |
Both are created through static factories, so they are called without new: BZZ.fromDecimalString('1'). Instances are immutable, and the arithmetic methods return a new instance rather than mutating the receiver.
Units
| Name | Description | Methods |
|---|---|---|
| Size | A size in bytes | static fromBytes, static fromKilobytes, static fromMegabytes, static fromGigabytes, static parseFromString, toBytes, toGigabytes, toFormattedString |
| Duration | A span of time | static fromSeconds, static fromHours, static fromDays, static fromWeeks, static fromYears, static fromEndDate, static parseFromString, toSeconds, toHours, toDays |
Both use 1000 rather than 1024 as the base for unit conversions, matching the Swarm papers on theoretical and effective storage capacity.
Swarm chunks
| Name | Description | Creation |
|---|---|---|
| Chunk | Span, max. 4096 bytes payload; address derived from content | makeContentAddressedChunk |
| SingleOwnerChunk | Identifier, signature, span, max. 4096 bytes payload; address derived from identifier and owner | makeSingleOwnerChunk |
Swarm primitives
| Name | Description | Methods |
|---|---|---|
| MantarayNode | Compact trie with reference values and JSON metadata | addFork, removeFork, calculateSelfAddress, find, findClosest, collect, marshal, unmarshal, saveRecursively, loadRecursively |
| ChunkSplitter | Streaming BMT that turns a byte stream into chunks | append, finalize, static root, static encryptedRoot |
| ChunkBuilder | A single chunk under construction | build, hash, encryptedHash |
| ChunkJoiner | Reassembles a chunk tree back into bytes | join, static collect, static collectEncrypted |
| Stamper | Signs postage stamps locally | stamp, getState, static fromBlank, static fromState |
MerkleTree was renamedMerkleTree is now ChunkSplitter, and ChunkBuilder.hash() returns a Reference rather than a Uint8Array. The codemod handles both changes.
Swarm objects
| Name | Description | Creation |
|---|---|---|
| SOCWriter | SingleOwnerChunk writer | bee.soc.makeWriter |
| SOCReader | SingleOwnerChunk reader | bee.soc.makeReader |
| FeedWriter | Feed writer | bee.feed.makeWriter |
| FeedReader | Feed reader | bee.feed.makeReader |
Bee API
Every bee-js method that talks to a node is listed below with the endpoint it calls and the least capable node type that can serve it. The tables are grouped by namespace. Most methods call a single endpoint; the few that compose several list each one. Methods that run entirely locally are listed at the end. Refer to the Bee API specifications for the details of each endpoint.
The node mode column reads as a minimum, so a more capable node also serves the endpoint:
- Ultra-light — any node, whether ultra-light, light or full.
- Light — a light node or a full node.
- Full — a full node only.
bee.data
| Method | Bee endpoint | Node mode |
|---|---|---|
data.upload | POST /bytes 🔗 | Light |
data.download | GET /bytes/:reference 🔗 | Ultra-light |
data.downloadReadable | GET /bytes/:reference 🔗 | Ultra-light |
data.probe | HEAD /bytes/:reference 🔗 | Ultra-light |
data.isRetrievable | GET /stewardship/:reference 🔗 | Ultra-light |
bee.file
| Method | Bee endpoint | Node mode |
|---|---|---|
file.upload | POST /bzz |