Catalog npm
Back to packageRELEASE DETAILS

1.2.3

npm

@0gfoundation/0g-ts-sdk

JS/TS SDK for 0G Storage

Published22 Apr 2026
Direct dependencies19
Transitive dependenciesNot calculated

README

Current package documentation

0g-ts-sdk

This is the JavaScript SDK for 0g-storage. Features include:

  • File Merkle Tree Class
  • Flow Contract Types
  • RPC methods support
  • File upload
  • Support browser environment
  • Tests for different environments
  • File download

Install

npm install @0gfoundation/0g-ts-sdk ethers

ethers is a peer dependency of this project.

Usage

Node.js environment ESM example:

Use ZgFile to create a file object, then call merkleTree method to get the merkle tree of the file.

import { Indexer, ZgFile } from '@0gfoundation/0g-ts-sdk';
import { ethers } from 'ethers';
import { exit } from 'process';

const file = await ZgFile.fromFilePath(<file_path>);
var [tree, err] = await file.merkleTree();
if (err === null) {
  console.log("File Root Hash: ", tree.rootHash());
} else {
  exit(1);
}
await file.close();

Upload file to 0g-storage:

import { getFlowContract } from '@0gfoundation/0g-ts-sdk';
const evmRpc = 'https://evmrpc-testnet.0g.ai';
const privateKey = ''; // with balance to pay for gas
const indRpc = 'https://indexer-storage-testnet-turbo.0g.ai'; // indexer rpc

const provider = new ethers.JsonRpcProvider(evmRpc);
const signer = new ethers.Wallet(privateKey, provider);

const indexer = new Indexer(indRpc);
// need to pay fees to store data in storage nodes
var [tx, err] = await indexer.upload(file, evmRpc, signer);
if (err === null) {
  console.log("File uploaded successfully, tx: ", tx);
} else {
  console.log("Error uploading file: ", err);
}

Download file from 0g-storage

err = await indexer.download(<root_hash>, <output_file>, <with_proof>);
if (err !== null) {
  console.log("Error downloading file: ", err);
}

Upload data to 0g-kv:

var [nodes, err] = await indexer.selectNodes(1);
if (err !== null) {
    console.log("Error selecting nodes: ", err);
    stop();
}

const batcher = new Batcher(1, nodes, flowContract, evmRpc);

const key1 = Uint8Array.from(Buffer.from("TESTKEY0", 'utf-8'));
const val1 = Uint8Array.from(Buffer.from("TESTVALUE0", 'utf-8'));
const key2 = Uint8Array.from(Buffer.from("TESTKEY1", 'utf-8'));
const val2 = Uint8Array.from(Buffer.from("TESTVALUE1", 'utf-8'));
batcher.streamDataBuilder.set("0x...", key1, val1);
batcher.streamDataBuilder.set("0x...", key2, val2);

var [tx, err] = await batcher.exec();

if (err === null) {
    console.log("Batcher executed successfully, tx: ", tx);
} else {
    console.log("Error executing batcher: ", err);
}

Download data from 0g-kv

const KvClientAddr = "http://3.101.147.150:6789"

const streamId = "0x..."
const kvClient = new KvClient(KvClientAddr)

let val = await kvClient.getValue(streamId, ethers.encodeBase64(key1));
console.log(val)

Browser environment example:

Import zgstorage.esm.js in your html file:

<script type="module">
  import { Blob, Indexer } from "./dist/zgstorage.esm.js";
  // Your code here...
</script>

Create file object from blob:

const file = new Blob(blob);
const [tree, err] = await file.merkleTree();
if (err === null) {
  console.log("File Root Hash: ", tree.rootHash());
}

File upload is same with node.js environment with the following provider change

import { BrowserProvider } from 'ethers';  // or from ethers.js url

let provider = new BrowserProvider(window.ethereum) // metamask need to be installed

Vite example:

To use the SDK with Vite, set up polyfills in your vite.config.ts:

import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

export default defineConfig({
  plugins: [
    ...
    nodePolyfills({
      include: ['crypto', 'buffer', 'stream', 'util', 'events'],
    }),
  ],
});

Now, you can import SDK files with the /browser suffix:

import { Indexer, Blob } from '@0gfoundation/0g-ts-sdk/browser';

Check codes in examples for more details.

Contribute

This project uses pnpm as package manager. After cloning the project, run pnpm install to install dependencies.

Generate Contract Flow Types

Make sure 0g-storage-contracts is in project sibling directory.

pnpm gen-contract-type-flow
pnpm gen-contract-type-market

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 19

Native requirements
19 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
@ethersproject/bytes
Environment
{
  "local_name": "@ethersproject/bytes",
  "alias": false,
  "peer_metadata": null
}
^5.7.0runtime
open-jsonrpc-provider
Environment
{
  "local_name": "open-jsonrpc-provider",
  "alias": false,
  "peer_metadata": null
}
^0.2.1runtime
@ethersproject/keccak256
Environment
{
  "local_name": "@ethersproject/keccak256",
  "alias": false,
  "peer_metadata": null
}
^5.7.0runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^29.6.4development
tslib
Environment
{
  "local_name": "tslib",
  "alias": false,
  "peer_metadata": null
}
^2.6.2development
rollup
Environment
{
  "local_name": "rollup",
  "alias": false,
  "peer_metadata": null
}
^3.28.1development
js-sha3
Environment
{
  "local_name": "js-sha3",
  "alias": false,
  "peer_metadata": null
}
^0.9.1development
ts-jest
Environment
{
  "local_name": "ts-jest",
  "alias": false,
  "peer_metadata": null
}
^29.1.1development
ts-node
Environment
{
  "local_name": "ts-node",
  "alias": false,
  "peer_metadata": null
}
^10.9.1development
typechain
Environment
{
  "local_name": "typechain",
  "alias": false,
  "peer_metadata": null
}
^8.3.1development
uglify-js
Environment
{
  "local_name": "uglify-js",
  "alias": false,
  "peer_metadata": null
}
^3.17.4development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^5.2.2development
@types/jest
Environment
{
  "local_name": "@types/jest",
  "alias": false,
  "peer_metadata": null
}
^29.5.4development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^20.5.9development
@typechain/ethers-v6
Environment
{
  "local_name": "@typechain/ethers-v6",
  "alias": false,
  "peer_metadata": null
}
^0.5.0development
@rollup/plugin-commonjs
Environment
{
  "local_name": "@rollup/plugin-commonjs",
  "alias": false,
  "peer_metadata": null
}
^25.0.4development
@rollup/plugin-node-resolve
Environment
{
  "local_name": "@rollup/plugin-node-resolve",
  "alias": false,
  "peer_metadata": null
}
^15.2.1development
rollup-plugin-polyfill-node
Environment
{
  "local_name": "rollup-plugin-polyfill-node",
  "alias": false,
  "peer_metadata": null
}
^0.12.0development
ethers
Environment
{
  "local_name": "ethers",
  "alias": false,
  "peer_metadata": null
}
6.13.1peer

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "22809050395ff5c717fe82c6e70e6f9508c9e5ed",
  "tarball": "https://registry.npmjs.org/@0gfoundation/0g-ts-sdk/-/0g-ts-sdk-1.2.3.tgz",
  "fileCount": 500,
  "integrity": "sha512-RfPb+tb6+1iwBn8RitFfTvdWvbz3go29PbjMVC3bvKXjOdGPaQdEFyXZOADW/asbwWTlOs/p8YFnp4M/b9B5jQ==",
  "signatures": [
    {
      "sig": "MEUCIB2iotk/8fcOMtr0zMC8u6xvoR5aRvViPTyGw30sWhrOAiEAgSWTq19Wk0XdvSya55ZtBYoHHTnkxetkL7ydnuEeYe8=",
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
    }
  ],
  "unpackedSize": 4776339
}

Repository

PENDING
0gfoundation/0g-ts-sdk

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 22:11 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:27cd2edeb56eaafa4d4a764b3a7ec2713876239e62adbe820b8942ef6fcf529a

Other collected versions

1.2.830 Apr1.2.730 Apr1.2.624 Apr1.2.524 Apr1.2.422 Apr1.2.221 AprView all collected versions →