Prerequisites
- Python 3.8 or higher
- The SDK installed:
pip install 0g-storage-sdk - A wallet with 0G tokens (get testnet tokens)
If you haven’t set up the SDK yet, follow the Installation guide first.
The upload flow
Load your file
ZgFile wraps a file handle and computes merkle trees. Use from_file_path for disk files or from_bytes for in-memory data.Upload options
Theupload_opts dict accepts:
| Key | Type | Default | Description |
|---|---|---|---|
account | LocalAccount | required | Signer for the Flow contract transaction |
tags | bytes | b"\x00" | Arbitrary tag bytes stored alongside the file |
finalityRequired | bool | True | Wait for the Flow log entry to be finalized |
taskSize | int | 10 | Segments per parallel upload task |
expectedReplica | int | 1 | Number of storage node replicas to target |
skipTx | bool | False | Skip the Flow submission if the file was already uploaded |
fee | int | 0 | Storage fee in wei (0 = auto-calculate from market contract) |
submitter | str | account address | Optional override for the on-chain submitter field |
retry_opts as a third argument — see Large Files for multi-GB upload patterns.
The download flow
Download by root hash. The SDK finds nodes that have the file, pulls segments, and writes the assembled file to disk.proof=True to verify every segment against its merkle proof during download. This is slower but guarantees byte-exact integrity.
Files typically take 3–5 minutes to propagate across storage shards after upload. If you download immediately, the indexer may not have enough replicas to serve your request yet.
Complete example
End-to-end upload and download:Upload from bytes
For in-memory data, useZgFile.from_bytes:
Check if a file already exists
Before uploading, you can skip the transaction if a file with the same root hash is already on the network. Compute the merkle root first, then query the storage nodes:Storage fees
Uploads cost a small on-chain fee paid to the Flow contract in 0G tokens. The SDK calculates the fee automatically by querying the market contract for the currentpricePerSector and multiplying by the number of sectors in your file.
- Override the fee by setting
"fee": <wei_amount>inupload_opts - The SDK prints the calculated fee to stdout before submission
- Fees are small — typically a fraction of an OG token for files up to a few megabytes
Handling errors
Upload and download return(result, err) tuples instead of raising. Always check err first:
retry_opts to indexer.upload(). See Error Handling for the full exception hierarchy.
Troubleshooting
Upload succeeds but download returns 'file not found'
Upload succeeds but download returns 'file not found'
Files take 3–5 minutes to propagate across storage shards. Wait, then retry. You can query individual storage nodes with
indexer.get_file_locations(root_hash) to see which nodes have your file.'Some direct uploads failed, but file may still propagate via network'
'Some direct uploads failed, but file may still propagate via network'
This is a non-fatal warning. The SDK submitted the file to the Flow contract and some direct segment uploads failed, but other nodes will propagate the data via gossip. The upload is considered successful once the transaction is confirmed.
Insufficient balance for storage fee
Insufficient balance for storage fee
Top up your wallet with more 0G tokens. For testnet, use faucet.0g.ai, or claim additional tokens from the hackathon faucet at 0g-faucet-hackathon.vercel.app using the code
AGENT-2026. For a rough estimate, 1 OG covers a few hundred kilobytes of storage.'Account required for transaction'
'Account required for transaction'
The
upload_opts dict must include the "account" key — the signer used to submit the Flow contract transaction. This is in addition to the signer positional argument.File upload hangs for several minutes
File upload hangs for several minutes
Large files (hundreds of MB+) take time. For files over 4 GB, use
Uploader.splitable_upload() — see Large Files.Next steps
Merkle trees & proofs
Compute root hashes, generate proofs, validate file integrity.
Large files
Splitable uploads and fragment downloads for multi-gigabyte files.
KV storage
Read and write key-value streams with access control.
Error handling
Exception hierarchy, retry logic, and error codes.