06.4 · Guide
Integrate storage
Upload and download with the TypeScript SDK on Galileo turbo.
Turbo and standard are different networks
0G Storage has a log layer for append-only archives and a key-value layer for live state. Uploads also choose a mode. Turbo and standard have different indexers and different flow contracts. A file you upload to turbo is not readable from standard.
On Galileo, turbo is the active path. Its indexer is indexer-storage-testnet-turbo.0g.ai. Standard’s testnet indexer is indexer-storage-testnet-standard.0g.ai and has been marked under maintenance. Mainnet turbo is indexer-storage-turbo.0g.ai. Mainnet standard is indexer-storage.0g.ai.
Install and upload a payload
Use a test key with faucet 0G on Galileo. Put the key in an environment file that is not committed. The SDK discovers the flow contract from the indexer, so you select the indexer rather than pasting a contract into this call.
import "dotenv/config";
import { Indexer, MemData } from "@0gfoundation/0g-storage-ts-sdk";
import { ethers } from "ethers";
const rpc = "https://evmrpc-testnet.0g.ai";
const indexerRpc = "https://indexer-storage-testnet-turbo.0g.ai";
const provider = new ethers.JsonRpcProvider(rpc);
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
const indexer = new Indexer(indexerRpc);
const file = new MemData(new TextEncoder().encode("hello 0G"));
const [rootHash, uploadError] = await indexer.upload(file, rpc, signer);
if (uploadError) throw uploadError;
console.log("root hash", rootHash);
const downloadError = await indexer.download(rootHash, "./hello.txt", true);
if (downloadError) throw downloadError;
console.log("wrote ./hello.txt");What a passing upload looks like
- The process prints a root hash and writes hello.txt containing hello 0G. The third argument to download asks the SDK to verify the Merkle proof.
- Search that root hash on storagescan-galileo.0g.ai. Mainnet uploads are searched on storagescan.0g.ai.
- An insufficient-funds error is a gas problem. Go back to the faucet. A revert that names the flow contract is often the wrong indexer, or a contract address that moved. Read docs.0g.ai/developer-hub/testnet/testnet-overview again.
- For a file on disk, the SDK’s ZgFile is the counterpart to MemData. The web starter kit is the path if you want a browser wallet instead of a key in .env.