Skip to main content

0G Storage SDKs

Build decentralized storage into your applications with our powerful SDKs designed for modern development workflows.

Available SDKs

  • Go SDK: Ideal for backend systems and applications built with Go
  • TypeScript SDK: Perfect for frontend development and JavaScript-based projects

Core Features

Both SDKs provide a streamlined interface to interact with the 0G Storage network:

  • Upload and Download Files: Securely store and retrieve data of various sizes and formats
  • Manage Data: List uploaded files, check their status, and control access permissions
  • Leverage Decentralization: Benefit from the 0G network's distributed architecture for enhanced data availability, immutability, and censorship resistance

Quick Start Resources

Starter Kits Available

Get up and running quickly with our comprehensive starter kits:

Both repositories include working examples, API documentation, and everything you need to start building.

Installation

Install the 0G Storage Client library:

go get github.com/0glabs/0g-storage-client

Setup

Import Required Packages

import (
"context"
"github.com/0glabs/0g-storage-client/common/blockchain"
"github.com/0glabs/0g-storage-client/indexer"
"github.com/0glabs/0g-storage-client/transfer"
"github.com/0glabs/0g-storage-client/core"
)

Initialize Clients

Create the necessary clients to interact with the network:

// Create Web3 client for blockchain interactions
w3client := blockchain.MustNewWeb3(evmRpc, privateKey)
defer w3client.Close()

// Create indexer client for node management
indexerClient, err := indexer.NewClient(indRpc)
if err != nil {
// Handle error
}

Parameters:

  • evmRpc: 0G Chain RPC endpoint (e.g., https://evmrpc-testnet.0g.ai/)
  • privateKey: Your Ethereum private key for signing transactions
  • indRpc: Indexer RPC endpoint (e.g., https://indexer-storage-testnet-turbo.0g.ai)

Core Operations

Node Selection

Select storage nodes before performing file operations:

nodes, err := indexerClient.SelectNodes(ctx, segmentNumber, expectedReplicas, excludedNodes)
if err != nil {
// Handle error
}

Parameters:

  • ctx: Context for operation management
  • segmentNumber: Identifies which storage segment to use
  • expectedReplicas: Number of file copies to maintain (minimum 1)
  • excludedNodes: List of nodes to exclude from selection

File Upload

Upload files to the network:

// Create uploader
uploader, err := transfer.NewUploader(ctx, w3client, nodes)
if err != nil {
// Handle error
}

// Upload file
txHash, err := uploader.UploadFile(ctx, filePath)
if err != nil {
// Handle error
}

Parameters:

  • ctx: Context for upload operation
  • w3client: Web3 client instance
  • nodes: Selected storage nodes
  • filePath: Path to the file being uploaded

File Hash Calculation

Calculate a file's Merkle root hash for identification:

rootHash, err := core.MerkleRoot(filePath)
if err != nil {
// Handle error
}
fmt.Printf("File hash: %s\n", rootHash.String())
Important

Save the root hash - you'll need it to download the file later!

File Download

Download files from the network:

// Create downloader
downloader, err := transfer.NewDownloader(nodes)
if err != nil {
// Handle error
}

// Download with optional verification
err = downloader.Download(ctx, rootHash, outputPath, withProof)
if err != nil {
// Handle error
}

Parameters:

  • ctx: Context for download operation
  • rootHash: File's unique identifier (Merkle root hash)
  • outputPath: Where to save the downloaded file
  • withProof: Enable/disable Merkle proof verification (true/false)

Best Practices

  1. Error Handling: Implement proper error handling and cleanup
  2. Context Management: Use contexts for operation timeouts and cancellation
  3. Resource Cleanup: Always close clients when done using defer client.Close()
  4. Verification: Enable proof verification for sensitive files
  5. Monitoring: Track transaction status for important uploads

Additional Resources


Need more control? Consider running your own storage node to participate in the network and earn rewards.