Skip to main content

0G Storage SDKs


0G offers two Software (SDKs) to seamlessly integrate decentralized storage into your applications:

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

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

  • Upload and Download Files: Securely store and retrieve data of various sizes and formats. Note: you can also use the explorers to do so.
  • 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.

Getting Started with the Go SDK

1. Installation

Download and install 0G Storage Client library, which provides the Go SDK functionalities, from the GitHub repository.

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

2. Initialization

Imports the necessary packages, including the 0G Storage Client library and creates a new storage.Client instance, providing it with the required configuration parameters:

import (
"context"
"github.com/0glabs/0g-storage-client"
)

func main() {
// ... (Obtain necessary configuration parameters: network endpoint, contract addresses, private key)

client, err := storage.NewClient(context.Background(), networkEndpoint, logContractAddress, privateKey)
if err != nil {
// Handle error
}

// ... (Use the client to interact with the 0G Storage network)
}
  • context.Background(): Creates a background context for managing the lifecycle of the client's operations.
  • networkEndpoint: The URL of the 0G network's RPC endpoint.
  • logContractAddress: The address of the 0G Log Contract on the blockchain.
  • privateKey: Your private key, used for authentication and transaction signing.

The if err != nil block handles any potential errors during client creation. Once the client is successfully created, you can use it to interact with the 0G Storage network.

3. Uploading a File

Utilize the client.UploadFile method to upload the file specified by filePath to the 0G network. The method returns the fileRoot (the unique identifier or hash of the uploaded file) and an error if any occurred during the upload process. The if err != nil block handles potential errors. If the upload is successful, the fileRoot is printed, which you'll need to retrieve the file later.

fileRoot, err := client.UploadFile(filePath)
if err != nil {
// Handle error
}

fmt.Println("File uploaded with root hash:", fileRoot)

4. Downloading a File

Uses the client.DownloadFile method to download a file from the 0G network. You need to provide the fileRoot (obtained during the upload) and the outputFilePath where you want to save the downloaded file. The method returns an error if any occurred during the download. The if err != nil block handles potential errors. If the download is successful, a success message is printed.

err := client.DownloadFile(fileRoot, outputFilePath)
if err != nil {
// Handle error
}

fmt.Println("File downloaded successfully!")