Sui.

Post

Share your knowledge.

shamueely.
Jul 20, 2025
Expert Q&A

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
3
4
Share
Comments
.

Answers

4
290697tz.
Jul 20 2025, 23:49

To fetch on-chain object data using the Sui CLI, you use the sui client object command. This retrieves the full metadata and content of the object stored on-chain, including its type, ownership (whether it's owned, shared, or immutable), version, and fields. This is crucial when you're debugging or developing on Sui, since on-chain state plays a central role in Move-based contracts, and objects are the fundamental unit of state. Unlike EVM chains where contracts hold internal variables, Sui exposes object states directly, making the object command key for inspecting and validating your application's behavior.

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 --json

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

5
Best Answer
Comments
.
HaGiang.
Jul 23 2025, 04:05

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
    }
}
10
Comments
.
MoonBags.
Jul 23 2025, 04:24

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

  1. 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.

  2. 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.

7
Comments
.
0xduckmove.
Jul 23 2025, 04:09

// --- 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

2
Comments
.

Do you know the answer?

Please log in and share it.