Post
Share your knowledge.
How do I fetch on-chain object data using the Sui CLI?
I'm trying to understand this aspect of the Sui Network because I'm either building, debugging, or deploying something that touches this area. I want a detailed explanation of how this mechanism or feature works, along with relevant CLI usage, Move code structure, or architectural concepts. My goal is to gain enough clarity to apply this knowledge in a real project—whether that's a custom smart contract, an NFT system, a wallet integration, or a DeFi tool. The Sui Network has unique features compared to EVM chains, so I'm particularly interested in what sets it apart and how that affects development best practices. It would help to have sample code, command line examples, or typical errors to watch for, especially when using the Sui CLI, SDK, or deploying on localnet/testnet. Ultimately, I want to avoid common mistakes, follow the best security principles, and ensure that the functionality I’m working on behaves as expected under realistic conditions.
- Sui
- SDKs and Developer Tools
- Security Protocols
- NFT Ecosystem
Answers
4To fetch on-chain object data using the Sui CLI, you use the sui client object
You start by identifying the object ID you want to inspect—usually returned from a previous transaction, minting operation, or created in your Move module. Once you have the ID, run:
sui client object
This returns a JSON-like output that includes objectType, owner, version, and the actual fields. For example, if you’re inspecting an NFT object, you’ll see the metadata, creator address, and any other custom fields you defined in your Move struct.
Here’s a sample use case. Suppose your Move code defines an object like:
struct MyNFT has key { id: UID, name: String, description: String, }
After minting the object, you’d get an ID back. You then use the CLI to query it like this:
sui client object 0xabc123...
This will show whether the object is still owned by the creator, if it was transferred, or if it's shared. This matters for ensuring your smart contract logic (like transfer permissions or visibility) works as expected.
One common mistake is assuming you can access or mutate an object without checking its current owner or version—Sui enforces strict object versioning. If you try to pass a stale object version into a transaction, it will fail. Always fetch the current version using this command before composing your next transaction.
To dig into fields more deeply (especially for nested or complex object types), you can pass the --json flag:
sui client object
This helps when you're parsing output programmatically or integrating with an SDK.
Best practice is to always verify object data before modifying it or using it in a contract call. This reduces bugs and helps you align your frontend/backend with actual on-chain state.
For a full breakdown of how to inspect and work with objects on-chain using the CLI and SDK, check the official docs here: https://docs.sui.io/reference/cli/client#object
Let's say you published a simple Move module that creates a MyObject with a value field. After publishing, a transaction would have created an instance of MyObject with a unique ID.
module my_project::my_module {
use sui::object::{Self, UID, ID};
use sui::transfer;
use sui::tx_context::{Self, TxContext};
struct MyObject has key, store {
id: UID,
value: u64,
message: vector<u8>,
}
/// Creates a new MyObject and transfers it to the sender.
public entry fun create_my_object(initial_value: u64, msg: vector<u8>, ctx: &mut TxContext) {
let new_object = MyObject {
id: object::new(ctx),
value: initial_value,
message: msg,
};
transfer::public_transfer(new_object, tx_context::sender(ctx));
}
/// Updates the value of an existing MyObject.
public entry fun update_my_object(obj: &mut MyObject, new_value: u64) {
obj.value = new_value;
}
/// Reads the value of an MyObject (example of a view function)
public fun get_my_object_value(obj: &MyObject): u64 {
obj.value
}
}
Fetching On-Chain Object Data with Sui CLI
To get information about any object stored on the Sui Network, you use the sui client object command. This command retrieves everything about an object: its type, who owns it (or if it's shared/unchangeable), its current version, and all the data fields inside it.
This is super important for debugging and developing on Sui because objects are the core way information is stored. Unlike other blockchains where data is hidden inside contracts, Sui makes object data directly viewable, helping you check how your app is working.
How to Use It
-
Get the Object ID: You need the unique ID of the object. You usually get this ID when the object is created (like when you mint an NFT) or from a previous transaction.
-
Run the Command:
sui client object <YOUR_OBJECT_ID>Replace
<YOUR_OBJECT_ID>with the actual ID of the object.This command gives you a JSON-like output showing details like
objectType,owner,version, and the object's actual data fields. For example, if it's an NFT, you'll see its name, description, and creator.
Example Scenario
Let's say your Move code has an NFT object defined like this:
struct MyNFT has key {
id: UID,
name: String,
description: String,
}
After you create (mint) an instance of MyNFT, you'll get its unique ID. Then you can inspect it:
sui client object 0xabc123... # Replace with your actual NFT's ID
This output will tell you if the NFT is still owned by its creator, if it was transferred to someone else, or if it's a shared object. This is critical for making sure your contract's rules (like who can transfer it) are working as you expect.
// --- 1. Create a new Move package and navigate into it --- // Run these commands in your terminal: // sui move new my_first_package // cd my_first_package
// --- 2. Create the Move module file --- // Create a file named 'sources/my_first_package.move' inside 'my_first_package' directory // and paste the following Move code into it:
/* module my_first_package::my_module { use sui::object::{Self, UID}; use sui::transfer; use sui::tx_context::{Self, TxContext};
/// A simple object to demonstrate creation and transfer.
struct MyObject has key, store {
id: UID,
value: u64,
message: vector<u8>,
}
/// Creates a new MyObject and transfers it to the sender.
public entry fun create_my_object(initial_value: u64, msg: vector<u8>, ctx: &mut TxContext) {
let new_object = MyObject {
id: object::new(ctx),
value: initial_value,
message: msg,
};
transfer::public_transfer(new_object, tx_context::sender(ctx));
}
/// Updates the value of an existing MyObject.
public entry fun update_my_object(obj: &mut MyObject, new_value: u64) {
obj.value = new_value;
}
/// Reads the value of an MyObject (example of a view function)
public fun get_my_object_value(obj: &MyObject): u64 {
obj.value
}
} */
// --- 3. Publish the Move package to Sui Devnet/Testnet --- // This command will compile your Move code and deploy it to the network. // Copy the 'package ID' from the output of
Do you know the answer?
Please log in and share it.
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
- How to Maximize Profit Holding SUI: Sui Staking vs Liquid Staking616
- Why does BCS require exact field order for deserialization when Move structs have named fields?65
- Multiple Source Verification Errors" in Sui Move Module Publications - Automated Error Resolution55
- Sui Move Error - Unable to process transaction No valid gas coins found for the transaction419
- Sui Transaction Failing: Objects Reserved for Another Transaction410